Differences between revisions 5 and 13 (spanning 8 versions)
Revision 5 as of 2005-02-18 22:54:19
Size: 1720
Editor: 168-103-146-113
Comment: PyTest
Revision 13 as of 2019-06-20 19:32:46
Size: 1930
Editor: IgorRocha
Comment: Diveintopython.net is no longer available. Replacing it for another mirror of the book.
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

PyUnit is an easy way to create unit testing programs with Python.
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 6: Line 5:
Line 9: Line 7:
{{{
#!python
{{{#!python
Line 12: Line 9:
Line 15: Line 11:
Line 16: Line 13:
    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.'"""

    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.'"""
Line 30: Line 26:
    def testB(s):
def testB(self):
Line 33: Line 30:
    def testC(s):
def testC(self):
Line 37: Line 35:
Line 38: Line 37:
    def setUp(s):
def setUp(self):
Line 40: Line 40:
    def tearDown(s):
def tearDown(self):
Line 42: Line 43:
    def testBlah(s):
        assert s.blahblah == "blah", "blah isn't blahing blahing correctly"

def testBlah(self):
        assert self.blahblah == "blah", "blah isn't blahing blahing correctly"
Line 48: Line 51:

For more details, see [http://pyunit.sourceforge.net/pyunit.html the PyUnit manual.]
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]].
Line 52: Line 54:
 * [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
 * [[http://pyunit.sourceforge.net|SourceForge.net PyUnit site]]
 * [[http://pyunit.sourceforge.net/pyunit.html#USING|Using Section of the PyUnit manual]]
 * [[https://www.cmi.ac.in/~madhavan/courses/prog2-2012/docs/diveintopython3/unit-testing.html|Dive into Python]] chapter on unit testing
 * UnitTests
Line 56: Line 59:
Alternative:
 * PyTest
----
Line 60: Line 61:

(none yet!)
 . (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


Discussion

  • (none yet!)

PyUnit (last edited 2019-06-20 19:32:46 by IgorRocha)

Unable to edit the page? See the FrontPage for instructions.