Comments (6)
@nguyening did you register ChildFactory?
from pytest-factoryboy.
@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.
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.
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.
@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.
Ah, okay -- thanks for pointing that out and clearing it up for me!
from pytest-factoryboy.
Related Issues (20)
- Allow to register factories in the class namespace.
- pytest-factoryboy seems to break factory.django.mute_signals HOT 4
- Fix tests/test_postgen_dependencies::test_postgen_related
- AttributeError using `LazyFixture` in `register()` HOT 5
- Parent factory model name disregards `_name` override. HOT 1
- Ship tests with source distribution HOT 1
- function uses no argument HOT 1
- UserWarning: UserWarning: Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged HOT 5
- Error `fixture not found` if define `register(_name=)` for connected SubFactory which is connected with another SubFactory HOT 1
- DeprecationWarning with pytest 7.2.0 HOT 4
- PytestAssertRewriteWarning
- Feature request: override name of factory fixture? HOT 3
- Alternative for `register`
- register with type hints HOT 3
- Improve handling of `factory.Maybe`
- Request other fixtures in fixtures generated by register HOT 3
- How can I pass function-scope fixtures (e.g. db sessions) to each Factory in combination with pytest-asyncio?
- Question regarding of usage of LazyFixture in @pytest.mark.parametrize on alias instead of chained fixture. HOT 1
- Broken support for Python 3.7 HOT 1
- 2.7.0: missing git version tag
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-factoryboy.