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?
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 ============================================================================================================================
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:
Output: (notice the memory addresses of self are different)
Thanks!
Seth