pytest-fixture-classes


Namepytest-fixture-classes JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://github.com/Ovsyanka83/pytest-fixture-classes
SummaryFixtures as classes that work well with dependency injection, autocompletetion, type checkers, and language servers
upload_time2023-09-02 17:06:17
maintainer
docs_urlNone
authorStanislav Zmiev
requires_python>=3.7
licenseMIT
keywords pytest py.test type hints annotations fixtures factory fixtures
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pytest Fixture Classes

Typed [factory fixtures](https://docs.pytest.org/en/6.2.x/fixture.html#factories-as-fixtures) that work well with dependency injection, autocompletetion, type checkers, and language servers.

No mypy plugins required!

## Installation

`pip install pytest-fixture-classes`

## Usage

### Quickstart

Here's a factory fixture from [pytest's documentation](https://docs.pytest.org/en/6.2.x/fixture.html#factories-as-fixtures):

```python
from typing import Any
import pytest


@pytest.fixture()
def orders() -> list[str]:
    return ["order1", "order2"]


@pytest.fixture
def make_customer_record(orders: list[str]) -> Callable[str, dict[str, Any]]:
    def _make_customer_record(name):
        return {"name": name, "orders": orders}

    return _make_customer_record


def test_customer_records(make_customer_record: Callable[str, dict[str, Any]]):
    customer_1 = make_customer_record("Lisa")
    customer_2 = make_customer_record("Mike")
    customer_3 = make_customer_record("Meredith")
```

And here's the same factory implemented using fixture classes:

```python
from pytest_fixture_classes import fixture_class
from typing import Any
import pytest


@pytest.fixture()
def orders():
    return ["order1", "order2"]


@fixture_class(name="make_customer_record")
class MakeCustomerRecord:
    orders: list[str]

    def __call__(self, name: str) -> dict[str, Any]:
        return return {"name": name, "orders": orders}


def test_customer_records(make_customer_record: MakeCustomerRecord):
    customer_1 = make_customer_record("Lisa")
    customer_2 = make_customer_record("Mike")
    customer_3 = make_customer_record("Meredith")
```

You can, of course, add any methods you like into the class but I prefer to keep it a simple callable.

### Rationale

If we want factory fixtures that automatically make use of pytest's dependency injection, we are essentially giving up any IDE/typechecker/language server support because such fixtures cannot be properly typehinted because they are returning a callable, not a value. And python is still pretty new to typehinting callables.

So we can't use ctrl + click, we don't get any autocompletion, and mypy/pyright won't warn us when we are using the factory incorrectly. Additionally, any changes to the factory's interface will require us to search for its usages by hand and fix every single one.

Fixture classes solve all of the problems I mentioned:

* Autocompletion out of the box
* Return type of the fixture will automatically be inferred by pyright/mypy
* When the interface of the fixture changes or when you use it incorrectly, your type checker will warn you
* Search all references and show definition (ctrl + click) also works out of the box

### Usage scenario

Let's say that we have a few pre-existing fixtures: `db_connection`, `http_session`, and `current_user`. Now we would like to write a new fixture that can create arbitrary users based on `name`, `age`, and `height` arguments. We want our new fixture, `create_user`, to automatically get our old fixtures using dependency injection. Let's see what such a fixture will look like:

```python
import pytest
import requests

@pytest.fixture
def db_connection() -> dict[str, str]:
    ...

@pytest.fixture
def http_session() -> requests.Session:
    ...


@pytest.fixture
def current_user() -> requests.Session:
    ...


@pytest.fixture
async def create_user(
    db_connection: dict[str, str],
    http_session: requests.Session,
    current_user: requests.Session,
) -> Callable[[str, int, int], dict[str, str | int | bool]]:
    async def inner(name: str, age: int, height: int):
        user = {...}
        self.db_connection.execute(...)
        if self.current_user[...] is not None:
            self.http_session.post(...)
        
        return user

    return inner

def test_my_code(create_user: Callable[[str, int str], dict[str, str | int | bool]]):
    johny = create_user("Johny", 27, 183)
    michael = create_user("Michael", 43, 165)
    loretta = create_user("Loretta", 31, 172)

    # Some testing code below
    ...

```

See how ugly and vague the typehints for create_user are? Also, see how we duplicate the return type and argument information? Additionally, if you had thousands of tests and if `test_my_code` with `create_user` were in different files, you would have to use plaintext search to find the definition of the fixture if you wanted to see how to use it. Not too nice.

Now let's rewrite this code to solve all of the problems I mentioned:

```python
from pytest_fixture_classes import fixture_class
from collections.abc import Mapping
import requests
import pytest


@pytest.fixture
def db_connection() -> dict[str, str]:
    ...


@pytest.fixture
def http_session() -> requests.Session:
    ...


@pytest.fixture
def current_user() -> Mapping[str, str | int | bool]:
    ...


@fixture_class(name="create_user")
class CreateUser:
    db_connection: Mapping[str, str]
    http_session: requests.Session
    current_user: Mapping[str, str | int | bool]

    def __call__(self, name: str, age: int, height: int) -> dict[str, str | int | bool]:
        user = {...}
        self.db_connection.execute(...)
        if self.current_user[...] is not None:
            self.http_session.post(...)
        
        return user


def test_my_code(create_user: CreateUser):
    johny = create_user("Johny", 27, 183)
    michael = create_user("Michael", 43, 165)
    loretta = create_user("Loretta", 31, 172)

    # Some testing code below
    ...
```

## Implementation details

* The fixture_class decorator turns your class into a frozen dataclass with slots so you won't be able to add new attributes to it after definiton. You can, however, define any methods you like except `__init__`.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Ovsyanka83/pytest-fixture-classes",
    "name": "pytest-fixture-classes",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "pytest,py.test,type hints,annotations,fixtures,factory fixtures",
    "author": "Stanislav Zmiev",
    "author_email": "zmievsa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/64/64/b73a75449af548c3cd4ae3fc04b05aead53444f95ef0fef0c7c77cad3198/pytest_fixture_classes-1.0.3.tar.gz",
    "platform": null,
    "description": "# Pytest Fixture Classes\n\nTyped [factory fixtures](https://docs.pytest.org/en/6.2.x/fixture.html#factories-as-fixtures) that work well with dependency injection, autocompletetion, type checkers, and language servers.\n\nNo mypy plugins required!\n\n## Installation\n\n`pip install pytest-fixture-classes`\n\n## Usage\n\n### Quickstart\n\nHere's a factory fixture from [pytest's documentation](https://docs.pytest.org/en/6.2.x/fixture.html#factories-as-fixtures):\n\n```python\nfrom typing import Any\nimport pytest\n\n\n@pytest.fixture()\ndef orders() -> list[str]:\n    return [\"order1\", \"order2\"]\n\n\n@pytest.fixture\ndef make_customer_record(orders: list[str]) -> Callable[str, dict[str, Any]]:\n    def _make_customer_record(name):\n        return {\"name\": name, \"orders\": orders}\n\n    return _make_customer_record\n\n\ndef test_customer_records(make_customer_record: Callable[str, dict[str, Any]]):\n    customer_1 = make_customer_record(\"Lisa\")\n    customer_2 = make_customer_record(\"Mike\")\n    customer_3 = make_customer_record(\"Meredith\")\n```\n\nAnd here's the same factory implemented using fixture classes:\n\n```python\nfrom pytest_fixture_classes import fixture_class\nfrom typing import Any\nimport pytest\n\n\n@pytest.fixture()\ndef orders():\n    return [\"order1\", \"order2\"]\n\n\n@fixture_class(name=\"make_customer_record\")\nclass MakeCustomerRecord:\n    orders: list[str]\n\n    def __call__(self, name: str) -> dict[str, Any]:\n        return return {\"name\": name, \"orders\": orders}\n\n\ndef test_customer_records(make_customer_record: MakeCustomerRecord):\n    customer_1 = make_customer_record(\"Lisa\")\n    customer_2 = make_customer_record(\"Mike\")\n    customer_3 = make_customer_record(\"Meredith\")\n```\n\nYou can, of course, add any methods you like into the class but I prefer to keep it a simple callable.\n\n### Rationale\n\nIf we want factory fixtures that automatically make use of pytest's dependency injection, we are essentially giving up any IDE/typechecker/language server support because such fixtures cannot be properly typehinted because they are returning a callable, not a value. And python is still pretty new to typehinting callables.\n\nSo we can't use ctrl + click, we don't get any autocompletion, and mypy/pyright won't warn us when we are using the factory incorrectly. Additionally, any changes to the factory's interface will require us to search for its usages by hand and fix every single one.\n\nFixture classes solve all of the problems I mentioned:\n\n* Autocompletion out of the box\n* Return type of the fixture will automatically be inferred by pyright/mypy\n* When the interface of the fixture changes or when you use it incorrectly, your type checker will warn you\n* Search all references and show definition (ctrl + click) also works out of the box\n\n### Usage scenario\n\nLet's say that we have a few pre-existing fixtures: `db_connection`, `http_session`, and `current_user`. Now we would like to write a new fixture that can create arbitrary users based on `name`, `age`, and `height` arguments. We want our new fixture, `create_user`, to automatically get our old fixtures using dependency injection. Let's see what such a fixture will look like:\n\n```python\nimport pytest\nimport requests\n\n@pytest.fixture\ndef db_connection() -> dict[str, str]:\n    ...\n\n@pytest.fixture\ndef http_session() -> requests.Session:\n    ...\n\n\n@pytest.fixture\ndef current_user() -> requests.Session:\n    ...\n\n\n@pytest.fixture\nasync def create_user(\n    db_connection: dict[str, str],\n    http_session: requests.Session,\n    current_user: requests.Session,\n) -> Callable[[str, int, int], dict[str, str | int | bool]]:\n    async def inner(name: str, age: int, height: int):\n        user = {...}\n        self.db_connection.execute(...)\n        if self.current_user[...] is not None:\n            self.http_session.post(...)\n        \n        return user\n\n    return inner\n\ndef test_my_code(create_user: Callable[[str, int str], dict[str, str | int | bool]]):\n    johny = create_user(\"Johny\", 27, 183)\n    michael = create_user(\"Michael\", 43, 165)\n    loretta = create_user(\"Loretta\", 31, 172)\n\n    # Some testing code below\n    ...\n\n```\n\nSee how ugly and vague the typehints for create_user are? Also, see how we duplicate the return type and argument information? Additionally, if you had thousands of tests and if `test_my_code` with `create_user` were in different files, you would have to use plaintext search to find the definition of the fixture if you wanted to see how to use it. Not too nice.\n\nNow let's rewrite this code to solve all of the problems I mentioned:\n\n```python\nfrom pytest_fixture_classes import fixture_class\nfrom collections.abc import Mapping\nimport requests\nimport pytest\n\n\n@pytest.fixture\ndef db_connection() -> dict[str, str]:\n    ...\n\n\n@pytest.fixture\ndef http_session() -> requests.Session:\n    ...\n\n\n@pytest.fixture\ndef current_user() -> Mapping[str, str | int | bool]:\n    ...\n\n\n@fixture_class(name=\"create_user\")\nclass CreateUser:\n    db_connection: Mapping[str, str]\n    http_session: requests.Session\n    current_user: Mapping[str, str | int | bool]\n\n    def __call__(self, name: str, age: int, height: int) -> dict[str, str | int | bool]:\n        user = {...}\n        self.db_connection.execute(...)\n        if self.current_user[...] is not None:\n            self.http_session.post(...)\n        \n        return user\n\n\ndef test_my_code(create_user: CreateUser):\n    johny = create_user(\"Johny\", 27, 183)\n    michael = create_user(\"Michael\", 43, 165)\n    loretta = create_user(\"Loretta\", 31, 172)\n\n    # Some testing code below\n    ...\n```\n\n## Implementation details\n\n* The fixture_class decorator turns your class into a frozen dataclass with slots so you won't be able to add new attributes to it after definiton. You can, however, define any methods you like except `__init__`.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Fixtures as classes that work well with dependency injection, autocompletetion, type checkers, and language servers",
    "version": "1.0.3",
    "project_urls": {
        "Documentation": "https://github.com/Ovsyanka83/pytest-fixture-classes",
        "Homepage": "https://github.com/Ovsyanka83/pytest-fixture-classes",
        "Repository": "https://github.com/Ovsyanka83/pytest-fixture-classes"
    },
    "split_keywords": [
        "pytest",
        "py.test",
        "type hints",
        "annotations",
        "fixtures",
        "factory fixtures"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "17058b2e512f68ac726d19ee9d75c2f786fdb9ac62651ac89e114893704e5bbf",
                "md5": "334893026ba0ae5892d73989f1f34459",
                "sha256": "0b1b3f0d2ccaa6fb620cbd9bf1b228979c5a1590627e6d0961fb73d29629221a"
            },
            "downloads": -1,
            "filename": "pytest_fixture_classes-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "334893026ba0ae5892d73989f1f34459",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 5484,
            "upload_time": "2023-09-02T17:06:15",
            "upload_time_iso_8601": "2023-09-02T17:06:15.844290Z",
            "url": "https://files.pythonhosted.org/packages/17/05/8b2e512f68ac726d19ee9d75c2f786fdb9ac62651ac89e114893704e5bbf/pytest_fixture_classes-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6464b73a75449af548c3cd4ae3fc04b05aead53444f95ef0fef0c7c77cad3198",
                "md5": "253e14dcdae159906201be5c1fdd0623",
                "sha256": "5aee41a63dbd2d6317ce5a5587a9cfc5a0f825589cb0e0c456b1c3c6d8edfa5a"
            },
            "downloads": -1,
            "filename": "pytest_fixture_classes-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "253e14dcdae159906201be5c1fdd0623",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 5542,
            "upload_time": "2023-09-02T17:06:17",
            "upload_time_iso_8601": "2023-09-02T17:06:17.800045Z",
            "url": "https://files.pythonhosted.org/packages/64/64/b73a75449af548c3cd4ae3fc04b05aead53444f95ef0fef0c7c77cad3198/pytest_fixture_classes-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-02 17:06:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Ovsyanka83",
    "github_project": "pytest-fixture-classes",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pytest-fixture-classes"
}
        
Elapsed time: 0.22359s