I was reading the Pytest documentation when I noticed a section titled "Grouping multiple tests in a class". There's a paragraph below with a caveat that each test method gets a unique copy of the class instance.Pytest provided a counter-example for why sharing the class instance would be a bad idea.
I don't quite understand this example. Could you help me understand the point being made with this example?
"""
Something to be aware of when grouping tests inside classes is that each test has a
unique instance of the class. Having each test share the same class instance would be
very detrimental to test isolation and would promote poor test practices.
This is outlined below:
"""
# content of test_class_demo.py
class TestClassDemoInstance:
def test_one(self):
assert 0
def test_two(self):
assert 0
$ pytest -k TestClassDemoInstance -q
FF [100%]
================================= FAILURES =================================
______________________ TestClassDemoInstance.test_one ______________________
self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>
def test_one(self):
> assert 0
E assert 0
test_class_demo.py:3: AssertionError
______________________ TestClassDemoInstance.test_two ______________________
self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>
def test_two(self):
> assert 0
E assert 0
test_class_demo.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_class_demo.py::TestClassDemoInstance::test_one - assert 0
FAILED test_class_demo.py::TestClassDemoInstance::test_two - assert 0
2 failed in 0.12s