A simpler solution I came to:
import sys
if "pytest" in sys.modules:
...
Pytest runner will always load the pytest
module, making it available in sys.modules
.
Of course, this solution only works if the code you're trying to test does not use pytest
itself.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…