The unittest
module provides a rich set of tools for constructing and running tests. This section demonstrates that a small subset of the tools suffice to meet the needs of most users.
Here is a short script to test three string methods:
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
A testcase is created by subclassing unittest.TestCase
. The three individual tests are defined with methods whose names start with the letters test
. This naming convention informs the test runner about which methods represent tests.
The crux of each test is a call to assertEqual()
to check for an expected result; assertTrue()
or assertFalse()
to verify a condition; or assertRaises()
to verify that a specific exception gets raised. These methods are used instead of the assert
statement so the test runner can accumulate all test results and produce a report.
The setUp()
and tearDown()
methods allow you to define instructions that will be executed before and after each test method. They are covered in more detail in the section Organizing test code.
The final block shows a simple way to run the tests. unittest.main()
provides a command-line interface to the test script. When run from the command line, the above script produces an output that looks like this:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
Passing the -v
option to your test script will instruct unittest.main()
to enable a higher level of verbosity, and produce the following output:
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
The above examples show the most commonly used unittest
features which are sufficient to meet many everyday testing needs. The remainder of the documentation explores the full feature set from first principles.
The unittest module can be used from the command line to run tests from modules, classes or even individual test methods:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
You can pass in a list with any combination of module names, and fully qualified class or method names.
Test modules can be specified by file path as well:
python -m unittest tests/test_something.py
This allows you to use the shell filename completion to specify the test module. The file specified must still be importable as a module. The path is converted to a module name by removing the ‘.py’ and converting path separators into ‘.’. If you want to execute a test file that isn’t importable as a module you should execute the file directly instead.
You can run tests with more detail (higher verbosity) by passing in the -v flag:
python -m unittest -v test_module
When executed without arguments Test Discovery is started:
python -m unittest
For a list of all the command-line options:
python -m unittest -h
Changed in version 3.2: In earlier versions it was only possible to run individual test methods and not modules or classes.