question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a function to detect if pytest is running

See original GitHub issue

What’s the problem this feature will solve?

Django settings modules often load environment variables from a .env file:

from pathlib import Path

import dotenv

BASE_DIR = Path(__file__).resolve().parent.parent

dotenv.load_dotenv(dotenv_path=BASE_DIR / ".env")

Loading from .env is not desirable during test runs. Environment variables added for local debugging may affect the test run in unexpected ways, such as making remote API calls.

The settings module should therefore only load the .env file if pytest isn’t running:

from pathlib import Path

import dotenv

BASE_DIR = Path(__file__).resolve().parent.parent

if ... # pytest is not running
    dotenv.load_dotenv(dotenv_path=BASE_DIR / ".env")

Changing behaviour during tests is usually undesirable, but here it’s done to isolate the test run, which is a good thing.

pytest documents a pattern to change behaviour using fixtures here: https://docs.pytest.org/en/latest/example/simple.html#detect-if-running-from-within-a-pytest-run . Unfortunately this techinque is incompatible with this particular problem. pytest-django’s fixtures load the settings module early, before fixtures from conftest.py fixtures run, so the pattern cannot be applied. And environment variable changes cannot be undone after the fact.

To solve this I made pytest-is-running, which can be used like so:

from pathlib import Path

import dotenv
import pytest_is_running

BASE_DIR = Path(__file__).resolve().parent.parent

if not pytest_is_running.is_running():
    dotenv.load_dotenv(dotenv_path=BASE_DIR / ".env")

pytest-is-running works because plugin fixtures can load early, and it uses hooks with tryfirst=True.

Describe the solution you’d like

Rather than have a documented workaround and plugin, I’d like to see a function in core pytest that provides the ability to check if pytest is running. It could simplify things for those using the both the documented pattern and the plugin.

It would also be good if the solution could somehow avoid the cost of doing import pytest, so that non-test pathways do not import the heap of stuff in pytest only to ignore it because pytest isn’t running. The plugin is deliberately structured to avoid this.

Alternative Solutions

I wrote the plugin.

Additional context

none

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
The-Compilercommented, Jan 11, 2022

We currently just document how to do this, probably in a way similarly to what your plugin does.

Note you could also just check if "pytest" in sys.modules: as long as your code under test does not import pytest.

We do have a PYTEST_CURRENT_TEST environment variable already. Personally I’d be fine with having a PYTEST_VERSION or whatever which gets set very early, I suppose PYTEST_CURRENT_TEST will be set too late for it to be useful at import time.

1reaction
The-Compilercommented, Jan 11, 2022

Also see https://github.com/pytest-dev/pytest-django/issues/333 which has some additional considerations and use-cases around this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Test if code is executed from within a py.test session
A simpler solution I came to: import sys if "pytest" in sys.modules: ... Pytest runner will always load the pytest module, ...
Read more >
pytest-is-running - PyPI
pytest will automatically find the plugin and use it when you run pytest. You can check if pytest is running with the is_running()...
Read more >
Basic patterns and examples — pytest documentation
Pass different values to a test function, depending on command line options¶. Suppose we want to write a ... which will add info...
Read more >
Usage and Invocations — pytest documentation
Pytest supports several ways to run and select tests from the command-line. ... This will run tests which contain names that match the...
Read more >
Working with custom markers — pytest documentation
Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found