Partial support library for structured testing
Project description
speclike
A helper library for pytest designed to make test code clearer and more structured.
The following is reproduced from __init__.py.
"""speclike
A helper library for pytest that aims to make test code clearer, more structured,
and easier to reuse.
It divides tests into two conceptual types:
- Externally defined tests — intended for **reusing test logic** across multiple cases
- Individual tests — written directly as single, self-contained test methods
Externally defined tests are composed of **two function definitions** that together form a single test:
Dispatcher:
Defines the *Arrange* and *Assert* phases of the AAA testing pattern.
Acts as a reusable test logic template.
Declares its expected *Act* function (the behavior under test)
using a `Sig` object as the default value of one parameter.
Actor:
Defines the *Act* phase.
It is invoked from within the dispatcher and contains the code that exercises the target behavior.
The actor explicitly specifies which dispatcher it belongs to by using `@case.ex(...)`.
The placement of these functions is as follows:
Dispatcher:
- Defined either at the module level or inside a class inheriting from `ExSpec`.
- Must have one parameter whose default value is a `Sig(...)` object.
The keys of `Sig` specify the argument names and their expected types.
Actor:
- Defined inside a class inheriting from `Spec`.
- Decorated with `@case.ex(dispatcher)` to bind it to a dispatcher.
- The method name must be `"_"` (underscore).
The generated test name is automatically derived from the dispatcher name.
Individual tests:
- Defined inside a class inheriting from `Spec`.
- They behave as normal pytest-compatible test methods.
All of these definitions use decorators provided by the `_Case` and `_Ex` classes,
which handle labeling and parametrization.
---
### Signature Definition via `Sig`
`Sig` defines the expected signature of the actor function.
Each keyword argument represents a parameter name and its type.
Example:
```python
from speclike import ExSpec, Spec, Sig
case, ex = Spec.get_decorators()
# Dispatcher definition
@ex.edge_pass.follows(-1, 0, 1)
def check_near_zero(act = Sig(value=int)):
act(value) # expect success, no exception
```
This declares that the corresponding actor must have a method signature:
`def _(self, value: int): ...`
If the actor’s parameters differ from those declared by `Sig`,
a descriptive error message is automatically generated during test collection.
---
### Actor Definition
The actor provides the *Act* behavior and is bound to a dispatcher via `@case.ex`.
Example:
```python
class SpecCheckNearZero(Spec):
@case.ex(check_near_zero)
def _(self, value: int):
target_func(value)
```
This actor is automatically paired with the dispatcher `check_near_zero`
and generates a pytest-compatible test function named `test_check_near_zero`.
---
### Labeling and Parametrization
Both `_Case` and `_Ex` decorators provide convenient labeling and parametrization helpers:
- Labels such as `@ex.edge`, `@case.feature`, `@case.error`, etc.
- Parametrization through `.follows()`, which generates
`pytest.mark.parametrize` based on the function’s parameters.
Example:
```python
@ex.feature.follows(-10, 0, 10)
def within_bounds(act = Sig(value=int)):
act(value)
```
---
### Notes
- `@case.ex(...)` must be used on methods named `"_"`.
- Automatic test generation currently applies only to classes inheriting from `Spec`.
- The API is still under development and may change, though most semantics are now stable.
"""
from speclike.speclike import Spec, ExSpec, Sig
__all__ = [
"Spec", "ExSpec", "Sig"
]
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.26.tar.gz.
File metadata
- Download URL: speclike-0.0.0.26.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef45bd205d4f2eb875de2d4c8b8c1c2cbd5f25cfca2a3737b5558b1d91d6ca39
|
|
| MD5 |
62e5cac327921eb973377a0c823506d7
|
|
| BLAKE2b-256 |
bc7226ac061d0d2f3f8218aa4abe3a59d4e04ed1e199d1b4fa37e5feb5efa859
|
File details
Details for the file speclike-0.0.0.26-py3-none-any.whl.
File metadata
- Download URL: speclike-0.0.0.26-py3-none-any.whl
- Upload date:
- Size: 12.8 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 |
524dfdd4882250d19b6c5aa3532355a1c60b97184416be4c4f09f966b9afff8c
|
|
| MD5 |
067b4b06d31acf9ce9d7320cfb84b838
|
|
| BLAKE2b-256 |
f5f1dcb28675a5f38f941c4ab7fa4589471d0a14945a80961ce7c91078927807
|