Skip to main content

Class as PyTest fixtures (and BDD steps)

Project description

pytest-class-fixtures — Object-oriented fixtures for pytest

Turn your class into a fixture

Ever have PyTest ´s @fixture tell you this?

ValueError: class fixtures not supported (maybe in the future)

well, the future is now:

from pytest_class_fixtures import class_fixture

@class_fixture(name="calc")
class Calculator:
    def __init__(self):
        self.result = 0

def test_calculator_initial_state(calc):
    assert calc.result == 0

Do you want BDD with that?

# calculator.feature
Feature: Calculator
    Scenario: Addition
        Given the calculator is cleared
        When I add 5
        Then the result should be 5
import pytest
from pytest_bdd import given, when, then, parsers, scenarios
from pytest_class_fixtures import class_fixture

scenarios("calculator.feature")

@class_fixture(name="calc")
class Calculator:
    def __init__ (self):
        self.clear()

    @given("the calculator is cleared")
    def clear (self):
        self.result = 0

    @when(parsers.parse("I add {number}"))
    def add (self, number):
        self.result += int(number)

@then(parsers.parse("the result should be {result}"))
def check_result(calc, result):
    assert calc.result == int(result)

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_class_fixtures-0.1.0.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

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

pytest_class_fixtures-0.1.0-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_class_fixtures-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_class_fixtures-0.1.0.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.6.0

File hashes

Hashes for pytest_class_fixtures-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f1420666e43a395918e213b3641bff4d03f90feeff2be1c0db40b06e94c773f5
MD5 e46ee2f1adb61b0f955acf08bfdf3a8d
BLAKE2b-256 bab6a8edfcc9f7245eea3c3aa2ca16996deabf5ca5103ceda6ebb760283d11cf

See more details on using hashes here.

File details

Details for the file pytest_class_fixtures-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_class_fixtures-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1cf97b949f75ef497c90a25536dae513618fad259b152a3b9af3f106bebf1508
MD5 982da7d9a08be49c20b484a0adc13e12
BLAKE2b-256 4d283682d323bacb462015d5494c3baf0d221e64a73968ed5fbff203c7286b81

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