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

PythonPytest

Python Problem Overview


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 if I'm running under a py.test session? What's the best way to handle this?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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...

Solution 3 - Python

A solution came from RTFM, although not in an obvious place. The manual also had an error in code, corrected below.

> Detect if running from within a pytest run > > Usually it is a bad idea to make application code behave differently > if called from a test. But if you absolutely must find out if your > application code is running from a test you can do something like > this: > > # content of conftest.py > def pytest_configure(config): > import sys > sys._called_from_test = True >
> def pytest_unconfigure(config): > import sys # This was missing from the manual > del sys._called_from_test > > and then check for the sys._called_from_test flag: > > if hasattr(sys, '_called_from_test'): > # called from within a test run > else: > # called "normally" > > accordingly in your application. It’s also a good idea to use your own > application module rather than sys for handling flag.

Solution 4 - Python

Working with pytest==4.3.1 the methods above failed, so I just went old school and checked with:

if 'pytest' in sys.argv[0]:
  print('pytest was called!')

Solution 5 - Python

While the hack explained in the other answer (http://pytest.org/latest/example/simple.html#detect-if-running-from-within-a-pytest-run) does indeed work, you could probably design the code in such a way you would not need to do this.

If you design the code to take the database to connect to as an argument somehow, via a connection or something else, then you can simply inject a different argument when you're running the tests then when the application drives this. Your code will end up with less global state and more modulare and reusable. So to me it sounds like an example where testing drives you to design the code better.

Solution 6 - Python

This could be done by setting an environment variable inside the testing code. For example, given a project

conftest.py
mypkg/
    __init__.py
    app.py
tests/
    test_app.py

In test_app.py you can add

import os
os.environ['PYTEST_RUNNING'] = 'true'

And then you can check inside app.py:

import os
if os.environ.get('PYTEST_RUNNING', '') == 'true':
    print('pytest is running')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLaizerView Question on Stackoverflow
Solution 1 - PythonramnesView Answer on Stackoverflow
Solution 2 - PythoncgonsView Answer on Stackoverflow
Solution 3 - PythonLaizerView Answer on Stackoverflow
Solution 4 - PythonduhaimeView Answer on Stackoverflow
Solution 5 - PythonflubView Answer on Stackoverflow
Solution 6 - PythonAlexView Answer on Stackoverflow