120

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?

4
  • Need more details. py.test, database? These are somewhat generic.
    – user590028
    Aug 7, 2014 at 17:11
  • 2
    py.test is the testing system (pytest.org). My DB here in Mongo, but it could be any DB, methinks.
    – Laizer
    Aug 7, 2014 at 17:21
  • Ahh.. Sorry I couldn't be more helpful. Glad you found the answer.
    – user590028
    Aug 7, 2014 at 17:23
  • 3
    This question is getting negative votes because it's considered "bad practice". For me, I have a lot of experiments where I just want to test if they run without error. Running things to completion takes way too long, so I've been passing around a "test_mode" argument to every experiment I want to test this way, which just does various things to shorten the experiment while still running through all the code. This approach is very repetitive, and kind of sloppy, I'd prefer just to ask the system if I'm in a test. Does anybody have a nicer solution than the one accepted below?
    – Peter
    Apr 13, 2015 at 17:37

6 Answers 6

135

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.

9
  • 10
    Seems like a cleaner / more pythonic solution if you ask me
    – NirIzr
    Jul 26, 2018 at 0:25
  • 3
    Much nicer answer that could be used for other test frameworks like nose as well
    – RichVel
    Oct 17, 2018 at 8:18
  • 4
    Likewise, this is the recommended approach for detecting py.test under Django. Jan 15, 2019 at 23:32
  • 4
    Warning: I'm getting false positives from this approach. In a non-pytest environment, I'm seeing pytest in sys.modules.keys().
    – duhaime
    Jun 5, 2019 at 0:50
  • 3
    @duhaime The only way that it is in sys.modules is that it has been imported, in a way or another, directly in your code or through another import (there might be one of the libraries you use that does import it). And as I said, "this solution only works if the code you're trying to test does not use pytest itself".
    – ramnes
    Jun 6, 2019 at 7:53
102

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

Note

  • This method works only when an actual test is being run.
  • This detection will not work when modules are imported during pytest collection.
5
  • Works for me. (in env.py for Flask-Migrate==2.5.2 alembic==1.3.3 Flask-SQLAlchemy==2.4.1 SQLAlchemy==1.3.13 pytest==5.3.5)
    – techniao
    Mar 29, 2020 at 1:57
  • 23
    Note that this method works only when some actual test is being run! This detection will not work when modules are imported during pytest collection!
    – kolypto
    Oct 28, 2021 at 22:39
  • 4
    Thanks, @kolypto! That explains why module-level "constants" were set incorrectly for me (as if no test was being run). The answer should include this important fact.
    – bers
    Feb 2, 2022 at 13:45
  • 2
    As I found out from my short investigation, this variable is not available during the execution of fixtures. Thus, you cannot make a decision about choosing a test database to initialize it. Dec 9, 2022 at 16:25
  • Nice, as prod code can avoid a pytest import dependancy
    – dsz
    Jan 17 at 22:46
50

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.

5
13

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

script_name = os.path.basename(sys.argv[0])
if script_name in ['pytest', 'py.test']:
  print('Running with pytest!')
4
  • If you're using 'py.test', this will test for both any(re.findall(r'pytest|py.test', sys.argv[0]))
    – naaman
    Dec 15, 2019 at 5:27
  • It seems that the env variable was introduced in 3.2: docs.pytest.org/en/stable/… but maybe it was temporarily removed in some versions ?
    – smarie
    Apr 7, 2021 at 16:08
  • I found that this is not always reliable. If you use pytest-xdist package this does not work. The module check works in that scenario though Oct 18, 2021 at 16:11
  • Yes, you should definitely use the module approach unless you get the false positives I describe in a comment on that answer suggestion!
    – duhaime
    Oct 19, 2021 at 15:08
3

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.

3
  • 4
    The py.test documentation you referenced is hardly a hack; it's the official solution to a common complaint. That said, the trivial one-liner proposed by ramnes is dramatically superior for most use cases. Regardless, What You Want To Do Is Considered Harmful™ is never a valid solution. If that was the best you could do, why bother responding at all? There are demonstrable architectural reasons for a codebase to detect py.test at runtime. Jan 15, 2019 at 23:26
  • 2
    I agree with this concern. In general, if you're writing your code to behave different when run in test, are you really testing your code? Or are you just testing your test code? As you branch your code in different directions when in test, you'll get farther and farther from the spirit of why tests work. #meta
    – macetw
    May 25, 2021 at 19:08
  • 1
    I've found this trick useful for overriding delay times in retry logic when I expect something to fail in my tests. Unit tests should execute fast.
    – Timo
    Jun 27 at 12:07
3

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')

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.