Skip to content

Tests within a class do not share the same class object #6890

Description

Hey All,

I was surprised when I found out that Tests within a class do not share the same Class object. My use case is that I am trying to save some data to self from one test, and use it again in another test. Why does every test object get a separate instance of the Test Class. What is the point of wrapping tests within a class, if the instance of the class does not persist for all of the tests in the class?

Example:

import pytest
  
class MySuper():
    counter = 0

class TestA(MySuper):
    def test_1(self):
        print("Test 1")
        self.counter += 3
        print(self.counter)
        print(self)
        print(dir(self))

    def test_2(self):
        print("Test 2")
        self.counter += 2
        print(self.counter)
        print(self)
        print(dir(self))
        assert self.counter == 5

Output: (notice the memory addresses of self are different)

pytest -s
=================================================================================================================================== test session starts ===================================================================================================================================
platform linux -- Python 3.6.9, pytest-3.8.2, py-1.7.0, pluggy-0.7.1
rootdir: /home/e339307/pytest_tmp, inifile:
plugins: swte-2.0.0, mock-1.10.0
collected 2 items                                                                                                                                                                                                                                                                         

my_simple_test.py Test 1
3
<my_simple_test.TestA object at 0x7f8daad2ef28>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'counter', 'test_1', 'test_2']
.Test 2
2
<my_simple_test.TestA object at 0x7f8daad2e7b8>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'counter', 'test_1', 'test_2']
F

======================================================================================================================================== FAILURES =========================================================================================================================================
______________________________________________________________________________________________________________________________________ TestA.test_2 _______________________________________________________________________________________________________________________________________

self = <my_simple_test.TestA object at 0x7f8daad2e7b8>

    def test_2(self):
        print("Test 2")
        self.counter += 2
        print(self.counter)
        print(self)
        print(dir(self))
>       assert self.counter == 5
E       assert 2 == 5
E        +  where 2 = <my_simple_test.TestA object at 0x7f8daad2e7b8>.counter

my_simple_test.py:20: AssertionError
====================================================================================================================================== REQUIREMENTS =======================================================================================================================================
=========================================================================================================================== 1 failed, 1 passed in 0.08 seconds ============================================================================================================================

Thanks!
Seth

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions