Try It Out Testing Faulty Addition

1. Use your favorite text editor to add a second set of tests to testl.py. These will be based on the first example. Add the following to your file:

class ArithTestFail (unittest.TestCase): def runTest (self):

""" Test addition and fail. """ self.failUnless (1+1==2, 'one plus one fails!') self.failIf (1+1 != 2, 'one plus one fails again!') self.failUnlessEqual (1+1, 2, 'more trouble with one plus one!') self.faillfEqual (1+1, 2, 'expected failure here') self.faillfEqual (1+1, 2, 'second failure')

suite = unittest.TestSuite() suite.addTest (ArithTest()) suite.addTest (ArithTestFail()) return suite

You also need to change the if statement that sets off the tests, and you need to make sure that it appears at the end of your file so that it can see both classes:

runner = unittest.TextTestRunner() test_suite = suite_2() runner.run (test_suite)

2. Now run the newly modified file (after you've saved it). You'll get a very different result with the second set of tests. In fact, it'll be very different from the prior test:

FAIL: Test addition and fail.

Traceback (most recent call last):

File "D:\Documents\ch12\test1.py", line 27, in runTest self.faillfEqual (1+1, 2, 'expected failure here') AssertionError: expected failure here

Ran 2 tests in 0.000s

Was this article helpful?

0 0

Post a comment