2

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
rtindru
  • 5,107
  • 9
  • 41
  • 59
  • 1
    That example seems quite poor. I suppose what they mean is that if one test sets attributes on the class instance, then another test could have expected the attributes to differ? – Justin Jun 30 '21 at 22:05
  • 1
    Each test gets a new instance of the class in order to avoid side effects. Because each test starts with a fresh class instance, you don't have tests erroneously passing because a previous test happened to set something up correctly. – larsks Jun 30 '21 at 22:20
  • Agree, I was wondering it it's just a poor example, or if I am missing something? However, what you say makes perfect sense to me. If `test_one` set `cls.value=1` and `test_two` set `cls.value=2`, then it could lead to problems due to the shared mutable state. – rtindru Jun 30 '21 at 22:20
  • Will wait for a couple more comments to see if someone can make sense of the example, otherwise I'll close this question :) – rtindru Jun 30 '21 at 22:23

2 Answers2

0

The example above states that:

Something to be aware of when grouping tests inside classes is that each test has a 
unique instance of the class.
______________________ 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>

However, both TestClassDemoInstance in test_one & test_two point to the same object address. This example will be clearer if the addresses for the two objects are different to signify object uniqueness.

qbit
  • 1
0

I wonder why nobody ran the code and checked.

After running below code -

class TestClassDemoInstance:
def test_one(self):
    assert 0

def test_two(self):
    assert 0

pytest --verbosity=1 code.py

I got two different address unlike mentioned in the documentation.

self = <random_code.TestClassDemoInstance object at 0x7ff8009c1190>
self = <random_code.TestClassDemoInstance object at 0x7ff8009c1890>
user5319825
  • 491
  • 1
  • 6
  • 16