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

List declaration raises fixture 'list' not found

Open sanchez-escobar opened this issue 7 years ago • 4 comments

factory_boy List declarations are not working with pytest_factoryboy for me. Below is a simple example that should bring out the issue (and an attachment). Could I please get any feedback on this?

Thanks in advance list_declaration_example.py.zip

import factory
from pytest_factoryboy import register


class ClassA(object):
    def __init__(self):
        self._val = 'test_value'

    @property
    def val(self):
        return self._val


class ClassB(object):
    def __init__(self):
        self._arr = [ClassA()]

    @property
    def arr(self):
        return self._arr


class ClassAFactory(factory.Factory):
    """TestClassA factory."""
    class Meta:
        model = ClassA

    _val = 'test_value'


class ClassBFactory(factory.Factory):
    """TestClassB factory."""
    class Meta:
        model = ClassB

    arr = factory.List([
        factory.SubFactory(ClassAFactory)
    ])

register(ClassAFactory)
register(ClassBFactory)

def test_example_a(class_a):
    """
    Assert that the nested list is properly tested
    """
    assert class_a.val == 'test_value'

def test_example_b(class_b):
    """
    Assert that the nested list is properly tested
    """
    assert len(class_b.arr) == 1

============================= test session starts ==============================
platform darwin -- Python 3.5.2, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/alejandrosanchez/Development/testdir, inifile: setup.cfg
plugins: factoryboy-2.0.1, cov-2.5.1
collected 2 items
list_declaration_example.py .E                                           [100%]
test setup failed
file /Users/alejandrosanchez/Development/testdir/list_declaration_example.py, line 49
  def test_example_b(class_b):
file <string>, line 2: source code not available
file <string>, line 2: source code not available
E       fixture 'list' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, class_a, class_a_factory, class_b, class_b__arr, class_b_factory, cov, doctest_namespace, factoryboy_request, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

<string>:2


==================================== ERRORS ====================================
_______________________ ERROR at setup of test_example_b _______________________
file /Users/alejandrosanchez/Development/testdir/list_declaration_example.py, line 49
  def test_example_b(class_b):
file <string>, line 2: source code not available
file <string>, line 2: source code not available
E       fixture 'list' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, class_a, class_a_factory, class_b, class_b__arr, class_b_factory, cov, doctest_namespace, factoryboy_request, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

<string>:2
====================== 1 passed, 1 error in 0.10 seconds =======================
Process finished with exit code 0

sanchez-escobar avatar Mar 13 '18 18:03 sanchez-escobar

Confirmed, same error when using it in combination with SQLAlchemy.

ttphan avatar Apr 03 '18 08:04 ttphan

Got this too

revmischa avatar May 15 '19 12:05 revmischa

+1

nicoCalvo avatar Jun 08 '19 14:06 nicoCalvo

Anyone have any luck? I'm getting this with custom objects still on latest version

lovetoburnswhen avatar Oct 19 '21 20:10 lovetoburnswhen

2+ years later, no comments. Fantastic!

joriskoris-emburse avatar Aug 16 '23 20:08 joriskoris-emburse

2+ years later, no comments. Fantastic!

Open a PR or request a refund

revmischa avatar Aug 16 '23 20:08 revmischa

nowadays pytest-factoryboy gives this warning:

 Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged by pytest-factoryboy, as it assumes that the model name is 'list' when using it as SubFactory or RelatedFactory, which is too generic and probably not what you want.
   You can giving an explicit name to the model by using:
   model = named_model(list, "Foo")
        factory_class = <class 'factory.base.ListFactory'>
        model_cls  = <class 'list'>
        model_name = 'list'

Try that out, it should probably fix your issue.

youtux avatar Aug 29 '23 19:08 youtux

anyway, this is the most correct way I can think of to fix the initial issue posted by @sanchez-escobar:

import factory
from pytest_factoryboy import register


class ClassA:
    def __init__(self, val):
        self.val = val


class ClassB:
    def __init__(self, arr):
        self.arr = arr


class ClassAFactory(factory.Factory):
    """TestClassA factory."""

    class Meta:
        model = ClassA

    val = "test_value"


class ClassBFactory(factory.Factory):
    """TestClassB factory."""

    class Meta:
        model = ClassB

    class Params:
        arr_0 = factory.SubFactory(ClassAFactory)

    arr = factory.LazyAttribute(lambda o: [o.arr_0])


register(ClassAFactory)
register(ClassBFactory)


def test_example_a(class_a):
    """
    Assert that the nested list is properly tested
    """
    assert class_a.val == "test_value"


def test_example_b(class_b):
    """
    Assert that the nested list is properly tested
    """
    assert len(class_b.arr) == 1

youtux avatar Aug 29 '23 19:08 youtux