Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple model_bakery.baker wrapper to serve as a Pytest fixture #511

Open
paduszyk opened this issue Dec 23, 2024 · 0 comments
Open

A simple model_bakery.baker wrapper to serve as a Pytest fixture #511

paduszyk opened this issue Dec 23, 2024 · 0 comments

Comments

@paduszyk
Copy link

This is a discussion/question kind of issue.

I have recently worked with a test suite (run with Pytest) in which basically each module uses the import:

from model_bakery import baker

I realised that baker module is actually a good (?) candidate for a fixture, so I wrote a simple wrapper like:

import pytest
from model_bakery import baker as _baker


@pytest.fixture
def baker():
    return _baker

If it's put in the root conftest.py, then I don't to need the import, but can use baker a regular fixture. For example:

@pytest.mark.django_db
def test_domain_str(baker):  # <- in place of the model_bakery.baker import
    # Arrange.
    domain = baker.make(
        "evaluation.Domain",
        name="natural sciences",
    )

    # Act.
    domain_str = str(domain)

    # Assert.
    assert domain_str == "natural sciences"

What would you say to extend the model_bakery (or develop a separate project for a plugin) by such a fixture?

Alternatively, one could develop this idea as a separate plugin for Pytest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant