May 2018
Beginner to intermediate
452 pages
11h 26m
English
Let's create a test case for the MyCalc class in the test_mycalc.py as follows:
from mycalc import MyCalcimport unittestclass TestMyCalc(unittest.TestCase): def test_add(self): mc = MyCalc(1, 10) assert mc.add() == 11if __name__ == '__main__': unittest.main()
The names of both your test modules and your test methods should be prefixed with test_. Doing so allows the unittest runner to automatically find test modules and distinguish test methods from other methods in your test case classes.
As you probably guessed, the TestCase class represents a test case. To make our test case for MyCalc, we subclass TestCase and start adding the test_ methods to test various aspects of our class. Our test_add() method creates a MyCalc ...