Code Monkey home page Code Monkey logo

Comments (6)

olegpidsadnyi avatar olegpidsadnyi commented on May 30, 2024

@nguyening did you register ChildFactory?

from pytest-factoryboy.

nguyening avatar nguyening commented on May 30, 2024

@olegpidsadnyi - how should I register the child factory? I've tried the following as well:

$ diff -C100 test.py test_with_child.py 
*** test.py	2017-07-06 12:46:13.000000000 -0400
--- test_with_child.py	2017-07-06 12:46:07.000000000 -0400
***************
*** 1,23 ****
--- 1,24 ----
  import factory
  import pytest_factoryboy
  
  
  class ChildFactory(factory.Factory):
      class Meta:
          model = dict
  
      child_attr = 'default'
  
  
  class ParentFactory(factory.Factory):
      class Meta:
          model = dict
  
      child = factory.SubFactory(ChildFactory)
  
  
  pytest_factoryboy.register(ParentFactory, 'parent')
+ pytest_factoryboy.register(ChildFactory, 'child')
  
  
  def test_basic(parent):
      assert parent.child.child_attr == 'default'

Test output:

$ pytest test_with_child.py 
======================================================================================================== test session starts =========================================================================================================
platform darwin -- Python 2.7.12, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: /private/tmp/test, inifile:
plugins: factoryboy-1.3.1
collected 1 item s

test_with_child.py E

=============================================================================================================== ERRORS ===============================================================================================================
____________________________________________________________________________________________________ ERROR at setup of test_basic ____________________________________________________________________________________________________
file /private/tmp/test/test_with_child.py, line 23
  def test_basic(parent):
file <string>, line 2: source code not available
file <string>, line 2: source code not available
E       fixture 'dict__child_attr' not found
>       available fixtures: cache, capfd, capsys, child, child__child_attr, child_factory, doctest_namespace, factoryboy_request, monkeypatch, parent, parent__child, parent_factory, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

<string>:2
====================================================================================================== 1 error in 0.03 seconds =======================================================================================================

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 30, 2024

Allright.. The reason is that pytest-factoryboy uses factoryboy declarations, so you can't specify the name of the fixture here: child = factory.SubFactory(ChildFactory)

Instead it is trying to get the name from the model (magic).

If you can use models that have names it would make it easier:

import factory
import pytest_factoryboy


class Child(object):

    def __init__(self, child_attr):
        self.child_attr = child_attr


class Parent(object):

    def __init__(self, child):
        self.child = child


class ChildFactory(factory.Factory):
    class Meta:
        model = Child

    child_attr = 'default'


class ParentFactory(factory.Factory):
    class Meta:
        model = Parent

    child = factory.SubFactory(ChildFactory)


pytest_factoryboy.register(ParentFactory)
pytest_factoryboy.register(ChildFactory)


def test_basic(parent):
    assert parent.child.child_attr == 'default'

from pytest-factoryboy.

nguyening avatar nguyening commented on May 30, 2024

I'm not sure how many factoryboy users are not using Django/some other ORM, but I think calling that out in the docs could be good?

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 30, 2024

@nguyening https://github.com/pytest-dev/pytest-factoryboy#model-fixture - it states the naming convention. The problem in your case that the name is "dict"

If you want dictionaries you could subclass your "models" from dicts..

class Parent(dict):
"""Parent"""

class Child(dict):
"""Child."""

Your fixtures should have names anyway. The reason of the limitation here is not because of the ORM.
Pytest works with fitures that are statically defined. So you can create relatively quickly some setup of objects that instantiate their dependencies. And you can address each parent or child by the name of the fixture. It will give you the same (a single instance).
The feature of registering a factory for a different model name is designed for things like:

book, second_book, author, other_author

If you need to partially specialize a book with another author- there's a LazyFixture for it.

register("book_of_another_author", BookFactory, author=LazyFixture("other_author"))

In your case you were trying to refer to parent/child from the FactoryBoy declaration. Which has nothing to do with fixtures and factory registration yet.

from pytest-factoryboy.

nguyening avatar nguyening commented on May 30, 2024

Ah, okay -- thanks for pointing that out and clearing it up for me!

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.