Google Grupper understøtter ikke længere nye Usenet-opslag eller -abonnementer. Tidligere indhold er fortsat synligt.

unit test for a printing method

62 visninger
Gå til det første ulæste opslag

noro

ulæst,
28. aug. 2006, 11.53.1928.08.2006
til
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

ulæst,
28. aug. 2006, 11.58.4528.08.2006
til
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

ulæst,
28. aug. 2006, 11.59.3328.08.2006
til pytho...@python.org

Scott David Daniels

ulæst,
28. aug. 2006, 14.36.3428.08.2006
til

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

ulæst,
28. aug. 2006, 15.28.1228.08.2006
til 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

ulæst,
28. aug. 2006, 22.17.2728.08.2006
til 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

ulæst,
29. aug. 2006, 02.04.0829.08.2006
til
> [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

ulæst,
29. aug. 2006, 04.47.5229.08.2006
til 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

ulæst,
29. aug. 2006, 06.20.4929.08.2006
til

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

ulæst,
29. aug. 2006, 06.42.0129.08.2006
til 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

ulæst,
29. aug. 2006, 15.02.1229.08.2006
til
"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 nye opslag