Partial support library for structured testing
Project description
speclike
speclike is a pytest helper library designed to define tests in a more structured and expressive way.
It provides a declarative approach for building tests from two complementary perspectives:
- Individual test bodies, written as ordinary methods.
- Externally defined dispatchers and actors, representing scenario-driven or behavior-based tests.
The framework automatically generates executable pytest test functions (test_...) from decorated functions and classes.
🧩 Core Concepts
1. Spec and ExSpec Classes
Spec— the main base class for declarative test specifications.
It manages auto-generated tests and delegates execution throughdispatch()ordispatch_async().ExSpec— groups externally defined dispatchers (functions that control test flow outside of the class).
Both are implemented using metaclasses (_SpecMeta, _ExSpecMeta) that synthesize pytest-compatible test functions during class creation.
2. Case and Ex Decorators
Case— marks individual test bodies or actor functions (def _(...):) within aSpecclass.
It can attach pytest marks, parametrize data, or skip tests dynamically.Ex— marks dispatcher functions used inExSpecor top-level definitions.
Dispatchers define parameter structure usingPRM, and connect to actors via@case.ex(dispatcher).
3. PRM (Parameter Prefix Rules)
Defines how test parameters behave and interact between dispatcher and actor.
| Prefix | Kind | Behavior |
|---|---|---|
_ |
AO (Actor-Only) | Created by dispatcher, passed to actor (not parametrized) |
| (none) | AP (Actor-Parametrized) | Parametrized and passed to actor |
__ |
PO (Param-Only) | Parametrized but not passed to actor (used for assertions) |
Parameter ordering must follow AO → AP → PO.
PRM validates actor signatures, generates pytest parametrization, and bridges runtime values through _ParamsBridge.
4. Dispatcher and Actor
- Dispatcher: a function decorated with
@exthat defines test input combinations usingPRM. - Actor: a function named
_decorated with@case.ex(dispatcher)that performs the actual behavior under test. - The library automatically links each actor to its dispatcher and generates a
test_<dispatcher>method that executes the pair.
5. Test Generation Workflow
- The metaclass scans for decorated functions (
TargetKind). - Each test body or dispatcher/actor pair is converted into a pytest-visible
test_...function. - Signatures, parametrization, and pytest marks are copied to preserve readability and IDE support.
- For external specs (
ExSpec), tests are created dynamically based on defined dispatchers.
6. Highlights
- Strong signature validation for actors against their
PRMdefinitions. - Automatic propagation of
pytest.mark.parametrizeand other pytest marks. - Source location (
co_firstlineno) is preserved for accurate traceback references. - Supports both sync and async test execution paths.
Example
from speclike import Spec, PRM
# Example domain object
class Context:
def compute(self, x: int) -> int:
return x * 10
# Get decorators
case, ex = Spec.get_decorators()
# Dispatcher (external). Parameters are NOT taken as direct function args.
# Access AP/PO values via the bridge `p`, and call the actor via `p.act(...)`.
@ex.follows(
[(1, 10), (2, 20), (3, 30)], # (value, __expected)
ids=["x1", "x2", "x3"]
)
def check(p = PRM(_ctx=Context, value=int, __expected=int)):
ctx = Context() # AO: create here
result = p.act(_ctx=ctx, value=p.value) # call actor with AO/AP
assert result == p.__expected # PO: only used in dispatcher
# Spec class with actor method named "_"
class TestCompute(Spec):
@case.ex(check)
def _(self, _ctx: Context, value: int) -> int:
# Actor receives AO/AP only, in the declared order.
return _ctx.compute(value)
At runtime, this generates:
test_check— a parametrized pytest function executing the dispatcher.act_for_check— an internal bound actor function used by the dispatcher.
Labeling Decorator (Case / Ex)
The library provides a hierarchical labeling mechanism applied through decorators such as:
@case.api.input.default
@case.network.timeout
@case.tmp
Each decorator call selects one label from a navigable hierarchy:
Major → Intermediate → Minor
You may specify 0 to 3 labels, and they can be combined simply by chaining attributes. The resulting labels are gathered and stored inside a single pytest mark:
pytest.mark.speclike("api", "input", "default")
The decorator ensures this mark is always attached cleanly to the function’s pytestmark.
How the Decorator Behaves
When you write:
@case.api.input.default
def check_something(): ...
the decorator:
- Collects the labels
"api","input","default" - Normalizes the target’s
pytestmarkinto a list - Appends a
speclike(...)mark containing those labels
The mark is purely declarative: each decorated function carries structured metadata describing its classification.
Current Status of the speclike Marker
A pytest marker named speclike is already defined,
but no runtime implementation, filtering logic, or pytest plugin behavior exists yet.
At this stage:
- the marker is attached correctly
- pytest recognizes it as a registered mark
- but it performs no special logic
- filtering such as
-m "speclike"is possible, yet argument-based filtering (speclike("api")) is not implemented until the plugin is created
Implementation is planned for future development.
Why This Classification Helps
Even without a full plugin, the classification system already provides a strong structural benefit:
- labels consistently encode feature areas, scenarios, or test intent
- larger test suites become easier to navigate and reason about
- test naming and grouping become more predictable
- future tooling or plugins can rely on the embedded structure
Once the speclike implementation is added, these labels will support richer filtering, reporting,
and domain-specific test behaviors—while keeping the decorator syntax compact and expressive.
Installation
pip
pip install speclike
github
pip install git+https://github.com/minoru-jp/speclike.git
Status
This project is in very early development (alpha stage).
APIs and behavior may change without notice.
License
MIT License © 2025 minoru_jp
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file speclike-0.0.0.30.tar.gz.
File metadata
- Download URL: speclike-0.0.0.30.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f32f51e50202330c20a88eb2db22ca6f0d24d215c8e681e68e53d6034de1678
|
|
| MD5 |
2a6e191975e2c61021522059449ec441
|
|
| BLAKE2b-256 |
ff926f7c2b5d774e237cc7d82de56cb8ddc31031bd325a98428eced143bc9acc
|
File details
Details for the file speclike-0.0.0.30-py3-none-any.whl.
File metadata
- Download URL: speclike-0.0.0.30-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00836ce229c55131595dc2443ca4a422dbbc3b674ac40abc90246d2328495897
|
|
| MD5 |
0091f769b4c0c0275d103faa1fff6d7d
|
|
| BLAKE2b-256 |
2fdff863c80bd5162d6464e76de122db68ccb3c0769eb0549b7ae15c8a05ec26
|