question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create two fixtures with shared value from a subfactory?

See original GitHub issue

This project has two factories using a third factory as subfactory – both User and Location have company as a Company subfactory. How can i get both user and location as fixtures with the same company instance?

class CompanyFactory(FlaskSQLAlchemyModelFactory):
    """Create companies and optionally save them to the database."""

    class Meta:  # noqa: D106
        model = Company


class LocationFactory(FlaskSQLAlchemyModelFactory):
    """Create location and optionally save them to the database."""

    class Meta:  # noqa: D106
        model = Location

    label = TEST_LABEL
    company = factory.SubFactory(CompanyFactory)


class UserFactory(FlaskSQLAlchemyModelFactory):
    """Create users and optionally save them to the database."""

    class Meta:  # noqa: D106
        model = User

    company = factory.SubFactory(CompanyFactory)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
youtuxcommented, Oct 28, 2020

UserFactory will use the company fixture when creating a user. That company fixture is registered by register(CompanyFactory).

If you want to use a different company for your user, you have to override the fixture or use an explicit pytest parametrization (pytest.mark.parametrize):

register(CompanyFactory)  # Implicitly registered under the "company" fixture name
register(CompanyFactory, "second_company")

register(LocationFactory)
register(UserFactory)

@pytest.mark.parametrize('user__company', [LazyFixture("second_company"))
def test_foo(user, company, second_company):
    assert user.company is second_company
0reactions
barrapontocommented, Oct 28, 2020

OMG you’re right! I just assumed they weren’t! Subfactories are only called once then, unless I somehow ask it not to?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create two fixtures with shared value from a subfactory ...
This project has two factories using a third factory as subfactory -- both User and Location have company as a Company subfactory. How...
Read more >
Welcome to pytest-factoryboy's documentation! — pytest ...
There are fixtures created for factory attributes. Attribute names are prefixed with the model fixture name and double underscore (similar to the convention ......
Read more >
Populate one to many relationships with Factory Boy & Django
Django. 09 - Populate one to many relationships with Factory Boy & Django - RelatedFactoryList & SubFactory. 742 views 1 year ago.
Read more >
Why does FactoryBoy create a new object from SubFactory ...
It has a setting FACTORY_DJANGO_GET_OR_CREATE that means it won't create a new object if one already exists. But when I ask for an...
Read more >
Factory injection, combining pytest with factory_boy
@pytest.fixture def you(your_father): """You can't be created without your ... so that pytest fixtures are shared among them instead of the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found