Skip to content

Instantly share code, notes, and snippets.

@ryancausey
Last active May 12, 2020 23:18
Show Gist options
  • Save ryancausey/904ad5b77349769d32c72d413dd79a59 to your computer and use it in GitHub Desktop.
Save ryancausey/904ad5b77349769d32c72d413dd79a59 to your computer and use it in GitHub Desktop.
Demonstrates pytest_factoryboy issue with SubFactory fixture names
import factory
from pytest_factoryboy import register
class AModel:
"""Simple model that refers to another model"""
def __init__(self, b_model):
self.b_model = b_model
class BModel:
"""The referred to model by A that has a name."""
def __init__(self, name):
self.name = name
class BModelFactory(factory.Factory):
"""Factory that creates B models."""
class Meta:
model = BModel
name = "John""
class AModelFactory(factory.Factory):
"""Factory that creates A models using a SubFactory to set the b_model attribute."""
class Meta:
model = AModel
b_model = factory.SubFactory(BModelBobFactory)
register(BModelFactory, "b_model_with_some_other_name")
register(AModelFactory)
def test_wrong_fixture_name(a_model):
"""This fails at the setup step due to not finding a fixture named "b_model__name".
The issue appears to be that while generating the fixtures for the AModelFactory's
SubFactory(BModelFactory) the `register` code doesn't take into account that the
BModelFactory was registered.
It actually looks like there isn't a clear way to properly introspect whether a
given factory class to be used for a SubFactory has already been registered with
an overridden model_name. I think some extra metadata will be required in order for
subsequent calls to `register` to be able to determine that?
"""
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment