279
Comment:
|
1883
Added mention of "unittest". Added link to https://docs.python.org/3.6/library/unittest.html. Fixed <Dive into Python> link, which was dead. Relocated final "UnitTests" wikipage reference.
|
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 = PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.) |
Line 3: | Line 4: |
http://pyunit.sourceforge.net | == Basic Example == Here's a basic example, making use of standard PyUnit conventions. |
Line 5: | Line 7: |
reference http://pyunit.sourceforge.net/pyunit.html#USING | {{{#!python import unittest from foobarbaz import Foo # code from module you're testing |
Line 7: | Line 11: |
This page will just show a simple example, alone, and then point the user to the full tutorial. | class SimpleTestCase(unittest.TestCase): def setUp(self): """Call before every test case.""" self.foo = Foo() self.file = open( "blah", "r" ) def tearDown(self): """Call after every test case.""" self.file.close() def testA(self): """Test case A. note that all test method names must begin with 'test.'""" assert foo.bar() == 543, "bar() not calculating values correctly" def testB(self): """test case B""" assert foo+foo == 34, "can't add Foo instances" def testC(self): """test case C""" assert foo.baz() == "blah", "baz() not returning blah correctly" class OtherTestCase(unittest.TestCase): def setUp(self): blah_blah_blah() def tearDown(self): blah_blah_blah() def testBlah(self): assert self.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]] and "unittest" at [[https://docs.python.org/3.6/library/unittest.html|docs.python.org]]. == 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.diveintopython3.net|Dive into Python]] chapter on unit testing with PyUnit * UnitTests ---- = Discussion = . (none yet!) |
PyUnit
PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.)
Basic Example
Here's a basic example, making use of standard PyUnit conventions.
1 import unittest
2 from foobarbaz import Foo # code from module you're testing
3
4
5 class SimpleTestCase(unittest.TestCase):
6
7 def setUp(self):
8 """Call before every test case."""
9 self.foo = Foo()
10 self.file = open( "blah", "r" )
11
12 def tearDown(self):
13 """Call after every test case."""
14 self.file.close()
15
16 def testA(self):
17 """Test case A. note that all test method names must begin with 'test.'"""
18 assert foo.bar() == 543, "bar() not calculating values correctly"
19
20 def testB(self):
21 """test case B"""
22 assert foo+foo == 34, "can't add Foo instances"
23
24 def testC(self):
25 """test case C"""
26 assert foo.baz() == "blah", "baz() not returning blah correctly"
27
28
29 class OtherTestCase(unittest.TestCase):
30
31 def setUp(self):
32 blah_blah_blah()
33
34 def tearDown(self):
35 blah_blah_blah()
36
37 def testBlah(self):
38 assert self.blahblah == "blah", "blah isn't blahing blahing correctly"
39
40
41 if __name__ == "__main__":
42 unittest.main() # run all tests
For more details, see the PyUnit manual and "unittest" at docs.python.org.
See Also
Dive into Python chapter on unit testing with PyUnit
Discussion
- (none yet!)