Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

speclike-0.0.0.25.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

speclike-0.0.0.25-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file speclike-0.0.0.25.tar.gz.

File metadata

  • Download URL: speclike-0.0.0.25.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for speclike-0.0.0.25.tar.gz
Algorithm Hash digest
SHA256 856473ecab12198c6f64b504ce6c571bf88e348ea324ae3602b302d02c3f6914
MD5 eeb28038886655fef4ec0ebdac1026d6
BLAKE2b-256 044456922245084de3cd7747eb5c530be3f6c14294fc83f5fb4a87168c31c223

See more details on using hashes here.

File details

Details for the file speclike-0.0.0.25-py3-none-any.whl.

File metadata

  • Download URL: speclike-0.0.0.25-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for speclike-0.0.0.25-py3-none-any.whl
Algorithm Hash digest
SHA256 3060333aef8fff1805ede48ff4e5e18fa62a127c9df41b23e380549fe079bd2a
MD5 c9903c1d914b5c2570f4f26c6fbe34d5
BLAKE2b-256 645d36a1ae955d8d5917e09604d0d0ebbbd6d2f56a10494418dbcd65c30dc797

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page