Main Content

Combining tests into suites, and suites into bigger suites - Python and unittest

Archive - Originally posted on "The Horse's Mouth" - 2015-03-01 08:30:52 - Graham Ellis

Why are you writing code? To meet a customer requirement! And that code needs to work, to be reliable, and to continue to be reliable into the future as you update it / fix bugs. If you're a programmer, have you ever applied a "quick fix" to something just to discover (perhaps later) that you've broken something else in the process?

So I'm going to encourage you to write a test program (and write it FIRST - even before the code). That probably won't be enough, so you'll write a series of test routines - a test suite - and in most languages (including python - today's subject), you'll find that there are regression or unit test frameworks which let you manage your group of tests - for example to set up data prior to each test, run and log the results, and report on how the tests have run. This means that you can simply say "retest that [class]" and have all the checks made - with a new test being added into the suite when a bug is reported or an enhancement requested to (initially) illustrtate the issue to resolved / code to be written, then later prove that it has been written, that it works, and that the old code still works too!.

From last week:

A group of classes (myTrain) to be tested - [here]
Unittests for that group [here]

A second group (station) to be tested - [here]
Unittests for that group [here]

Note - both sets of tests are representative only - there should be a lot more tests in each suite.

Requirement to combine test suites

Complete applications and systems comprise many classes and groups, and you'll want to run all the groups of tests together. You can do that too by combining your test suites. There seems to be a lack of clear, staightforward examples around - so here is the code for combining the two sets of test described above:

  # Combining Test Suites - unittest
  
  # ----------------- Unit test framework
  import unittest
  
  # ---------------- Individual test suites
  import Traintest
  import Stationtest
  
  # ----------------- Load all the test cases
  suiteList = []
  suiteList.append(unittest.TestLoader().loadTestsFromTestCase(Traintest.TrainTest))
  suiteList.append(unittest.TestLoader().loadTestsFromTestCase(Stationtest.StationTest))
  
  # ---------------- Join them together and run them
  comboSuite = unittest.TestSuite(suiteList)
  unittest.TextTestRunner(verbosity=0).run(comboSuite)


That source available [here] too in order to complete the example.

On all of our courses, we ensure we teach the need for testing from the start. We encourage good practise, with initial design work up front, and behaviour or test driven development where appropriate.