O'Reilly logo

Python Essentials by Steven F. Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the unittest library for testing

For more complex testing, the doctest examples may not provide enough depth or flexibility. A docstring with a large number of cases would become too long to be effective as documentation. A docstring with complex test setup, teardown, or mock objects may not be useful as documentation either.

For these cases, we'll use the unittest module to define test cases and test suites. When using unittest, we'll generally create separate modules. These test modules will contain TestCase classes that contain test methods.

Here's a quick overview of a typical test case class definition:

import unittest from Chapter_7.ch07_ex1 import FtoC class Test_FtoC(unittest.TestCase): def setUp(self): self.temps= [50, 60, 72] def ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required