Skip to main content

py-fixture

Documentation Status

pytest style fixtures

Features:

  • fixture decorator to mark function as fixture factory
  • Fixture Scope - unit of work for fixture value evaluation
  • Fixture Context - Fixture Scope is also a context manager
  • Value Cache - Fixture value is evaluated only once per scope
  • Fixture Factory Argument Replacement - Invoke fixture factory with replaced arguments
  • Generator Fixture - Generator function could be used as fixture factory
  • Scope tear down - through .finish() method. Individual fixtures are torn down in reverse order of their evaluation
  • Generator fixture tear down
  • Detect recursive evaluation

Examples

Basic Example

This basic example demonstrates the fixture decorator, fixture scope and value cache.

from pyfixture import fixture, FixtureScope
@fixture
def x():
    print("Evaluate x")
    return 1

scope = FixtureScope()
scope.get_fixture_value('x')
# Evaluate x
# 1
scope.get_fixture_value('x')
# 1

Advanced Example

This example build on the basic example by demostrating additional features:

  • Fixture factory argument replacement
  • Fixture Context - fixture scope using context manager
  • Generator fixture
  • Scope tear down
  • Generator fixture tear down
from pyfixture import fixture, FixtureScope

@fixture
def x():
    print("Evaluate x")
    yield 1
    print("Tear down x")

@fixture
def y():
    print("Evaluate y")
    yield 2
    print("Tear down y")

@fixture
def z(x, y):
    print("Evaluate z")
    yield x + y
    print("Tear down z")

def i_am_not_fixture(a, x, y, z):
    print(f"i_am_not_function: a: {a}, x: {x}, y:{y}, z:{z}")

with FixtureScope() as scope:
    print("Get z for the first time")
    assert scope.get_fixture_value("z") == 3
    print("Get z for the second time")
    assert scope.get_fixture_value("z") == 3
    print("Bind a function")
    binded = scope.bind(i_am_not_fixture, ignore_missing=True)
    binded(200)
    print("Finish scope")
# Get z for the first time
# Evaluate x
# Evaluate y
# Evaluate z
# Get z for the second time
# Bind a function
# i_am_not_function: a:200, x: 1, y:2, z:3
# Finish scope
# Tear down z
# Tear down y
# Tear down x

Fixtures can have other fixtures as parameters. Parameters are evaluated and replaced automatcally:

@fixture
def theta(z):
    print("Theta: {z}")
    return 2 * z

with FixtureScope() as scope:
    print(scope.get_fixture_value("theta"))

Recursive fixture evaluation detected automatically and RecursiveFixtureEvaluation exception is raised:

from pyfixture import fixture, FixtureScope

@fixture
def one(two):
    pass

@fixture
def two(three):
    pass

@fixture
def three(one):
    pass

with FixtureScope() as s:
    s.get_fixture_value("one")

Reallistic Example

from pyfixture import fixture, FixtureScope

@fixture
def existing_user():
    user = User.objects.get_or_create(username="myuser")
    yield user
    user.delete()

@fixture(name="given_existing_order")
def existing_order(existing_user):
    order = Order.objects.get_or_create(id=1, user=existing_user)
    yield order
    order.delete()

scope = FixtureScope()
order = scope.get_fixture_value("existing_order")
user = scope.get_fixture_value("user")
assert order.user is user
# ...

Metadata

Release files for pyfixture 0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfixture 0.7
File Size Uploaded Provenance Actions
pyfixture-0.7.tar.gz 18.4 kB Without provenance Details for pyfixture-0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyfixture 0.7
File Interpreter ABI Platform Actions
pyfixture-0.7-py2.py3-none-any.whl Python 3, Python 2 none any Details for pyfixture-0.7-py2.py3-none-any.whl

Total release size: 38.4 kB

Release files / pyfixture-0.7.tar.gz

Download URL pyfixture-0.7.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bb18c16cebab77e8f97be34fe0861c8ac4f8e3c2fa7bf14d5f2b8544021ecd31
BLAKE2b-256 checksum
How to use checksums
51ead725333fcd2576a79f190da82cbdf1d83ff166599ee2c5224094c34b4be6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.9.6 requests/2.28.2 setuptools/58.1.0 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.10.2

Release files / pyfixture-0.7-py2.py3-none-any.whl

Download URL pyfixture-0.7-py2.py3-none-any.whl
Size 20.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
14ea0a883e2db61d522cdc5e187e1726e84adc3884fba61454b0c91f5ef816ee
BLAKE2b-256 checksum
How to use checksums
9686c1b5e842250b8c49c3df5868a3b7fb5e2251dac6dbedba61381b42d78cc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.9.6 requests/2.28.2 setuptools/58.1.0 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.10.2

Security for pyfixture 0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.7 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page