How to share the same instance for all methods of a pytest test class

Viewed 2001

I have a simple test class

@pytest.mark.incremental
class TestXYZ:

    def test_x(self):
        print(self)

    def test_y(self):
        print(self)

    def test_z(self):
        print(self)

When I run this I get the following output:

test.TestXYZ object at 0x7f99b729c9b0

test.TestXYZ object at 0x7f99b7299b70

testTestXYZ object at 0x7f99b7287eb8

This indicates that the 3 methods are called on 3 different instances of TestXYZ object. Is there anyway to change this behavior and make pytest call all the 3 methods on the same object instance. So that I can use the self to store some values.

1 Answers
Related