title: How to parameterize a test depending on fixture parameters in pytest?
body: <p>I have a Python program that generates C code according to an input specification. I am writing tests with pytest. Naturally, the testing strategy includes some tests on the generated C code.</p>
  <p>For these tests, the plan looks like this:</p>
  <ul>
  <li><p>We have a set of directories, each containing one specification file and a set of applicable input/expected output cases.</p>
  </li>
  <li><p>A fixture will handle generating the C code and compiling it. This fixture will be parameterized over the set of specification files (which are read programmatically by the test script). This has the advantage that the build can be done only once for all test cases under that specification (since the build is expensive).</p>
  </li>
  <li><p>A test function will take the <code>GeneratedCode</code> object from the fixture, run it with a certain input, and verify the expected output. This will be parameterized over the set of input/output cases (which are also read programmatically by the script).</p>
  </li>
  </ul>
  <p>This way, adding new test cases is as simple as adding a new specification or test case file. There is no need to copy and paste code in the test script.</p>
  <p>I imagined it looking something like this:</p>
  <pre class="lang-py prettyprint-override"><code># Get the list of specification files and test cases programmatically
  specification_names = get_list_of_specifications()
  test_cases = dict()
  for spec in specification_names:
      # get_list_of_test_cases() returns a list of (input, output) tuples
      test_cases[spec] = get_list_of_test_cases(spec)
  
  class GeneratedCode:
      def __init__(spec):
          &quot;&quot;&quot;Generate the C code for spec in a temp directory&quot;&quot;&quot;
          self.name = spec
          ...
      
      def build():
          &quot;&quot;&quot;Build the generated C code&quot;&quot;&quot;
          ...
      
      def run(input):
          &quot;&quot;&quot;Run the code on given input.&quot;&quot;&quot;
          ...
      
      def cleanup():
          ...
  
  @pytest.fixture(scope=&quot;module&quot;, params=specification_names)
  def generated_code(request):
      code = GeneratedCode(request.param)
      code.build()
      yield code
      code.cleanup()
  
  @pytest.mark.parametrize('test_input,expected_output', test_cases[???])
  def test_generated_code(generated_code, test_input, expected_output):
      assert generated_code.run(test_input) == expected_output
  </code></pre>
  <p>Of course, the problem here is that <code>@pytest.mark.parametrize()</code> can't just use the same set of test cases each time since it depends on the specification the code was generated from. If we can get the parameter for the current fixture, we can look it up in the <code>test_cases</code> dict, but I'm not sure how to do that, or if it's even possible.</p>
  <p>Is there a way to accomplish this? Is there some other way I should approach these tests?</p>