pytest-dev / pytest-dev/pytest-factoryboy

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

Open
#263 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
400
Forks
44
Avg merge
6h 34m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing test_that_fails from the issue with pytest-factoryboy 2.8.1 and factory-boy 3.3.3, then trace the model fixture's handling of the registered BarFactory and its nested SelfAttribute. Done means the model fixture produces a Bar whose a value matches bar.b.c, while test_that_works and the existing test suite still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.