Testtools: Unit Testing Extensions

2.9.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates defining a test case using `testtools.TestCase`, adding custom details to test results for enhanced debugging, and explicitly skipping tests. It also shows how to invoke the `testtools` runner directly.

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()

view raw JSON →