Skip to content

Commit bad7a02

Browse files
committed
document new class instance per test
1 parent 4a1557f commit bad7a02

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

doc/en/getting-started.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,55 @@ Once you develop multiple tests, you may want to group them into a class. pytest
153153
154154
The first test passed and the second failed. You can easily see the intermediate values in the assertion to help you understand the reason for the failure.
155155

156+
Some reasons why grouping tests in a class can be useful is:
157+
158+
* Structural or organizational reasons
159+
* Sharing fixtures for tests only in that particular class
160+
* Applying marks at the class level and having them implicitly apply to all tests
161+
162+
Something to be aware of when grouping tests inside classes is that each test does not have the same instance of the class.
163+
Having each test share the same class instance would be very detrimental to test isolation and would promote poor test practices.
164+
This is outlined below:
165+
166+
.. code-block:: python
167+
168+
class TestClassDemoInstance:
169+
def test_one(self):
170+
assert 0
171+
172+
def test_two(self):
173+
assert 0
174+
175+
176+
.. code-block:: pytest
177+
178+
$ pytest -k TestClassDemoInstance -q
179+
180+
FF [100%]
181+
============================================================================================================== FAILURES ===============================================================================================================
182+
___________________________________________________________________________________________________ TestClassDemoInstance.test_one ____________________________________________________________________________________________________
183+
184+
self = <test_example.TestClassDemoInstance object at 0x000001F001A24EB8>, request = <FixtureRequest for <Function test_one>>
185+
186+
def test_one(self, request):
187+
> assert 0
188+
E assert 0
189+
190+
testing\test_example.py:4: AssertionError
191+
___________________________________________________________________________________________________ TestClassDemoInstance.test_two ____________________________________________________________________________________________________
192+
193+
self = <test_example.TestClassDemoInstance object at 0x000001F001A54908>, request = <FixtureRequest for <Function test_two>>
194+
195+
def test_two(self, request):
196+
> assert 0
197+
E assert 0
198+
199+
testing\test_example.py:7: AssertionError
200+
======================================================================================================= short test summary info =======================================================================================================
201+
FAILED testing/test_example.py::TestClassDemoInstance::test_one - assert 0
202+
FAILED testing/test_example.py::TestClassDemoInstance::test_two - assert 0
203+
2 failed in 0.17s
204+
156205
Request a unique temporary directory for functional tests
157206
--------------------------------------------------------------
158207

0 commit comments

Comments
 (0)