pytest-bdd icon indicating copy to clipboard operation
pytest-bdd copied to clipboard

Using frame inspection to obtain feature file path is unreliable

Open DrewDevereux opened this issue 3 years ago • 2 comments

Pytest-bdd uses frame inspection to obtain the path to its feature file. This in unreliable because

Pytest performs assert rewriting, and caches the rewritten modules. If I run pytest-bdd on my local system, where my tests are in "/projects/my-project/tests", then the cached rewritten modules will end up containing this path in their metadata.

If I then spin up a docker container, with my project mounted as "/app/my-project", then when I run my tests, pytest will load the cached rewritten modules, and the metadata will be wrong. This usually doesn't matter, but it matters to pytest-bdd, because pytest-bdd will get the wrong path, and go looking for its feature file in "/projects/my-project/tests/....", and crash with a FileNotFoundError.

DrewDevereux avatar Feb 01 '21 08:02 DrewDevereux

Workaround for the docker case is to have **/__pycache__ in your .dockerignore. But this doesn't fix the general issue of running locally or across a mount.

DrewDevereux avatar Feb 03 '21 01:02 DrewDevereux

I am continuing to run into this issue when deploying the same test suite to different docker containers.

The above workaround is fine if you copy the test suite into your image at build stage. It doesn't help if you mount the test suite at container runtime.

For runtime, a workaround is always to run your tests with PYTHONDONTWRITEBYTECODE environment variable set to 1, or using -B option to python i.e. python -B -m pytest.

DrewDevereux avatar May 13 '21 05:05 DrewDevereux