Pytest running doesn't take into consideration env.
See original GitHub issueEnvironment 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:
- Set a test that depends on a enviroment variable to run.
- Declare that variable in an
.env
file or interminal.integrated.env.linux
- Run test.
Logs
Output for Python
in the Output
panel (View
→Output
, 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:
- Created 4 years ago
- Reactions:14
- Comments:35 (8 by maintainers)
Top GitHub Comments
@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?
@CMLL the way i solved it was to install pytest-dotenv
I ran the command
pip install pytest-dotenv
And ready! Maybe you need to reload the vscode application