Skip to main content

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


Download files

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

Source Distribution

pytest_bonsai-0.0.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

pytest_bonsai-0.0.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

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

Hashes for pytest_bonsai-0.0.1.tar.gz
Algorithm Hash digest
SHA256 bf50eebb95ade88a748e5f369b9f7a24ca77e9d243a54373d13cabda3520c8d8
MD5 d2a731af002094b8a5ab62251b28e787
BLAKE2b-256 5a934e000ffc26b0ba22f33debe5e195e1b3e2b5afda8996730a0df0227dd6e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_bonsai-0.0.1.tar.gz:

Publisher: python-publish.yml on mrzechonek/pytest-bonsai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for pytest_bonsai-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 71936b36656a4623115655b7f9e9d8275e98b5aca1d8326249aa155afa165fdb
MD5 5320c2c3d59b0da19298673455b7edd9
BLAKE2b-256 af8c352b5955e4a66964e45b5f2f54642480fb2cd236963cc6dd477af79639d6

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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