punit.setups#

Setups provide a mechanism for performing initialization before test execution.

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

Scopes#

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

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

Example#

from punit import fact, setup

@setup
def db_setup():
    global _connection
    _connection = connect_to_database()

@fact
def test_query():
    assert query(_connection) is not None
class Setup(target)#

Bases: object

Wraps a @setup-decorated initialization function or method.

Setups execute immediately before each test runs, allowing you to prepare resources or reset state without cluttering test bodies with try/finally blocks.

Setups come in two scopes: module-scoped (bare function) and class-scoped (method inside a test class). The two scopes are independent of each other.

Example#

from punit import fact, setup

@setup
def my_setup():
    prepare_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 setup function.

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

Return type:

None

setup(target)#

Decorates a function or method as a Setup that runs before each test.

A setup may be synchronous or asynchronous. If it raises an exception, the corresponding test is marked as failed but no further processing occurs for that test.

Return type:

Callable

Args:

target: The function or method to decorate as a Setup

Returns:

The original, undecorated target – no wrapper is installed

Example#

from punit import fact, setup, teardown

@setup
def db_setup():
    global _connection
    _connection = connect_to_database()

@teardown
def db_teardown():
    global _connection
    if _connection:
        _connection.close()
        _connection = None

@fact
def test_query():
    assert query(_connection) is not None
Raises:
Exception: If target is not a function/method, or if it already carries

another pUnit decorator attribute.

class SetupManager#

Bases: object

static instance()#
Return type:

SetupManager

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

Look up a setup 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 setups pass class_name = None or empty string; the manager normalises this to "".

Return type:

Optional[Setup]

put(sd)#

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

Return type:

None

record_error()#

Called when a setup function raises an exception.

Return type:

None

static reset()#

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

Return type:

None