title: pytest: how to use a mark to inject a fixture?
body: <p>I'm writing a pytest plugin with a fixture that has a side effect of setting up some desirable mocks. I'd like to write a simple mark that will allow the user to call this fixture setup before the test runs, without having to include the fixture in the test function parameters -- essentially, "injecting" the fixture using a mark. My reasoning is that the user may want the mocked setup without needing the return value of the fixture itself, in which case it seems more intuitive to me to use a mark than to require that they declare a fixture they won't be using.</p>
  
  <p>How might I use a mark to require a fixture in pytest? Looking at the docs, it seems like I want to hook into something like <code>pytest_collection_modifyitems</code>, check for the relevant mark on each item using <code>Item.iter_markers</code>, and then somehow update the list of fixtures. Reading the code, however, I'm having trouble figuring out how exactly to trigger that fixture setup. </p>
  
  <p>Here's a simplified example of what the fixture in question looks like:</p>
  
  <pre><code>@pytest.fixture
  def mocks(mocker):
      ret_val = 10
      mocker.patch('path.to.patch', return_value=ret_val)
      return ret_val
  </code></pre>
  
  <p>Here's what the user can do to set up the mocks now:</p>
  
  <pre><code>def test_mocks(mocks):
      # 'path.to.patch' will be mocked in this test
      # ... test code ...
  </code></pre>
  
  <p>But here's what the test might look like if the fixture could be triggered via a mark instead:</p>
  
  <pre><code>@pytest.mark.mocks
  def test_mocks():
      # 'path.to.patch' will be mocked in this test, too
      # ... test code ...
  </code></pre>