[Tutor] testing framework

Eike Welk eike.welk at gmx.net
Wed Apr 29 17:44:54 CEST 2009


Hello Spir!

On Thursday 23 April 2009, spir wrote:
> I would like to refactor tests of an application in a consistent
> form. I have a ton of tests for each module, but they are all ad
> hoc things. doctest looks great. The issue is it seems based on
> command line-like testing:

I have researched a workable solution based on the "py.test" 
framework. 
http://codespeak.net/py/dist/test/test.html

(The nose framework works in a very similar way 
http://code.google.com/p/python-nose/)

I think doctests are only for small examples that illustrate the 
docstrings. The "unittest" library has problems with library modules 
that use each other; one has to tweak "sys.path" prior to testing. 
(Doctests probably have this problem with "sys.path" too.)


To use the "py.test" script you put all your test code into separate 
files. The filenames must start with "test_". These files contain 
test functions; their names must must also start with "test_". 
The "py.test" script searches for test files and uses a lot of magic 
to do the right thing, especially it fiddles with "sys.path"

Here is your example done with "py.test":

-------- start file "test_example.py" ------------

def test_factorial():
    import example               #The import my also fail
    fact5 = example.factorial(5)
    assert fact5 == 120

-------- end file --------------------------------

"py.test" can also execute tests written with the "unittest" library 
(important for me). The test files are then structured like this:

-------- start file "test_unittest_example.py" ------------

import unittest
pytest_plugins = "pytest_unittest"

class TestExample(unittest.TestCase):
    #your existing unittest implementation goes here
    pass

-------- end file -----------------------------------------


"py.test" writes nicely formated stack traces, and code snippets into 
the terminal window. On Linux at least, it even uses a bit of color 
and bold text. If you are spoiled by graphical debuggers and 
clickable error messages (like me), you can also use the test files 
for debugging with your IDE. You put something like this at the end 
of the test file:

if __name__ == '__main__':
    test_factorial()       #This test triggers the error

So, the simplicity of its main concepts makes "py.test" quite 
convenient.


Kind regards,
Eike. 


More information about the Tutor mailing list