Closed dropkickdev closed 3 years ago
I thought it was a bug but it wasn't at all. My apologies. Here is the solution I found for anyone who's interested in finding out how to make @pytest.mark.parametrize
work.
# Fixtures
@pytest.fixture
def client():
with TestClient(get_app()) as tc:
yield tc
@pytest.fixture
def loop(client):
yield client.task.get_loop()
# Test
param = [
('user.create', ['AdminGroup', 'NoaddGroup']),
('page.create', ['DataGroup'])
]
@pytest.mark.parametrize('perm, out', param)
@pytest.mark.focus
def test_permissions_get_groups(loop, perm, out):
async def ab():
groups = await Permission.get_groups(perm)
assert Counter(groups) == Counter(out)
loop.run_until_complete(ab())
Thanks for sharing!
Describe the bug In FastAPI, when running a test which uses
@pytest.mark.parametrize
to test multiple values, the first test goes through but the second and succeeding ones do not. Regardless of the test data being run they all have the same error.If
@pytest.mark.parametrize
has 3 types of data to test then the error above comes up 2x since only the first test would work. I'm guessing after the first test it thinks everything is all done and closes the event loop.Changing the
scope
of the fixture also doesn't work and leads toTo Reproduce
And the models (simplified for this)
Expected behavior Event loop would stay open until the last
@pytest.mark.parametrize
test is done.Additional context As a hack, I'm running
param
inside the test as a loop so there would technically only be one test but with multiple assertions. It works but I think using@pytest.mark.parametrize
is much better.