我有一个我在应用程序中使用的单例类(Singleton在元类中实现)
class Singleton(type):
def __init__(self, *args, **kwargs):
self.__instance = None
super(Singleton, self).__init__(*args, **kwargs)
def __call__(self, *args, **kwargs):
if self.__instance is None:
self.__instance = super(Singleton, self).__call__(*args, **kwargs)
return self.__instance
else:
return self.__instance
class ApplicationContext(object):
__metaclass__ = Singleton
def __init__(self, db):
pass
现在我希望这个对象位于py.test上下文中 - 但我希望它在某些测试中不受影响。 我试图创建一个拆卸功能,但它似乎没有工作......
@pytest.fixture
def context(db, request):
_context = ApplicationContext(db)
def teardown():
print 'teardown'
del _context #this is not working. What should be done here?
request.addfinalizer(teardown)
return _context