Code Monkey home page Code Monkey logo

Comments (5)

subhashb avatar subhashb commented on May 30, 2024 13

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.

from pytest-factoryboy.

Matoking avatar Matoking commented on May 30, 2024 1

I decided to ditch pytest-factoryboy and simply write a function that injects the factories as fixtures as-is, per the example in pytest-dev/pytest#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.

from pytest-factoryboy.

MRigal avatar MRigal commented on May 30, 2024

I also do report the same problems with 2.9.2

from pytest-factoryboy.

pawelad avatar pawelad commented on May 30, 2024

I would be happy to help with this but would prefer some feedback from the maintainers regarding their preferred way.

My preference would be to go along with factory_boy changes / deprecations (that is, change postgen_declarations to post_declarations .as_dict(), more or less, which I did locally - it works and is easy) but that would require pytest-factoryboy to specify the minimal factory_boy version, possibly with a new major release and a note in the README about it.

Let me know what you guys think.

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 30, 2024

Please try 2.0.1

from pytest-factoryboy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.