punit.theories#

Theories are tests that validate behavior across a variant arrangement of state.

State is usually acquired from an external source, separated from the test definition. A Theory requires at least one data decorator (such as @inlinedata) in addition to the @theory decorator for execution.

Example#

from punit import theory, inlinedata

@theory
@inlinedata(0, 1, 1)
@inlinedata(1, 1, 2)
@inlinedata(1, 2, 3)
def test_addition(a, b, c):
    assert a + b == c
class Theory(target)#

Bases: object

Wraps a parameterised test (@theory) and its collected data points.

Example#

@theory
def add_correct(x, y): ...

@inlinedata((1, 2), (3, 4))
@theory
def add_correct(x, y):
    assert x + y == x + y
property datas: list[tuple]#
property metadata: CallableMetadata#
property target: LambdaType | MethodType | BuiltinMethodType | Callable#
async execute(module, data)#
Return type:

Any | None

theory(target)#

Decorates a function or method as a ‘Theory-based’ parameterized test.

Theories validate behavior across a variant arrangement of state. A theory decorator alone is insufficient for execution – you must also supply data using at least one data decorator (such as @inlinedata).

Return type:

Callable

Args:

target: The function or method to decorate as a Theory test

Returns:

The original, undecorated target – no wrapper is installed

Example#

from punit import theory, inlinedata

@theory
@inlinedata(0, 1, 1)
@inlinedata(1, 1, 2)
@inlinedata(1, 2, 3)
def myFunction(a, b, c):
    assert a + b == c
Raises:
Exception: If target is not a function/method, or if it already carries

another pUnit decorator attribute.

inlinedata(*args)#

Decorates a ‘Theory-based’ test with inline data points for parameterization.

Each call to @inlinedata provides one set of arguments that will be passed to the theory function as a tuple. Multiple @inlinedata decorators may be stacked; each one adds another data point.

Return type:

Callable

Args:
*args: One or more positional values for this data point. These become

the tuple of arguments passed to the theory function.

Returns:

A wrapper that attaches the data point to the target via TheoryManager

Example#

from punit import theory, inlinedata

@theory
@inlinedata(0, 1, 1)
@inlinedata(1, 1, 2)
def add_correct(a, b, c):
    assert a + b == c
class TheoryManager#

Bases: object

static instance()#
Return type:

TheoryManager

property excludeTraits: list[Trait]#
property includeTraits: list[Trait]#
get(module_name)#
Return type:

list[Theory]

put(theory)#
Return type:

None

withData(target, data)#
Return type:

None