Skip to content

Commit 1aa5298

Browse files
authored
Update README.rst examples (#199)
* Remove highlight from the "Factory fixture" section, since it's probably the least important * Update example of other_book__author to be more clear
1 parent a2561c1 commit 1aa5298

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

Diff for: README.rst

+44-37
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,6 @@ Concept
3535
Library exports a function to register factories as fixtures. Fixtures are contributed
3636
to the same module where register function is called.
3737

38-
Factory Fixture
39-
---------------
40-
41-
Factory fixtures allow using factories without importing them. The fixture name convention is to use the lowercase-underscore
42-
form of the class name.
43-
44-
.. code-block:: python
45-
46-
import factory
47-
from pytest_factoryboy import register
48-
49-
class AuthorFactory(factory.Factory):
50-
class Meta:
51-
model = Author
52-
53-
54-
register(AuthorFactory) # => author_factory
55-
56-
57-
def test_factory_fixture(author_factory):
58-
author = author_factory(name="Charles Dickens")
59-
assert author.name == "Charles Dickens"
60-
6138

6239
Model Fixture
6340
-------------
@@ -83,6 +60,23 @@ class name.
8360
assert author.name == "Charles Dickens"
8461
8562
63+
Attributes are Fixtures
64+
-----------------------
65+
66+
There are fixtures created automatically for factory attributes. Attribute names are prefixed with the model fixture name and
67+
double underscore (similar to the convention used by factory_boy).
68+
69+
70+
.. code-block:: python
71+
72+
@pytest.mark.parametrize("author__name", ["Bill Gates"])
73+
def test_model_fixture(author):
74+
assert author.name == "Bill Gates"
75+
76+
77+
Multiple fixtures
78+
-----------------
79+
8680
Model fixtures can be registered with specific names. For example, if you address instances of some collection
8781
by the name like "first", "second" or of another parent as "other":
8882

@@ -92,7 +86,7 @@ by the name like "first", "second" or of another parent as "other":
9286
register(AuthorFactory) # author
9387
register(AuthorFactory, "second_author") # second_author
9488
95-
# `register(...)` can be used as a decorator too
89+
9690
@register # book
9791
@register(_name="second_book") # second_book
9892
@register(_name="other_book") # other_book, book of another author
@@ -103,23 +97,14 @@ by the name like "first", "second" or of another parent as "other":
10397
10498
@pytest.fixture
10599
def other_book__author(second_author):
106-
"""Make the relation of the second_book to another (second) author."""
100+
"""Make the relation of the `other_book.author` to `second_author`."""
107101
return second_author
108102
109103
104+
def test_book_authors(book, second_book, other_book, author, second_author):
105+
assert book.author == second_book.author == author
106+
assert other_book.author == second_author
110107
111-
Attributes are Fixtures
112-
-----------------------
113-
114-
There are fixtures created for factory attributes. Attribute names are prefixed with the model fixture name and
115-
double underscore (similar to the convention used by factory_boy).
116-
117-
118-
.. code-block:: python
119-
120-
@pytest.mark.parametrize("author__name", ["Bill Gates"])
121-
def test_model_fixture(author):
122-
assert author.name == "Bill Gates"
123108
124109
SubFactory
125110
----------
@@ -141,6 +126,28 @@ post-generation
141126

142127
Post-generation attribute fixture implements only the extracted value for the post generation function.
143128

129+
Factory Fixture
130+
---------------
131+
132+
`pytest-factoryboy` also registers factory fixtures, to allow their use without importing them. The fixture name convention is to use the lowercase-underscore form of the class name.
133+
134+
.. code-block:: python
135+
136+
import factory
137+
from pytest_factoryboy import register
138+
139+
class AuthorFactory(factory.Factory):
140+
class Meta:
141+
model = Author
142+
143+
144+
register(AuthorFactory) # => author_factory
145+
146+
147+
def test_factory_fixture(author_factory):
148+
author = author_factory(name="Charles Dickens")
149+
assert author.name == "Charles Dickens"
150+
144151
145152
Integration
146153
-----------

0 commit comments

Comments
 (0)