473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

unit test for a printing method

What is the proper way to test (using unit test) a method that print
information?
for example:

def A(s):
print '---'+s+'---'

and i want to check that A("bla") really print out "---bla---"

thanks
amit

Aug 28 '06 #1
10 10671
noro wrote:
What is the proper way to test (using unit test) a method that print
information?
for example:

def A(s):
print '---'+s+'---'

and i want to check that A("bla") really print out "---bla---"
You can replace sys.stdout with a cStringIO-object or any other
file-protocol implementing object and such collect the data.

Diez
Aug 28 '06 #2
noro wrote:
What is the proper way to test (using unit test) a method that print
information?
for example:

def A(s):
print '---'+s+'---'

and i want to check that A("bla") really print out "---bla---"
http://docs.python.org/lib/module-doctest.html

</F>

Aug 28 '06 #3
noro wrote:
What is the proper way to test (using unit test) a method that print
information?
for example:

def A(s):
print '---'+s+'---'

and i want to check that A("bla") really print out "---bla---"

thanks
amit
For silly module myprog.py:
def A(s):
print '---'+s+'---'
in test_myprog.py:
import unittest
from cStringIO import StringIO # or from StringIO ...
import sys
import myprog

class SomeIOTests(unittest.TestCase):
def setUp(self):
self.held, sys.stdout = sys.stdout, StringIO()

def test_trivialArg(self):
myprog.A('')
self.assertEqual(sys.stdout.getvalue(), '------\n')

def test_simpleArg(self):
myprog.A('simple')
self.assertEqual(sys.stdout.getvalue(), '---simple---\n')

def tearDown(self):
sys.stdout = self.held

if __name__ == '__main__':
unittest.main()

--Scott David Daniels
sc***********@acm.org
Aug 28 '06 #4
Scott David Daniels wrote:
For silly module myprog.py:
def A(s):
print '---'+s+'---'
in test_myprog.py:
import unittest
from cStringIO import StringIO # or from StringIO ...
why are you trying to reinvent doctest ?

</F>

Aug 28 '06 #5
At Monday 28/8/2006 12:59, Fredrik Lundh wrote:
What is the proper way to test (using unit test) a method that print
information?
for example:

def A(s):
print '---'+s+'---'

and i want to check that A("bla") really print out "---bla---"

http://docs.python.org/lib/module-doctest.html
When the output goes a bit more complicated, consider splitting such
method in parts: some generate the output, others just print it. Then
you can test the former using the standard unittest framework.

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 29 '06 #6
[OP] What is the proper way to test (using unit test) a method that print
information?
[...]
Fredrik Lundh <fr*****@pythonware.comwrites:
>
Scott David Daniels wrote:
>For silly module myprog.py:
def A(s):
print '---'+s+'---'
in test_myprog.py:
import unittest
from cStringIO import StringIO # or from StringIO ...

why are you trying to reinvent doctest ?
The OP asked for unit test. This could be read that
the OP wants to use module unittest.

Aug 29 '06 #7
Marco Wahl wrote:
Fredrik Lundh <fr*****@pythonware.comwrites:
>>
Scott David Daniels wrote:
>>For silly module myprog.py:
def A(s):
print '---'+s+'---'
in test_myprog.py:
import unittest
from cStringIO import StringIO # or from StringIO ...

why are you trying to reinvent doctest ?

The OP asked for unit test. This could be read that
the OP wants to use module unittest.
http://docs.python.org/lib/doctest-unittest-api.html

</F>

Aug 29 '06 #8

Fredrik Lundh wrote:
Scott David Daniels wrote:
For silly module myprog.py:
def A(s):
print '---'+s+'---'
in test_myprog.py:
import unittest
from cStringIO import StringIO # or from StringIO ...

why are you trying to reinvent doctest ?

</F>
it was my understanding that "doctest" is intented to test the little
examples in a function/class documention, do people use it for more
then that, i.e - do an extentive output testing for thier apps?

amit

Aug 29 '06 #9
"noro" wrote:
>why are you trying to reinvent doctest ?

it was my understanding that "doctest" is intented to test the little
examples in a function/class documention, do people use it for more
then that, i.e - do an extentive output testing for thier apps?
yes (by using doctest to test the test programs, rather than the individual
modules).

doctest is a lot more agile than traditional unittest development -- mostly
because instead of having to write both the test code and the expected
result, you just write good test code, run the tests, and verifies the output
manually before pasting it into your test script. use your energy to come
up with good tests, not to deal with framework artifacts.

</F>

Aug 29 '06 #10
"Fredrik Lundh" <fr*****@pythonware.comwrites:
>>>
why are you trying to reinvent doctest ?

The OP asked for unit test. This could be read that
the OP wants to use module unittest.

http://docs.python.org/lib/doctest-unittest-api.html
Ahh, that's good to know that doctests can be
integrated into unittest suites.

Thank you for the information

Aug 29 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
5
by: Andy | last post by:
How can you unit test nested functions? Or do you have to pull them out to unit test them, which basically means I will never use nested functions. Also, same thing with private member functions...
5
by: VvanN | last post by:
hi, fellows I'd like to intruduce a new unit test framework for C++ freely available at: http://unit--.sourceforge.net/ It does not need bothering test registration, here is an example ...
5
by: shuisheng | last post by:
Dear All, I was told that unit test is a powerful tool for progamming. If I am writing a GUI code, is it possible to still using unit test? I have a little experience in using unittest++. But...
2
by: lpcarignan | last post by:
Hi all, Right now, I'm debugging a C# library by starting an external application. To do this, I go in the project settings of the C# project, go in the Debug section and check the radio button...
1
by: rich_sposato | last post by:
I released version 2.0 of C++ Unit Test Library. You can download it from SourceForget.Net at http://sourceforge.net/projects/cppunittest/ .. I wrote this unit test library because other unit...
6
by: Vyacheslav Maslov | last post by:
Hi all! I have many many many python unit test, which are used for testing some remote web service. The most important issue here is logging of test execution process and result. I strongly...
2
by: =?Utf-8?B?QXJtaW4gR2FsbGlrZXI=?= | last post by:
Hi I've got an unexpected error in a unit test. I want to test a activity from Windows Workflow Foundation (WF). First, I executed the test outside of the activity just in the test-init...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
I was wonder if there is any protocol or suggestions on how to setup unit testing projects in large solution??? If you have a large solution of say 50 projects and add a project for every unit...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.