Testtools: Unit Testing Extensions
testtools is a set of extensions to the Python standard library's unit testing framework. These extensions have been derived from many years of experience with unit testing in Python. It aims to provide the latest in unit testing technology in a way that will work with Python 3.10+ and PyPy3, with active development and regular releases.
Warnings
- breaking testtools versions 2.5.0 and later removed the dependency on and explicit references to `unittest2`. If your project implicitly relied on `unittest2` being present or imported from it, this change will cause import errors or behavioral differences.
- breaking When running on Python 3.12.1, `testtools` versions older than 2.7.2 may encounter issues due to a breaking API change within Python itself. `testtools` 2.7.2 includes a fix for this incompatibility.
- gotcha Recent versions of `testtools` (2.9.0 and later) require Python 3.10+. Attempting to install or run `testtools` on older Python environments will result in installation failures or runtime errors.
- deprecated The `distutils` integration within `testtools` was deprecated in version 2.6.0. While `distutils` itself is deprecated in Python, projects that relied on this specific `testtools` integration may need to update their build or test setup scripts.
Install
-
pip install testtools -
pip install testtools[twisted]
Imports
- TestCase
from testtools import TestCase
- text_content
from testtools.content import text_content
- run
from testtools.run import main as testtools_main
Quickstart
from testtools import TestCase
from testtools.content import text_content
from testtools.run import main as testtools_main
class MyTest(TestCase):
def test_example_success(self):
self.assertTrue(True, "This assertion should pass.")
def test_example_failure_with_detail(self):
# Add a custom detail to the test result for context on failure.
self.addDetail('debug-log', text_content("Detailed log entry from test execution."))
self.assertEqual(1, 2, "Expected 1 to equal 2, but it did not.")
def test_example_skip(self):
self.skipTest("This test is intentionally skipped as it's not relevant right now.")
if __name__ == '__main__':
# Run tests using the testtools runner.
# In larger projects, consider 'testrepository' or 'python -m testtools.run'
testtools_main()