Creating a test suite

It is often helpful to formally define a test suite. The unittest package is capable of discovering tests by default. When aggregating tests from multiple test modules, it's sometimes helpful to create a test suite in every test module. If each module defines a suite() function, we can replace test discovery with importing the suite() functions from each module. Also, if we customize TestRunner, we must use a suite. We can execute our tests as follows:

def suite2() -> unittest.TestSuite:
s = unittest.TestSuite()
load_from = unittest.defaultTestLoader.loadTestsFromTestCase
s.addTests(load_from(TestCard))
s.addTests(load_from(TestAceCard))
s.addTests(load_from(TestFaceCard))
return s

We build a suite from our three TestCase class definitions and then provide that suite to a unittest.TextTestRunner() instance. We use the default TestLoader in unittest. This TestLoader examines a TestCase class to locate all the test methods. The value of TestLoader.testMethodPrefix is test, which is how test methods are identified within a class. Each method name is used by the loader to create a separate test object.

Using TestLoader to build test instances from appropriately named methods of TestCase is one of the two ways to use TestCase. In a later section, we'll look at creating instances of TestCase manually; we won't rely on TestLoader for these examples. We can run this suite with the following code:

if __name__ == "__main__":
t = unittest.TextTestRunner()
t.run(suite2())

We'll see output like the following code:

...F.F 
====================================================================== 
FAIL: test_should_returnStr (__main__.TestAceCard) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
  File "p3_c15.py", line 80, in test_should_returnStr 
    self.assertEqual("A♠", str(self.ace_spades)) 
AssertionError: 'A♠' != '1♠' 
- A♠ 
+ 1♠ 
 
 
====================================================================== 
FAIL: test_should_returnStr (__main__.TestFaceCard) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
  File "p3_c15.py", line 91, in test_should_returnStr 
    self.assertEqual("Q♥", str(self.queen_hearts)) 
AssertionError: 'Q♥' != '12♥' 
- Q♥ 
+ 12♥ 
 
 
---------------------------------------------------------------------- 
Ran 6 tests in 0.001s 
 
FAILED (failures=2) 

The TestLoader class created two tests from each TestCase class. This gives us a total of six tests. The test names are the method names, which begin with test.

Clearly, we have a problem. Our tests provide an expected result that our class definitions don't meet. We've got more development work to do for the Card classes in order to pass this simple suite of unit tests. We'll leave the fixes as exercises for you.

Starting to design tests can seem daunting at first. There are a few guidelines that can be helpful. In the next section, we will talk about edges—often limits, and corners—often the interfaces between components, which can help us design better tests.

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

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