Skip to content

Instantly share code, notes, and snippets.

@eshirazi
Last active October 7, 2024 20:39
Show Gist options
  • Select an option

  • Save eshirazi/f282354d8b1c21c3829f6489fea971cd to your computer and use it in GitHub Desktop.

Select an option

Save eshirazi/f282354d8b1c21c3829f6489fea971cd to your computer and use it in GitHub Desktop.
Is Python running in unit-test mode
import inspect
def is_running_unit_tests():
return any(
i[1].endswith("unittest/loader.py") or
i[1].endswith("unittest\\loader.py") or
i[1].endswith("unittest/main.py") or
i[1].endswith("unittest\\main.py") or
i[1].endswith("unittest/runner.py") or
i[1].endswith("unittest\\runner.py") or
i[1].endswith("pytest/main.py") or
i[1].endswith("pytest\\main.py")
for i in inspect.stack()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment