Reorganize the old test code. In the application scenario, there was a previous test code without unittest, which was run through another packaging file and rarely used in daily life.
Retooling old test code to run inside unittest.
Sometimes, we may have developed DEMO code to exercise our system. We don't have
Rewrite it to run it inside unittest. Instead, it is easy to hook it up to the test framework and run
It with some small changes.
1. Create a file named recipe7.py in which to put our application code that we
Will be testing.
2. Pick a class to test. In this case, we will use our Roman numeral converter.
3. Create a new file named recipe7_legacy.py to contain test code that doesn't use
The unittest module.
4. Create a set of legacy tests that are coded, based on Python's assert function, not
With unittest, along with a runner.
5. Run the legacy tests. What is wrong with this situation? Did all the test methods run?
Have we caught all the bugs?
6. Create a new file called recipe7_pyunit.py.
7. Create a unittest set of tests, Wrapping each legacy test method inside unittest's
Functiontestcase.
8. Run the unittest test. did all the tests run this time? Which test failed? Where is
The bug?
Test code:
Code# !usr/bin/env python 2.7# coding: utf-8# filename: recipe7.pyclass RomanNumeralConverter(object): def __init__(self): self.digit_map = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1} def convert_to_decimal(self, roman_numeral): val = 0 for char in roman_numeral: val += self.digit_map[char] return val
Code# !usr/bin/env python 2.7# coding: utf-8# filename: recipe7_legacy.pyfrom recipe7 import *class RomanNumeralTester(object): def __init__(self): self.cvt = RomanNumeralConverter() def simple_test(self): print "+++ Converting M to 1000" assert self.cvt.convert_to_decimal("M") == 1000 def combo_test1(self): print "+++ Converting MMX to 2010" assert self.cvt.convert_to_decimal("MMXX") == 2010 def combo_test2(self): print "+++ Converting MMMMDCLXVIII to 4668" val = self.cvt.convert_to_decimal("MMMMDCLXVII") self.check(val, 4668) def other_test(self): print "+++ Converting MMMM to 4000" val = self.cvt.convert_to_decimal("MMMM") self.check(val, 4000) def check(self, actual, expected): if (actual != expected): raise AssertionError("%s doesn't equal %s" % \ (actual, expected)) def test_the_system(self): self.simple_test() self.combo_test1() self.combo_test2() self.other_test()if __name__ == "__main__": tester = RomanNumeralTester() tester.test_the_system()
Code# !usr/bin/env python 2.7# coding: utf-8# filename: recipe7_pyunit.pyfrom recipe7 import *from recipe7_legacy import *import unittestif __name__ == "__main__": tester = RomanNumeralTester() suite = unittest.TestSuite() for test in [tester.simple_test, tester.combo_test1, \ tester.combo_test2, tester.other_test]: testcase = unittest.FunctionTestCase(test) suite.addTest(testcase) unittest.TextTestRunner(verbosity=2).run(suite)
Output result:
Unittest. Case. functiontestcase (simple_test)... ++ converting m to 1000
OK
Unittest. Case. functiontestcase (combo_test1)... ++ converting MMX to 2010
Fail
Unittest. Case. functiontestcase (combo_test2)... ++ converting mmmmdclxviii to 4668
Fail
Unittest. Case. functiontestcase (other_test)... ++ converting mmmm to 4000
OK
========================================================== ====================================
Fail: unittest. Case. functiontestcase (combo_test1)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe7_legacy.py", line 17, in combo_test1
Assert self. CVT. convert_to_decimal ("mmxx") = 2010
Assertionerror
========================================================== ====================================
Fail: unittest. Case. functiontestcase (combo_test2)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe7_legacy.py", line 22, in combo_test2
Self. Check (Val, 4668)
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe7_legacy.py", line 32, in check
(Actual, expected ))
Assertionerror: 4667 doesn' t equal 4668
----------------------------------------------------------------------
Ran 4 tests in 0.001 s
Failed (failures = 2)