Skip to main content

Pytest plugin to load fixtures from YAML files

Project description

pytest-fixture-maker: Load fixture data from local YAML files.

Master

PyPI version PyPI

pytest-fixture-maker is a plugin for pytest, which load fixtures data from local YAML files.

Example file with fixtures:

# fixtures/test_fetch_by_key.yml

--- 

test_fetch_by_key:
  storage: 
    accounts:
      - { key: 1, name: "Visa Classic" }
      - { key: 2, name: "Visa Gold"}

test_success:
  - id: First test case
    account_key: 1
    expected: { key: 1, name: "Visa Classic" }
  - id: Second test case
    account_key: 2
    expected: { key: 2, name: "Visa Gold" }

test_missing:
  - id: Try fetch missing account
    account_key: 3
    expected_exc: AccountNotFound

Example conftest.py file:

# conftest.py

from typing import Any

import pytest

from tests.accounts import Account, AccountRepo, Storage


@pytest.fixture(scope="function")
def account(request) -> Account:
    """Account entity."""
    return Account(**request.param)


@pytest.fixture(scope="function")
def storage(request) -> Storage:
    """Entity storage."""
    storage = Storage()

    if "accounts" in request.param:
        accounts = [Account(**account) for account in request.param["accounts"]]
        storage.accounts = AccountRepo(accounts=accounts)

    return storage


@pytest.fixture(scope="function")
def expected(request) -> Any:
    """Expected test case result."""
    return request.param

Example tests:

from typing import Type

import pytest

from tests.accounts import Account, AccountNotFound, Storage


@pytest.fixture
def account_key(request) -> int:
    """Test account identifier."""
    return request.param


@pytest.fixture
def expected(request) -> Account:
    """Expected test case result."""
    return Account(**request.param)


@pytest.mark.integration
def test_success(storage: Storage, account_key: int, expected: Account) -> None:
    """Successful fetch account by key from storage."""
    result = storage.accounts.fetch_by_key(account_key)

    assert result == expected


@pytest.fixture
def expected_exc(request) -> Type[AccountNotFound]:
    """Expected exception when account not found in storage."""
    return AccountNotFound


@pytest.mark.integration
def test_missing(storage: Storage, account_key: int, expected_exc: Type[AccountNotFound]) -> None:
    """Try to fetch missing account by key from storage."""
    with pytest.raises(expected_exc):
        storage.accounts.fetch_by_key(account_key)

Installation

To install pytest-fixture-maker, do:

(env) $ python3 -m pip install pytest-fixture-maker

Changelog

0.1.0 (2021-09-19)

Initial release.

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-fixture-maker-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

pytest_fixture_maker-0.1.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-fixture-maker-0.1.1.tar.gz.

File metadata

  • Download URL: pytest-fixture-maker-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.9 CPython/3.8.10 Linux/5.8.0-1041-azure

File hashes

Hashes for pytest-fixture-maker-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e0d61d5f42631abd77fbeb5e7208403d0a53dae1c68acd1e0acda5149626c224
MD5 acc9c744c7e4eed2cae873a216bab808
BLAKE2b-256 54dccadce4cf0bdd3684b1756bdeff09550392b0f60ec4031d8be50d032f08fb

See more details on using hashes here.

File details

Details for the file pytest_fixture_maker-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_fixture_maker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5dea848c1aa09f5f7a0bb8b170b1bdff2e7fbb41cb998dfe96e15fff0c7711dc
MD5 125df9054f2123a4559a792f346fd961
BLAKE2b-256 73f9d7ea1411fd6290ed20e8a3b6fb04b04f0d7b755effc46592e0f1059b3251

See more details on using hashes here.

Supported by

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