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.

Errors with factory_boy master: 'FactoryOptions' object has no attribute 'postgen_declarations'

See original GitHub issue

There are errors with factory_boy master:

================================================ ERRORS ================================================
_______________________________ ERROR collecting tests/test_circular.py ________________________________
tests/test_circular.py:50: in <module>
    register(AuthorFactory)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
___________________________ ERROR collecting tests/test_factory_fixtures.py ____________________________
tests/test_factory_fixtures.py:56: in <module>
    class AuthorFactory(factory.Factory):
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
_____________________________ ERROR collecting tests/test_lazy_fixture.py ______________________________
tests/test_lazy_fixture.py:29: in <module>
    register(UserFactory)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
_________________________ ERROR collecting tests/test_postgen_dependencies.py __________________________
tests/test_postgen_dependencies.py:23: in <module>
    class FooFactory(factory.Factory):
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 4 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================= 4 error in 0.30 seconds ========================================
ERROR: InvocationError: '…/Vcs/pytest-factoryboy/.tox/py36-pytest3/bin/py.test --junitxml=…/Vcs/pytest-factoryboy/.tox/py36-pytest3/log/junit-py36-pytest3.xml pytest_factoryboy tests'
_______________________________________________ summary ________________________________________________
ERROR:   py36-pytest3: commands failed

Bisecting brings up the following commits (2 are skipped, since they cause a different error):

https://github.com/FactoryBoy/factory_boy/commit/c079d87ddb8e0eeabc66762d7f8f5bb81a143ff0 https://github.com/FactoryBoy/factory_boy/commit/e44766245e70fa102a7e1f85d3bd66c3646d271e https://github.com/FactoryBoy/factory_boy/commit/6f202077a5c8156fe96f8a028f883c14962f5b95

Likely related to the refactoring mentioned in https://github.com/pytest-dev/pytest-factoryboy/issues/23#issuecomment-316717930.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
subhashbcommented, Jul 31, 2017

Confirming that this has something to do with 2.9.0 (master) changes alone. I switched to FactoryBoy 2.8.1 and was able to avoid this issue for now.

1reaction
Matokingcommented, Dec 26, 2017

I decided to ditch pytest-factoryboy and simply write a function that injects the factories as fixtures as-is, per the example in https://github.com/pytest-dev/pytest/issues/2424

So, if your current conftest.py looks like this:

import factory
from pytest_factoryboy import register


class UserFactory(factory.DjangoModelFactory):
    class Meta:
        model = User

    username = factory.Sequence(lambda n: "TestUser%d" % n)


register(UserFactory)

try the following instead:

import factory
import pytest


class UserFactory(factory.DjangoModelFactory):
    class Meta:
        model = User

    username = factory.Sequence(lambda n: "TestUser%d" % n)


def register(factory_cls):
    def generate_fixture(cls):
        @pytest.fixture(scope="module")
        def created_fixture():
            return cls
        return created_fixture

    def to_snakecase(text):
        """
        Converts text from CamelCase to snake_case
        Source: https://stackoverflow.com/a/1176023/8981236
        """
        s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text)
        return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

    # Inject the fixture with snakecase name
    name = to_snakecase(factory_cls.__name__)

    globals()[name] = generate_fixture(factory_cls)


register(UserFactory)

My current test suite works with it without modifications, and I no longer need pytest-factoryboy as the dependency. Your mileage may vary, so this may not be enough for your needs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

factory_boy has no attribute 'objects' - django - Stack Overflow
This error basically occuring if your model class is not inherited from the django models.Model. So "objects" is not available.
Read more >
Reference — Factory Boy stable documentation
This attribute indicates that the Factory subclass should not be used to generate objects, but instead provides some extra defaults.
Read more >
Using factory_boy with ORMs - Read the Docs
When using RelatedFactory or PostGeneration attributes, the base object will be saved once all post-generation hooks have run. class factory.django.
Read more >
factory.django — Factory Boy rbarrois-guide documentation
__name__)) try: manager = model_class.objects except AttributeError: # When inheriting from an abstract model with a custom # manager, the class has no...
Read more >
factory.declarations — Factory Boy stable documentation
Resolver): The object holding currently computed attributes step: a factory.builder. ... Raises: AttributeError: if obj has no 'name' attribute.
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