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.

Pytest running doesn't take into consideration env.

See original GitHub issue

Environment data

  • VS Code version: 1.36.1
  • Extension version (available under the Extensions sidebar): 2019.9.29489-dev
  • OS and version: Ubuntu 19.04
  • Python version (& distribution if applicable, e.g. Anaconda): 2.7.15+
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
  • Relevant/affected Python packages and their versions: pytest
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info #3977): LanguageServer

Expected behaviour

Test discovery and running have the enviroment variables declared in the .env file or the terminal.integrated.env.linux settings before executing.

Actual behaviour

Tests that depend on a specific environment variable being set to run fail.

Steps to reproduce:

  1. Set a test that depends on a enviroment variable to run.
  2. Declare that variable in an .env file or in terminal.integrated.env.linux
  3. Run test.

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

In this output I’m running a test that depends on a variable PANOPTA_INTEGRATION_TESTS_CONFIG to point to a file, and if not found it defaults to this file /etc/panopta_tests_support_data.yaml, which doesn’t exist. I have declared said variable in an .env file at my workspace root. PANOPTA_INTEGRATION_TESTS_CONFIG=/home/cllamach/Panopta/classic/src/models/tests/integration/support_data_config.yaml

But when adding log statements to pytest run module I see it tries to load the default file instead of the one declared in the .env file. I ran into a similar problem with the PYTHONPATH variable not being taken into account when declared into the .env file and was able to bypass it by installing pytest-pythonpath to declare its values there.

python /home/cllamach/.vscode/extensions/ms-python.python-2019.9.29489-dev/pythonFiles/testing_tools/run_adapter.py discover pytest -- -s --cache-clear --disable-warning /home/cllamach/Panopta/classic/src/tests/
Test Discovery failed: 
Error: ============================= test session starts ==============================
platform linux2 -- Python 2.7.15+, pytest-4.4.1, py-1.8.0, pluggy-0.9.0
rootdir: /home/cllamach/Panopta/classic/src/tests, inifile: pytest.ini
plugins: xdist-1.28.0, sugar-0.9.2, pythonpath-0.7.3, forked-1.0.2, pylama-7.7.1
ConfTest /home/cllamach/Panopta/classic/src/tests/integration/conftest.py
PKGPath /home/cllamach/Panopta/classic/src/tests/integration
Error import conftest [Errno 2] No such file or directory: '/etc/panopta_tests_support_data.yaml'
collected 0 items / 1 errors

==================================== ERRORS ====================================
________________________ ERROR collecting test session _________________________
../../.virtualenvs/panopta_env/local/lib/python2.7/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
../../.virtualenvs/panopta_env/local/lib/python2.7/site-packages/py/_path/common.py:419: in gen
    if p.check(dir=1) and (rec is None or rec(p))])
../../.virtualenvs/panopta_env/local/lib/python2.7/site-packages/_pytest/main.py:660: in _recurse
    ihook = self.gethookproxy(dirpath)
../../.virtualenvs/panopta_env/local/lib/python2.7/site-packages/_pytest/main.py:481: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
../../.virtualenvs/panopta_env/local/lib/python2.7/site-packages/_pytest/config/__init__.py:419: in _getconftestmodules
    mod = self._importconftest(conftestpath.realpath())
../../.virtualenvs/panopta_env/local/lib/python2.7/site-packages/_pytest/config/__init__.py:461: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   ConftestImportFailure: (local('/home/cllamach/Panopta/classic/src/tests/integration/conftest.py'), (<type 'exceptions.IOError'>, IOError(2, 'No such file or directory'), <traceback object at 0x7f7a47389ef0>))
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
===================== 21 warnings, 1 error in 0.53 seconds =====================

Traceback (most recent call last):
  File "/home/cllamach/.vscode/extensions/ms-python.python-2019.9.29489-dev/pythonFiles/testing_tools/run_adapter.py", line 18, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/home/cllamach/.vscode/extensions/ms-python.python-2019.9.29489-dev/pythonFiles/testing_tools/adapter/__main__.py", line 90, in main
    parents, result = run(toolargs, **subargs)
  File "/home/cllamach/.vscode/extensions/ms-python.python-2019.9.29489-dev/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 36, in discover
    raise Exception('pytest discovery failed (exit code {})'.format(ec))
Exception: pytest discovery failed (exit code 2)

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)

XXX

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:14
  • Comments:35 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
luabudcommented, Oct 11, 2019

@nickcointracker yes, it does when you specify the path to the .env file in your settings.json file. For example, if the .env file is in your workspace folder, you can add the following line there:

"python.envFile": "${workspaceFolder}/.env"

Does that help you?

4reactions
SandoDevcommented, Aug 24, 2021

@CMLL the way i solved it was to install pytest-dotenv

test_one py - pytest-env - Visual Studio Code_755

I ran the command pip install pytest-dotenv

And ready! Maybe you need to reload the vscode application

test_one py - pytest-env - Visual Studio Code_757

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass environment variables to pytest - Stack Overflow
If I hard code these environment variables, for eg: ENV_NAME = 'staging', ENV_NUMBER = '5' in my code and then run the tests...
Read more >
Temp environment variables for pytest | by Shay Palachy
First, set up the environment variables you want to add and/or remove for the duration of the test in temp_env_var.py , using the...
Read more >
Testing Environment variable in pytest - DEV Community ‍ ‍
Using pytest-env plugin. We can use this plugin to set environment variables that don't really matter to the function implementations.
Read more >
Configuration — pytest documentation
Many pytest settings can be set in a configuration file, ... unique nodeid which is rooted at the rootdir and takes into account...
Read more >
pytest import mechanisms and sys.path / PYTHONPATH
pytest as a testing framework needs to import test modules and conftest.py files for execution. Importing files in Python (at least until recently)...
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