pytest-factoryboy icon indicating copy to clipboard operation
pytest-factoryboy copied to clipboard

`Trait`s not being considered by pytest_factoryboy

Open youtux opened this issue 1 year ago • 3 comments

Given a factory with traits, pytest_factoryboy should treat them as normal params, and allow them to be parametrized.

MCVE:

@dataclass
class Article:
    comments: int
    views: int

@register
class ArticleFactory(Factory):
    class Meta:
        model = Article

    class Params:
        boring = Trait(
            comments=0,
            views=0,
        )
    comments = 10
    views = 1000

@pytest.mark.parametrize("article__boring", [True])
def test_boring_article(article):
    assert article.comments == 0
    assert article.views == 0

youtux avatar May 10 '24 09:05 youtux

Same here. Is there any workaround this ?

an0o0nym avatar Sep 19 '24 15:09 an0o0nym

I guess the only workaround for now is to use custom fixtures.So for given MCVE would be something like:

@pytest.fixture()
def boring_article():
  return ArticleFactory(boring=True)

an0o0nym avatar Sep 19 '24 15:09 an0o0nym

A Trait can be enabled/disabled by a Factory subclass:

class OriginalFactory(Factory):
    class Params:
        my_trait = factory.Trait(
            some_parameter=3
        )
        
class WithTraitFactory(OriginalFactory):
    my_trait = True

You can register then such a factory and give a different name for the "out of box" entity it creates.

register(WithTraitFactory, "fixture_with_trait")

ddanecki avatar Jan 10 '25 10:01 ddanecki