Closely related: In python, is there a good idiom for using context managers in setup/teardown
I have a context manager that is used in tests to fix the time/timezone. I want to have it in a pytest funcarg (or fixture, we are using pytest 2.2.3 but I can translate backwards). I could just do this:
def pytest_funcarg__fixedTimezone(request):
    # fix timezone to match Qld, no DST to worry about and matches all
    # Eastern states in winter.
    fixedTime = offsetTime.DisplacedRealTime(tz=' Australia/Brisbane')
    def setup():
        fixedTime.__enter__()
        return fixedTime
    def teardown(fixedTime):
        # this seems rather odd?
        fixedTime.__exit__(None, None, None)
... but it's a bit icky. In the related Q jsbueno points out: The problem is that your code has no provision to call the object's __exit__ method properly if an exception occurs. 
His answer uses a metaclass approach. But this is not that useful for pytest where often tests are just functions, not classes. So what would be the pytest-y way to solve this? Something involving runtest hooks?
 
     
     
     
    