punit.teardowns#

Teardowns provide a mechanism for performing cleanup after test execution.

A @teardown-decorated function is executed immediately after each test runs, allowing you to release resources or reset state without cluttering test bodies with try/finally blocks.

Scopes#

  • Module-scoped; a bare function decorated with @teardown; fires once per test across the entire module.

  • Class-scoped; a method inside a test class decorated with @teardown; fires once per test within that specific class only.

Example#

from punit import fact, teardown

@teardown
def my_teardown():
    cleanup_database()

@fact
def test_something():
    assert True
class Teardown(target)#

Bases: object

Wraps a @teardown-decorated cleanup function or method.

Teardowns execute immediately after each test runs, allowing you to release resources or reset state without cluttering test bodies with try/finally blocks.

Like setups, teardowns come in module-scoped and class-scoped variants. The two scopes are independent of each other.

Example#

from punit import fact, teardown

@teardown
def my_teardown():
    cleanup_database()

@fact
def test_something():
    assert True
property metadata: CallableMetadata#
property scope_type: str#
property target: LambdaType | MethodType | BuiltinMethodType | Callable#
async execute(module, obj=None)#

Execute the teardown function.

For module-scoped teardowns, obj is ignored. For class-scoped teardowns on methods, obj should be an instance of the decorated class; it is used as self via method binding.

Return type:

None

teardown(target)#

Decorates a function or method as a Teardown that runs after each test.

A teardown may be synchronous or asynchronous, just like Facts and Theories. If a teardown raises an exception, the corresponding test is marked as failed.

Return type:

Callable

Args:

target: The function or method to decorate as a Teardown

Returns:

The original, undecorated target – no wrapper is installed

Example#

from punit import fact, teardown

class MyTestClass:
    @fact
    def test_a(self):
        assert True

    @teardown
    def tearDownClass(self):
        reset_temp_files()
Raises:
Exception: If target is not a function/method, or if it already carries

another pUnit decorator attribute.

class TeardownManager#

Bases: object

static instance()#
Return type:

TeardownManager

property teardown_error_count: int#
get(scope_type, module_name, class_name=None)#

Look up a teardown by (scope_type, module_name, class_name).

For class-scoped lookups class_name must match the first segment of the decorated method’s __qualname__. For module-scoped teardowns pass class_name = None or empty string; the manager normalises this to "".

Return type:

Optional[Teardown]

put(td)#

Store a teardown keyed by (scope_type, module_name, class_or_empty).

Return type:

None

record_error()#

Called when a teardown function raises an exception.

Return type:

None

static reset()#

Reset the singleton (used between test modules / suites).

Return type:

None