I have tests that are identical for various values, so I want to parametrize the test class. Because the code is making network calls, I also want to use class scoped fixtures.
I tried
import pytest
@pytest.mark.parametrize('fruit', ['apple', 'banana'])
class TestFruit:
@pytest.fixture(scope='class')
def get_fruit(self, fruit):
print(f'Network call to GET {fruit}. This is expensive.')
return fruit
def test_fruit(self, get_fruit):
print(f'The fruit is {get_fruit}.')
assert get_fruit == 'apple'
def test_fruit2(self, get_fruit):
print(f'Fruit in test_fruit2 is {get_fruit}')
assert get_fruit == 'orange'
I want all of the tests to be run for every 'fruit' listed, and I want get_fruit
to only be called once per class, not for each test.
When I run the code, I get ScopeMismatch: You tried to access the function scoped fixture endpoint with a class scoped request object
Unexpected behavior of class scoped fixture when calling a class scoped parametrized fixture in Pytest describes the issue I'm having, but I don't see a difference between the code posted in the question and the code posted in the answer.