There is a good amount of unittest documentation, for the beginner the way different people have written unittest can be confusing. Here is my attempt to dispel the confusion. There are sets of many methods which does the same thing, use the one which you and your teammates can understand the best and use it consistently.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/usr/bin/python
# This is for different methods to do unittests.
# Should serve as a handy reference and quick-lookup instead of digging into the
# docs.
import unittest
def raises_error(*args, **kwargs):
print args, kwargs
raise ValueError("Invalid Value:" + str(args), str(kwargs))
class UnitTestExamples(unittest.TestCase):
def setUp(self):
print 'In setUp()'
self.fixture = range(1,10)
def tearDown(self):
print 'In tearDown()'
del self.fixture
# The following 3 Testcases are Same.
def testAssertAlmostEqual(self):
self.assertAlmostEqual(3.1, 4.24-1.1, places=1,msg="Upto 1 decimal place.")
def testAssertAlmostEquals(self):
self.assertAlmostEquals(3.1, 4.24-1.1, places=1,msg="Upto 1 decimal place.")
def testFailUnlessAlmostEqual(self):
self.failUnlessAlmostEqual(3.1, 4.24-1.1, places=1,msg="Upto 1 decimal place.")
# The following 3 Testcases are same.
def testAssertEqual(self):
self.assertEqual(True,True)
self.assertEqual(False,False)
def testAssertEquals(self):
self.assertEqual(True,True)
self.assertEquals(False,False)
def testFailUnlessEqual(self):
self.failUnlessEqual(True,True)
self.assertEquals(False,False)
# The following 2 testcases are same.
def testAssertFalse(self):
self.assertFalse(False)
def testFailIf(self):
self.failIf(False)
# The following 3 testcases are same.
def testAssertNotAlmostEqual(self):
self.assertNotAlmostEqual(1.1,1.9,places=1)
def testAssertNotAlmostEquals(self):
self.assertNotAlmostEqual(1.1,1.9,places=1)
def testFailIfAlmostEqual(self):
self.failIfAlmostEqual(1.1,1.9,places=1)
# The following 3 testcases are same.
def testAssertNoEqual(self):
self.assertNotEqual(True,False)
self.assertNotEqual(False,True)
def testAssertNoEquals(self):
self.assertNotEquals(True,False)
self.assertNotEquals(False,True)
def testFailIfEqual(self):
self.failIfEqual(False,True)
# The following 2 testcases are same.
def testAssertRaises(self):
self.assertRaises(ValueError, raises_error, 'a',b='c')
def testFailUnlessRaises(self):
self.failUnlessRaises(ValueError, raises_error, 'a',b='c')
# The following 3 testcases are same.
def testAssertTrue(self):
self.assertTrue(True)
def testFailUnless(self):
self.failUnless(True)
def testassert_(self):
self.assert_(True)
if __name__ == '__main__':
unittest.main()
|
Tags: unittests