title: py.test - how to use a context manager in a funcarg/fixture
body: <p><em>Closely related:</em> <a href="https://stackoverflow.com/questions/8416208/in-python-is-there-a-good-idiom-for-using-context-managers-in-setup-teardown">In python, is there a good idiom for using context managers in setup/teardown</a></p>
  
  <hr>
  
  <p>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 <code>pytest</code> 2.2.3 but I can translate backwards). I could just do this:</p>
  
  <pre class="lang-py prettyprint-override"><code>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)
  </code></pre>
  
  <p>... but it's a bit icky. In the related Q <a href="https://stackoverflow.com/users/108205/jsbueno">jsbueno</a> points out: <em>The problem is that your code has no provision to call the object's <code>__exit__</code> method properly if an exception occurs.</em> </p>
  
  <p><a href="https://stackoverflow.com/a/8529165/54056">His answer</a> 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 <a href="http://pytest.org/latest/plugins.html#generic-runtest-hooks" rel="noreferrer">runtest hooks</a>?</p>