415
Comment: dive into py ref
|
1693
simple code demonstrating basic unit tests
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
stub page - intend to expand with an example full test suite, as written in PyUnit. | = PyUnit = |
Line 3: | Line 3: |
http://pyunit.sourceforge.net | PyUnit is an easy way to create unit testing programs with Python. |
Line 5: | Line 5: |
reference http://pyunit.sourceforge.net/pyunit.html#USING | == Basic Example == |
Line 7: | Line 7: |
This page will just show a simple example, alone, and then point the user to the full tutorial. | Here's a basic example, making use of standard PyUnit conventions. |
Line 9: | Line 9: |
The online book [http://www.diveintopython.org/toc/index.html Dive into Python] has a chapter on unit testing in python with PyUnit. | {{{ #!python import unittest from foobarbaz import Foo # code from module you're testing class SimpleTestCase(unittest.TestCase): def setUp(s): """ Called before every single test case. """ s.foo = Foo() s.file = open( "blah", "r" ) def tearDown(s): """ Called after every single test case. """ s.file.close() def testA(s): """test case A; note that all test method names must begin with 'test.'""" assert foo.bar() == 543, "bar() not calculating values correctly" def testB(s): """test case B""" assert foo+foo == 34, "can't add Foo instances" def testC(s): """test case C""" assert foo.baz() == "blah", "baz() not returning blah correctly" class OtherTestCase(unittest.TestCase): def setUp(s): blah_blah_blah() def tearDown(s): blah_blah_blah() def testBlah(s): assert s.blahblah == "blah", "blah isn't blahing blahing correctly" if __name__ == "__main__": unittest.main() # run all tests }}} For more details, see [http://pyunit.sourceforge.net/pyunit.html the PyUnit manual.] == See Also == * [http://pyunit.sourceforge.net SourceForge.net PyUnit site] * [http://pyunit.sourceforge.net/pyunit.html#USING Using Section of the PyUnit manual] * [http://www.diveintopython.org/toc/index.html Dive into Python] chapter on unit testing with PyUnit = Discussion = (none yet!) |
PyUnit
PyUnit is an easy way to create unit testing programs with Python.
Basic Example
Here's a basic example, making use of standard PyUnit conventions.
1 import unittest
2
3 from foobarbaz import Foo # code from module you're testing
4
5 class SimpleTestCase(unittest.TestCase):
6 def setUp(s):
7 """
8 Called before every single test case.
9 """
10 s.foo = Foo()
11 s.file = open( "blah", "r" )
12 def tearDown(s):
13 """
14 Called after every single test case.
15 """
16 s.file.close()
17 def testA(s):
18 """test case A; note that all test method names must begin with 'test.'"""
19 assert foo.bar() == 543, "bar() not calculating values correctly"
20 def testB(s):
21 """test case B"""
22 assert foo+foo == 34, "can't add Foo instances"
23 def testC(s):
24 """test case C"""
25 assert foo.baz() == "blah", "baz() not returning blah correctly"
26
27 class OtherTestCase(unittest.TestCase):
28 def setUp(s):
29 blah_blah_blah()
30 def tearDown(s):
31 blah_blah_blah()
32 def testBlah(s):
33 assert s.blahblah == "blah", "blah isn't blahing blahing correctly"
34
35 if __name__ == "__main__":
36 unittest.main() # run all tests
For more details, see [http://pyunit.sourceforge.net/pyunit.html the PyUnit manual.]
See Also
[http://pyunit.sourceforge.net SourceForge.net PyUnit site]
[http://pyunit.sourceforge.net/pyunit.html#USING Using Section of the PyUnit manual]
[http://www.diveintopython.org/toc/index.html Dive into Python] chapter on unit testing with PyUnit
Discussion
- (none yet!)