Decorators
@resource
Register a function as a dependency injection resource. Signature:| Name | Type | Default | Description |
|---|---|---|---|
fn | Callable | None | None | The resource factory function |
scope | Scope | str | Scope.CASE | Lifecycle scope: "case", "suite", or "session" |
on_resolve | Callable | None | None | Hook called once when resource is first created |
on_injection | Callable | None | None | Hook called every time resource is injected |
on_teardown | Callable | None | None | Hook called after generator teardown runs |
@sut
Register a function factory as a traced system-under-test resource. Signature:| Name | Type | Default | Description |
|---|---|---|---|
fn | Callable | None | None | The SUT factory function to register |
scope | Scope | str | Scope.CASE | Resource lifecycle scope: "case", "suite", "session" |
method | str | "__call__" | Method to trace when the factory returns a non-callable instance |
validate_cases | list[Case[Any]] | None | None | Cases to validate against the resolved SUT signature (raises on invalid input) |
@parametrize
Run a merit function with multiple parameter combinations. Signature:| Name | Type | Default | Description |
|---|---|---|---|
argnames | str | Sequence[str] | - | Parameter name(s) as string or sequence |
argvalues | Iterable[Any] | - | List of value tuples for each parameter combination |
ids | Sequence[str] | None | None | Optional custom IDs for each test case |
@repeat
Run a merit function multiple times to test consistency. Signature:| Name | Type | Default | Description |
|---|---|---|---|
count | int | - | Number of times to run the merit |
min_passes | int | None | count | Minimum passes required (defaults to all) |
@run_inline
Run a synchronous merit inline on the event-loop thread. By default, synchronous merits are offloaded to a worker thread viaasyncio.to_thread(...). Use @run_inline to opt out when thread affinity matters.
Signature:
| Name | Type | Description |
|---|---|---|
fn | Callable[..., Any] | Synchronous merit function to mark for inline execution |
- Applies only to synchronous
def merit_*functions. async def merit_*functions already run on the event-loop thread.
@tag
Add tags to merit functions or classes for filtering and organization. Signature:| Name | Type | Description |
|---|---|---|
*names | str | Tag names to apply |
@tag.skip
Skip a merit function with an optional reason. Signature:| Name | Type | Default | Description |
|---|---|---|---|
reason | str | None | None | Optional explanation for why merit is skipped |
@tag.xfail
Mark a merit as expected to fail. Signature:| Name | Type | Default | Description |
|---|---|---|---|
reason | str | None | None | Optional explanation for expected failure |
strict | bool | False | If True, passing is treated as a failure (unexpected pass) |
Classes
Case
Container for test case inputs and reference data. Attributes:| Name | Type | Description |
|---|---|---|
id | UUID | Unique identifier (auto-generated) |
tags | set[str] | Tags for filtering or categorization |
metadata | dict[str, str | int | float | bool | None] | Arbitrary key-value pairs |
references | RefsT | Reference data for validation (typed or dict), defaults to {} |
sut_input_values | dict[str, Any] | Input arguments to pass to the SUT |
CaseGroup
Container for grouping related cases with group-level references and a pass threshold. Type Parameters:| Name | Default | Description |
|---|---|---|
RefsT | dict[str, Any] | Type of each case’s references |
GroupRefsT | dict[str, Any] | Type of the group’s references |
| Name | Type | Description |
|---|---|---|
name | str | Unique group identifier (used in reports and ID suffixes) |
cases | list[Case[RefsT]] | One or more cases in this group (min 1) |
references | GroupRefsT | Group-level reference data, defaults to {} |
min_passes | int | Minimum case passes required for the group to pass (default 1, must be ≥ 1 and ≤ len(cases)) |
Scope
Enum defining resource lifecycle scopes. Values:| Value | Description |
|---|---|
Scope.CASE | Fresh instance per parametrized merit case |
Scope.SUITE | Shared within a single merit file/module |
Scope.SESSION | Shared across entire merit run |
Runner
Execute discovered merits and return aMeritRun.
Signature:
timeout):
timeoutis a run-level limit (not per-test).- Cancellation is cooperative: on timeout, the run is marked
stopped_earlyand no new tests are started. - In-flight work may not stop immediately, especially synchronous merits already executing in worker threads.
run_idaccepts either aUUIDobject or UUID string.run()run_idoverrides constructorrun_id.- If neither is provided, Merit auto-generates a new UUID.
- If
save_to_db=Trueand the selected run UUID already exists,run()raisesValueError. run_id_exists()can be used as a preflight check before execution.
Functions
Imperative Outcome Control
Merit provides imperative functions to control test outcomes at runtime. These are different from decorators and are used for conditional control flow within tests.skip
Skip the current test imperatively. Signature:| Name | Type | Default | Description |
|---|---|---|---|
reason | str | "" | Explanation for why the test is being skipped |
SkipTest exception)
Example:
fail
Explicitly fail the current test. Signature:| Name | Type | Default | Description |
|---|---|---|---|
reason | str | "" | Explanation for why the test is failing |
FailTest exception)
Example:
xfail
Mark the current test as expected to fail and stop execution. Signature:| Name | Type | Default | Description |
|---|---|---|---|
reason | str | "" | Explanation for why the test is expected to fail |
XFailTest exception)
Example:
merit.skip()(function) vs@merit.tag.skip()(decorator)merit.fail()(function) vs no decorator equivalentmerit.xfail()(function) vs@merit.tag.xfail()(decorator)
iter_cases
Decorator to run a merit function for each case. Signature:| Name | Type | Description |
|---|---|---|
*cases | Case[RefsT] | One or more test cases to iterate over |
min_passes | int | None | Minimum number of passing case executions required for the parent merit to pass. Defaults to all cases. |
min_passesmust be>= 1min_passescannot exceed the number of providedcases
iter_case_groups
Decorator to run a merit function for each case group, iterating cases within each group. Signature:| Name | Type | Description |
|---|---|---|
*groups | CaseGroup[RefsT, GroupRefsT] | One or more case groups to iterate over |
| Name | Type | Description |
|---|---|---|
group | CaseGroup | The current group being executed |
case | Case | The current case within the group |
- Each group produces a nested execution; within each group, cases are iterated using the group’s
min_passesthreshold. - The parent merit passes only if all groups pass (i.e. every group meets its own
min_passes).
- At least one group is required (empty call sets a deferred definition error)
SUT case validation
Validate case inputs by passing them to@sut(validate_cases=...). Validation runs during SUT resolution and raises if any case does not match the resolved callable/method signature.