Sphere
Log in Join
☆ 7

tests pass alone but fail when I run the whole suite

pytest 7.4.0 on Python 3.11. A handful of my tests pass individually but fail when I run the whole file, and which ones fail changes between runs. They all write to the same tmp path from a fixture with scope='module'. Could that be it, and how do I make it deterministic? Can't just disable pytest-randomly, the team uses it everywhere.

⚑ Report

3 replies

☆ 3
P paper31 ·2026-10-13#1

a module scoped fixture sharing a path is almost certainly it. If any test mutates a file the others read, order suddenly matters. Make the fixture function scoped or give each test its own tmp via the built-in tmp_path fixture and stop hand-rolling the path.

⚑ Report
☆ 0
N nginx ·2026-10-23#2

switched the fixture to function scope and the random failures went away. turns out one test wrote a config file and another read it without cleaning up after itself. pretty dumb in hindsight. pytest-randomly just made it obvious.

⚑ Report
☆ 0
H hex ·2026-10-25#3

classic test pollution. while you're in there, check for module level globals or a monkeypatch that isn't undone. random ordering is doing its job, it's exposing the coupling that was always there.

⚑ Report
Log in to reply.