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:
objectWraps 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
@inlinedataprovides one set of arguments that will be passed to the theory function as a tuple. Multiple@inlinedatadecorators 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