It is easy to adjust the test runner to print out every test method as it is run.
In the following steps, we will run test cases with more detailed output, giving us better insight to how things run:
recipe3.py
in which to store this recipe's code.class RomanNumeralConverter(object): def __init__(self, roman_numeral): self.roman_numeral = roman_numeral self.digit_map = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1} def convert_to_decimal(self): val = 0 for char in self.roman_numeral: val += self.digit_map[char] return val
Test
appended to the end.import unittest class RomanNumeralConverterTest(unittest.TestCase):
def test_parsing_millenia(self): value = RomanNumeralConverter("M") self.assertEquals(1000, value.convert_to_decimal()) def test_parsing_century(self): "This test method is coded to fail for demo." value = RomanNumeralConverter("C") self.assertEquals(10, value.convert_to_decimal())
if __name__ == "__main__": suite = unittest.TestLoader().loadTestsFromTestCase( RomanNumeralConverterTest) unittest.TextTestRunner(verbosity=2).run(suite)
A key part of automated testing is organizing the tests. The base units are called test cases. These can be combined together into test suites. Python's unittest
module provides TestLoader().loadTestsFromTestCase
to fetch all the test*
methods automatically into a test suite. This test suite is then run through unittest's TextTestRunner
with an increased level of verbosity.
The previous screenshot shows each method along with its module and class name, as well as success/failure.
This recipe not only demonstrates how to turn up the verbosity of running tests, but also shows what happens when a test case fails. It renames the test
method with the document string embedded in the test
method, and prints the details later after all the test methods have been reported.
3.142.114.19