Skip to main content

Allows you to use fixtures in @pytest.mark.parametrize.

Project description

pytest-lazy-fixtures

codecov CI PyPI version

Use your fixtures in @pytest.mark.parametrize.

This project was inspired by pytest-lazy-fixture.

Improvements that have been made in this project:

  1. You can use fixtures in any data structures
  2. You can access the attributes of fixtures
  3. You can use functions in fixtures

Installation

pip install pytest-lazy-fixtures

Usage

To use your fixtures inside @pytest.mark.parametrize you can use lf (lazy_fixture).

import pytest
from pytest_lazy_fixtures import lf

@pytest.fixture()
def one():
    return 1

@pytest.mark.parametrize('arg1,arg2', [('val1', lf('one'))])
def test_func(arg1, arg2):
    assert arg2 == 1

lf can be used with any data structures. For example, in the following example, lf is used in the dictionary:

import pytest
from pytest_lazy_fixtures import lf

@pytest.fixture()
def one():
    return 1

@pytest.mark.parametrize('arg1,arg2', [('val1', {"value": lf('one')})])
def test_func(arg1, arg2):
    assert arg2 == {"value": 1}

You can also specify getting an attribute through a dot:

import pytest
from pytest_lazy_fixtures import lf

class Entity:
    def __init__(self, value):
        self.value = value

@pytest.fixture()
def one():
    return Entity(1)

@pytest.mark.parametrize('arg1,arg2', [('val1', lf('one.value'))])
def test_func(arg1, arg2):
    assert arg2 == 1

And there is some useful wrapper called lfc (lazy_fixture_callable). It can work with any callable and your fixtures, e.g.

import pytest
from pytest_lazy_fixtures import lf, lfc

class Entity:
    def __init__(self, value):
        self.value = value

    def __str__(self) -> str:
        return str(self.value)

    def sum(self, value: int) -> int:
        return self.value + value

@pytest.fixture()
def entity():
    return Entity(1)

@pytest.fixture()
def two():
    return 2

@pytest.fixture()
def three():
    return 3

@pytest.fixture()
def entity_format():
    def _entity_format(entity: Entity):
        return {"value": entity.value}

    return _entity_format

@pytest.mark.parametrize(
    "message",
    [
        lfc(
            "There is two lazy fixture args, {} and {}! And one kwarg {field}! And also simple value {simple}".format,
            lf("entity"),
            lf("two"),
            field=lf("three"),
            simple="value",
        ),
    ],
)
def test_lazy_fixture_callable_with_func(message):
    assert message == "There is two lazy fixture args, 1 and 2! And one kwarg 3! And also simple value value"


@pytest.mark.parametrize("formatted", [lfc("entity_format", lf("entity"))])
def test_lazy_fixture_callable_with_lf(formatted, entity):
    assert formatted == {"value": entity.value}


@pytest.mark.parametrize("result", [lfc("entity.sum", lf("two"))])
def test_lazy_fixture_callable_with_attr_lf(result):
    assert result == 3

Contributing

Contributions are very welcome. Tests can be run with pytest.

License

Distributed under the terms of the MIT license, pytest-lazy-fixtures is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

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_lazy_fixtures-1.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

pytest_lazy_fixtures-1.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_lazy_fixtures-1.1.1.tar.gz.

File metadata

  • Download URL: pytest_lazy_fixtures-1.1.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.5.0-1023-azure

File hashes

Hashes for pytest_lazy_fixtures-1.1.1.tar.gz
Algorithm Hash digest
SHA256 0c561f0d29eea5b55cf29b9264a3241999ffdb74c6b6e8c4ccc0bd2c934d01ed
MD5 d1b9987a15334fcf9bc44083457efec6
BLAKE2b-256 2115a33df3d8d0f44bde7382c9201a06fa277d8442d783ab46874b616aae9239

See more details on using hashes here.

File details

Details for the file pytest_lazy_fixtures-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_lazy_fixtures-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a4b396a361faf56c6305535fd0175ce82902ca7cf668c4d812a25ed2bcde8183
MD5 dce76875f3d28fa9d5cc3a52baaef164
BLAKE2b-256 603a9354c3765bc1459fc18f6d5cf62709b3a606de55bd02d0483ceefd7fd98f

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