No project description provided
Project description
pytest bonsai
pytest-bonsai is a plugin that brings elegant, declarative, and composable test data to your test suite.
pytest-bonsai helps you grow minimal, yet expressive dependency trees using Python dataclasses, fixtures, and dynamic parameter resolution.
Installation
$ pip install pytest-bonsai
Basics: Dataclass Fixtures
Bonsai is built around indirect parametrization, but instead of using parametrization to multiply the number of test, it uses it to customize test setup.
A trivial example
from dataclasses import dataclass
from pytest_bonsai import parametrized_fixture, FixtureRequest
@dataclass
class User:
name: str
@dataclass
class UserParam:
name: str = "Alice"
@parametrized_fixture(User)
def user(request: FixtureRequest[UserParam]) -> User:
return User(
name=request.param.name,
)
def test_alice(user: User):
assert user.name == "Alice"
@user.parametrize(name="Bob")
def test_bob(user: User):
assert user.name == "Bob"
A more interesting case
The UserParam dataclass can use other fixtures to set its default values:
from dataclasses import dataclass
import pytest
from pytest_bonsai import parametrized_fixture, FixtureRequest
@dataclass
class User:
name: str
@pytest.fixture
def username():
return "Charlie"
@dataclass
class UserParam:
name: str = field(default_factory=username)
@parametrized_fixture(User)
def user(request: FixtureRequest[UserParam]) -> User:
return User(
name=request.param.name,
)
def test_charlie(user):
assert user.name == "Charlie"
@pytest.mark.parametrize("username", ["Derek"], indirect=True)
def test_derek(user):
assert user.name == "Derek"
Always-indirect fixtures
Calling @parametrized_fixture without any arguments makes the fixture always
indirect, and return the parameter:
from pytest_bonsai import parametrized_fixture
@parametrized_fixture
def email(request): ...
@email.parametrize('edward@af.mil')
def test_edward(email):
assert email == 'edward@af.mi')
While by itself it's not very useful, it makes it easier to define extension
points like username from the previous example and email in the next one.
Even more interesting: lambdas that take fixtures
from dataclasses import dataclass, field
import pytest
from pytest_bonsai import parametrized_fixture, FixtureRequest
@dataclass
class User:
name: str
@parametrized_fixture
def email(request): ...
@dataclass
class UserParam:
name: str = field(default_factory=username)
@parametrized_fixture(User)
def user(request: FixtureRequest[UserParam]) -> User:
return User(
name=request.param.name,
)
def test_charlie(user):
assert user.name == "Charlie"
@user.parametrize(name=lambda email: email.split('@')[0].title())
@email.parametrize('edward@af.mil')
def test_edward(user):
assert user.name == "Edward"
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 pytest_bonsai-0.0.1.tar.gz.
File metadata
- Download URL: pytest_bonsai-0.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf50eebb95ade88a748e5f369b9f7a24ca77e9d243a54373d13cabda3520c8d8
|
|
| MD5 |
d2a731af002094b8a5ab62251b28e787
|
|
| BLAKE2b-256 |
5a934e000ffc26b0ba22f33debe5e195e1b3e2b5afda8996730a0df0227dd6e6
|
Provenance
The following attestation bundles were made for pytest_bonsai-0.0.1.tar.gz:
Publisher:
python-publish.yml on mrzechonek/pytest-bonsai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_bonsai-0.0.1.tar.gz -
Subject digest:
bf50eebb95ade88a748e5f369b9f7a24ca77e9d243a54373d13cabda3520c8d8 - Sigstore transparency entry: 193377947
- Sigstore integration time:
-
Permalink:
mrzechonek/pytest-bonsai@b21636f5a86f41996e3cd9fafd6baad333b31452 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/mrzechonek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b21636f5a86f41996e3cd9fafd6baad333b31452 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_bonsai-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pytest_bonsai-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71936b36656a4623115655b7f9e9d8275e98b5aca1d8326249aa155afa165fdb
|
|
| MD5 |
5320c2c3d59b0da19298673455b7edd9
|
|
| BLAKE2b-256 |
af8c352b5955e4a66964e45b5f2f54642480fb2cd236963cc6dd477af79639d6
|
Provenance
The following attestation bundles were made for pytest_bonsai-0.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on mrzechonek/pytest-bonsai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_bonsai-0.0.1-py3-none-any.whl -
Subject digest:
71936b36656a4623115655b7f9e9d8275e98b5aca1d8326249aa155afa165fdb - Sigstore transparency entry: 193377951
- Sigstore integration time:
-
Permalink:
mrzechonek/pytest-bonsai@b21636f5a86f41996e3cd9fafd6baad333b31452 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/mrzechonek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b21636f5a86f41996e3cd9fafd6baad333b31452 -
Trigger Event:
release
-
Statement type: