Skip to content

The model fixture does not work with factory.SelfAttribute and factory.SubFactory #263

@ryancausey

Description

@ryancausey

The model fixture doesn't appear to respect when a self attribute is used within a sub factory declaration. This does work as expected when using the factory fixture, however.

pytest-factoryboy version 2.8.1
factory-boy version 3.3.3

Here is an example that demonstrates the problem:

import factory
from pytest_factoryboy import register


class Foo:
    def __init__(self, c):
        self.c = c


class Bar:
    def __init__(self, a, b):
        self.a = a
        self.b = b


class FooFactory(factory.Factory):
    class Meta:
        model = Foo

    c = "blah"


class BarFactory(factory.Factory):
    class Meta:
        model = Bar

    a = "foozgooz"
    b = factory.SubFactory(FooFactory, c=factory.SelfAttribute("..a"))


register(FooFactory)
register(BarFactory)


def test_that_works(bar_factory):
    bar = bar_factory()
    assert bar.a == bar.b.c


def test_that_fails(bar):
    assert bar.a == bar.b.c

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions