Test if code is executed from within a py.test session

前端 未结 6 1407
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 18:19

I\'d like to connect to a different database if my code is running under py.test. Is there a function to call or an environment variable that I can test that will tell me i

6条回答
  •  青春惊慌失措
    2020-12-08 18:57

    There's also another way documented in the manual: https://docs.pytest.org/en/latest/example/simple.html#pytest-current-test-environment-variable

    Pytest will set the following environment variable PYTEST_CURRENT_TEST.

    Checking the existence of said variable should reliably allow one to detect if code is being executed from within the umbrella of pytest.

    import os
    if "PYTEST_CURRENT_TEST" in os.environ:
        # We are running under pytest, act accordingly...
    

提交回复
热议问题