Running test cases from the command line with increased verbosity

It is easy to adjust the test runner to print out every test method as it is run.

How to do it...

In the following steps, we will run test cases with more detailed output, giving us better insight to how things run:

  1. Create a new file called recipe3.py in which to store this recipe's code.
  2. Pick a class to test. In this case, we will use our Roman numeral converter:
    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
  3. Create a test class using the same name as the class under test with Test appended to the end.
    import unittest
    class RomanNumeralConverterTest(unittest.TestCase):
  4. Create several test methods. For this recipe, the tests have been deliberately coded to fail.
      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())
  5. Define a test suite that automatically loads all the test methods, and then runs them with a higher level of verbosity.
    if __name__ == "__main__":
      suite = unittest.TestLoader().loadTestsFromTestCase( 
                     RomanNumeralConverterTest)
      unittest.TextTestRunner(verbosity=2).run(suite)
  6. Run the file from the command line. Notice how the test method that fails prints out its Python docstring:
    How to do it...

How it works...

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.

Note

TextTestRunner is unittest's only test runner. Later in this book, we will look at other test tools that have different runners, including one that plugs in a different unittest test runner.

The previous screenshot shows each method along with its module and class name, as well as success/failure.

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.114.19