How to create two fixtures with shared value from a subfactory?
See original GitHub issueThis 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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
UserFactory will use the
company
fixture when creating a user. Thatcompany
fixture is registered byregister(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):
OMG you’re right! I just assumed they weren’t! Subfactories are only called once then, unless I somehow ask it not to?