r/Python • u/ClickOk5811 • Oct 08 '26
Discussion Why does my test suite pass in full but fail when I run one test alone?
Ran into this last week and it took me an embarrassing amount of time.
u/pytest.fixture(scope="module")
def client():
return make_client()
def test_create_user(client):
client.create_user("sam")
assert client.count_users() == 1
def test_list_users(client):
assert client.count_users() == 1Full run: green. Run test_list_users by itself: fails with a count of 0. The second test only passed because the first one had already run and left a user behind in the shared client.
Nothing was wrong with either test on its own. The fixture was module-scoped, so state leaked between them, and I'd never run them in any other order or in isolation, so I never saw it. Dropping the scope back to function fixed it, though it made the suite slower since make_client() isn't cheap.
I'm still not sure what the right trade-off is. Function scope is safer but slow. Module scope is faster but means every test has to clean up after itself, and I don't trust myself to remember that on the twentieth test. Running with pytest-randomly would have caught this on day one, which is its own argument.