I write and test the code below with Python3.
Example production code
To explain my idea I have created an example of production code with 4 functions. The name of the file is prod.py
:
def fast_func1():
return 10
def fast_func2():
return 20
def slow_func1():
return 100
def slow_func2():
return 200
I suppose that the time of execution of the functions with the prefix fast
is short, while the time of execution of the functions with the prefix slow
is long.
Divide tests into 2 classes
In a file called test_prod.py
, I have written 4 tests. The tests are contained inside 2 separated test classes called TestFast
and TestSlow
:
class TestFast(unittest.TestCase):
def test_fast_func1(self):
self.assertEqual(10, prod.fast_func1())
def test_fast_func2(self):
self.assertEqual(20, prod.fast_func2())
class TestSlow(unittest.TestCase):
def test_slow_func1(self):
self.assertEqual(100, prod.slow_func1())
def test_slow_func2(self):
self.assertEqual(200, prod.slow_func2())
Use of the classes unittest.TestLoader
and unittest.TestSuite
In the file test_prod.py
I have used the classes TestLoader
and TestSuite
of the unittest
module to complete the test code and select the tests to execute.
The selection is done by one or more arguments passed to test_prod.py
by command line.
Below I show the complete code of the file test_prod.py
:
import unittest
import prod
import sys
class TestFast(unittest.TestCase):
def test_fast_func1(self):
self.assertEqual(10, prod.fast_func1())
def test_fast_func2(self):
self.assertEqual(20, prod.fast_func2())
class TestSlow(unittest.TestCase):
def test_slow_func1(self):
self.assertEqual(100, prod.slow_func1())
def test_slow_func2(self):
self.assertEqual(200, prod.slow_func2())
if __name__ == '__main__':
loader = unittest.TestLoader()
suite = unittest.TestSuite()
for test_class in sys.argv[1:]:
suite.addTest(loader.loadTestsFromTestCase(eval(test_class)))
runner = unittest.TextTestRunner(verbosity=3)
result = runner.run(suite)
Selection of the tests by command line
To execute the fast tests only (use the argument TestFast
that is the name of one of the classes):
> python test_prod.py TestFast
test_fast_func1 (__main__.TestFast) ... ok
test_fast_func2 (__main__.TestFast) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
To execute the slow tests only use the argument TestSlow
that is the name of the other class:
> python test_prod.py TestSlow
test_slow_func1 (__main__.TestSlow) ... ok
test_slow_func2 (__main__.TestSlow) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
To execute all tests (pass 2 arguments to the test_prod.py
script):
> python test_prod.py TestSlow TestFast
test_slow_func1 (__main__.TestSlow) ... ok
test_slow_func2 (__main__.TestSlow) ... ok
test_fast_func1 (__main__.TestFast) ... ok
test_fast_func2 (__main__.TestFast) ... ok
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK