@@ -35,29 +35,6 @@ Concept
35
35
Library exports a function to register factories as fixtures. Fixtures are contributed
36
36
to the same module where register function is called.
37
37
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
-
61
38
62
39
Model Fixture
63
40
-------------
@@ -83,6 +60,23 @@ class name.
83
60
assert author.name == " Charles Dickens"
84
61
85
62
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
+
86
80
Model fixtures can be registered with specific names. For example, if you address instances of some collection
87
81
by the name like "first", "second" or of another parent as "other":
88
82
@@ -92,7 +86,7 @@ by the name like "first", "second" or of another parent as "other":
92
86
register(AuthorFactory) # author
93
87
register(AuthorFactory, " second_author" ) # second_author
94
88
95
- # `register(...)` can be used as a decorator too
89
+
96
90
@register # book
97
91
@register (_name = " second_book" ) # second_book
98
92
@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":
103
97
104
98
@pytest.fixture
105
99
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` ."""
107
101
return second_author
108
102
109
103
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
110
107
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"
123
108
124
109
SubFactory
125
110
----------
@@ -141,6 +126,28 @@ post-generation
141
126
142
127
Post-generation attribute fixture implements only the extracted value for the post generation function.
143
128
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
+
144
151
145
152
Integration
146
153
-----------
0 commit comments