pytest how to parameterize part of a config to each test?

Solution 1:

I wrote a package called Parametrize From File that makes it very easy to load test parameters from config files. I think it'd be a good match for what you're trying to do. Here's how you'd use it:

  1. Reorganize your config file such that the top level is a dictionary mapping test names to lists of test cases:
    # test_joker.yml
    test_joker_names:
      - id: test1
        name: joker
    
      - id: test2
        name: joker
    
    test_not_joker_names:
      - id: test3
        name: not_joker
    
      - id: test4
        name: not_joker
    
  2. Decorate your test functions with @parametrize_from_file:
    # test_joker.py
    import parametrize_from_file
    
    @parametrize_from_file
    def test_joker_names(name):
        assert name == 'joker'
    
    @parametrize_from_file
    def test_not_joker_names(name):
        assert name != 'joker'
    
  3. Run pytest like usual.

Some comments:

  • In this example the two test cases for each test are redundant, but I assume in your real application you have multiple different test cases for each test.
  • This approach gets rid of the fixture entirely, which I think is a good thing. If you actually need a fixture though, Parameterize From File can also parametrize fixtures.
  • The id field in the config file is interpreted specially. It's used as the name for the test case, and not passed to the test as a parameter. This is described more in the docs (linked above).
  • I chose the file/test names in this example so that the parameters would be found by default (i.e. without having to specify any paths). But it's also possible to specify paths if the defaults don't work for you.