Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

unit test for a printing method

62 views
Skip to first unread message

noro

unread,
Aug 28, 2006, 11:53:19 AM8/28/06
to
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

Diez B. Roggisch

unread,
Aug 28, 2006, 11:58:45 AM8/28/06
to
noro wrote:

You can replace sys.stdout with a cStringIO-object or any other
file-protocol implementing object and such collect the data.

Diez

Fredrik Lundh

unread,
Aug 28, 2006, 11:59:33 AM8/28/06
to pytho...@python.org

Scott David Daniels

unread,
Aug 28, 2006, 2:36:34 PM8/28/06
to

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
scott....@acm.org

Fredrik Lundh

unread,
Aug 28, 2006, 3:28:12 PM8/28/06
to pytho...@python.org
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>

Gabriel Genellina

unread,
Aug 28, 2006, 10:17:27 PM8/28/06
to pytho...@python.org

>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

Marco Wahl

unread,
Aug 29, 2006, 2:04:08 AM8/29/06
to
> [OP] What is the proper way to test (using unit test) a method that print
> information?
> [...]

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

Fredrik Lundh

unread,
Aug 29, 2006, 4:47:52 AM8/29/06
to pytho...@python.org
Marco Wahl wrote:

> Fredrik Lundh <fre...@pythonware.com> writes:
>>
>> 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>

noro

unread,
Aug 29, 2006, 6:20:49 AM8/29/06
to

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

Fredrik Lundh

unread,
Aug 29, 2006, 6:42:01 AM8/29/06
to pytho...@python.org
"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>

Marco Wahl

unread,
Aug 29, 2006, 3:02:12 PM8/29/06
to
"Fredrik Lundh" <fre...@pythonware.com> writes:
>>>
>>> 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

0 new messages