Skip to main content

Extensions to the Python standard library unit testing framework

Project description

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 and come from many different sources.

What better way to start than with a contrived code snippet?:

from testtools import TestCase
from testtools.content import Content
from testtools.content_type import UTF8_TEXT
from testtools.matchers import Equals

from myproject import SillySquareServer

class TestSillySquareServer(TestCase):

    def setUp(self):
        super(TestSillySquareServer, self).setUp()
        self.server = self.useFixture(SillySquareServer())
        self.addCleanup(self.attach_log_file)

    def attach_log_file(self):
        self.addDetail(
            'log-file',
            Content(UTF8_TEXT,
                    lambda: open(self.server.logfile, 'r').readlines()))

    def test_server_is_cool(self):
        self.assertThat(self.server.temperature, Equals("cool"))

    def test_square(self):
        self.assertThat(self.server.silly_square_of(7), Equals(49))

Why use testtools?

Matchers: better than assertion methods

Of course, in any serious project you want to be able to have assertions that are specific to that project and the particular problem that it is addressing. Rather than forcing you to define your own assertion methods and maintain your own inheritance hierarchy of TestCase classes, testtools lets you write your own “matchers”, custom predicates that can be plugged into a unit test:

def test_response_has_bold(self):
   # The response has bold text.
   response = self.server.getResponse()
   self.assertThat(response, HTMLContains(Tag('bold', 'b')))

More debugging info, when you need it

testtools makes it easy to add arbitrary data to your test result. If you want to know what’s in a log file when a test fails, or what the load was on the computer when a test started, or what files were open, you can add that information with TestCase.addDetail, and it will appear in the test results if that test fails.

Extend unittest, but stay compatible and re-usable

testtools goes to great lengths to allow serious test authors and test framework authors to do whatever they like with their tests and their extensions while staying compatible with the standard library’s unittest.

testtools has completely parametrized how exceptions raised in tests are mapped to TestResult methods and how tests are actually executed (ever wanted tearDown to be called regardless of whether setUp succeeds?)

It also provides many simple but handy utilities, like the ability to clone a test, a MultiTestResult object that lets many result objects get the results from one test suite, adapters to bring legacy TestResult objects into our new golden age.

Cross-Python compatibility

testtools gives you the very latest in unit testing technology in a way that will work with Python 3.10+ and PyPy3.

If you wish to use testtools with Python 2.4 or 2.5, then please use testtools 0.9.15.

If you wish to use testtools with Python 2.6 or 3.2, then please use testtools 1.9.0.

If you wish to use testtools with Python 3.3 or 3.4, then please use testtools 2.3.0.

If you wish to use testtools with Python 2.7 or 3.5, then please use testtools 2.4.0.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

testtools-2.9.0.tar.gz (220.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

testtools-2.9.0-py3-none-any.whl (110.2 kB view details)

Uploaded Python 3

File details

Details for the file testtools-2.9.0.tar.gz.

File metadata

  • Download URL: testtools-2.9.0.tar.gz
  • Upload date:
  • Size: 220.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for testtools-2.9.0.tar.gz
Algorithm Hash digest
SHA256 eb562d1a2157c6d443c8c118861eb452b3655d1842f9b61b8567ad01b32826e3
MD5 72599f8375a7c2e4cdf6871fd7e7106e
BLAKE2b-256 977bfdbe3ffdcac11e2afc1d3d3ff190e9d00d50e1be1b7fdd9fdd9780f74ca0

See more details on using hashes here.

Provenance

The following attestation bundles were made for testtools-2.9.0.tar.gz:

Publisher: publish.yml on testing-cabal/testtools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file testtools-2.9.0-py3-none-any.whl.

File metadata

  • Download URL: testtools-2.9.0-py3-none-any.whl
  • Upload date:
  • Size: 110.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for testtools-2.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32e2f96be68e2a0ed8a3ad14d9923848b8a53cbd2eaf6c3b2a2061753f8ab7c3
MD5 6783614dc918bed3f9905462494331f8
BLAKE2b-256 2f2645b958d9d40742594eb564bab1519b55e06493099e5b9e1c3d6d02deadd7

See more details on using hashes here.

Provenance

The following attestation bundles were made for testtools-2.9.0-py3-none-any.whl:

Publisher: publish.yml on testing-cabal/testtools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page