Python pytest中单例类的拆卸

Python pytest中单例类的拆卸,python,singleton,pytest,Python,Singleton,Pytest,我在应用程序中使用了一个singleton类(singleton在元类中实现) 现在我希望这个对象位于py.test上下文中,但我希望它在某些测试中不受影响。 我试图创建一个拆卸函数,但它似乎不起作用 @pytest.fixture def context(db, request): _context = ApplicationContext(db) def teardown(): print 'teardown' del _context

我在应用程序中使用了一个singleton类(singleton在元类中实现)

现在我希望这个对象位于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
@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