teardown of a singleton class in pytest
<p>I have a singleton class I'm using in the application (Singleton is implemented in metaclass)</p>

<pre><code>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
</code></pre>

<p>Now I want this object to be in a py.test context - but I want it out of the way in some tests. 
I tried to create a teardown function but it didn't seem to work...</p>

<pre><code>@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
</code></pre>