I'm using a singleton object to manage database connections. I'm running a extense suite of tests that depends on that object. Also I have to test that object too so i have to delete it and check if its __del__
method executes properly.
When I test it, because I deleted the singleton, other tests fail because they can't access it anymore. I need to restore it after deleting it, or avoid deleting it and test the delete method other way. Changing the fixture scope could cause an increase in execution time, so its the last resort.
My singleton is something like this:
class Singleton(type):
def __call__(cls, *args, **kwargs):
if not hasattr(cls, "singleton_instance"):
cls.singleton_instance = super().__call__(*args, **kwargs)
return cls.singleton_instance
class SpecificSingle(metaclass=Singleton):
def __init__(self):
self.data = 'some complex data'
def __del__(self):
# complex logic before delete the object
del self.data
pass
And some tests to simulate mines:
import pytest
@pytest.fixture(scope='session')
def use_the_single():
single = SpecificSingle()
yield single
del single
def test_delete(use_the_single):
use_the_single.__del__()
assert(use_the_single not in locals())
def test_something(use_the_single):
test = use_the_single.data
assert(test == 'some complex data')