vscode-python: Testing with pytest always issues a file not found error

Whenever I run the tests, I always getting this strange error: Detail: Unable to read file (Error: File not found image I really don’t have a c file in my project. But where is it coming from?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Not sure if it the same bug or not. Getting File not found on Mac OS for pytest when click on shaded Run Test above the test. The file and the test obviously exist. Error is below.

============================= test session starts ==============================
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/vistatest/Git/vista
plugins: postgresql-1.4.0, flask-0.14.0, mock-1.10.4, flask-sqlalchemy-1.0.2
collected 0 items

- generated xml file: /var/folders/lv/_hsxgz4926343l7tn7tc43wm0000gn/T/tmp-1213osfyIlUdGERC.xml -
========================= no tests ran in 0.01 seconds =========================
ERROR: file not found: ./git_work/test_fsrepo.py::test_fsrepo_local_stats

@luabud I can confirm that if test directory contains an pytest.ini, and file path for the files in the test directory are being incorrectly calculated while running tests.

For eg. Expected path: c:\Users\luabud\Apps\vscode\tests\two\test_d.py Actual path: c:\Users\luabud\Apps\vscode\two\test_d.py

I’m getting a similar error:

Environment data

  • VS Code version: 1.36.1
  • Extension version (available under the Extensions sidebar): 2019.6.24221
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.3
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): venv
  • Relevant/affected Python packages and their versions: pytest version 5.0.1

Expected behaviour

no “file not found” errors being displayed in the output channel when running tests

Actual behaviour

“file not found” error is displayed in the output channel. In the case below, for some reason the “tests” folder is being ignored in the file path: image

Steps to reproduce:

  1. Open a folder with tests following this structure: image

  2. Have at least one of the tests methods fail

  3. Make sure you have a pytest.ini file in the tests folder (in this case I only had [pytest] addopts = -s -v in it)

  4. Reload the window and run all tests. Watch the following error appear: image

Logs

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

python C:\Users\luabud\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\testing_tools\run_adapter.py discover pytest -- -s --cache-clear tests
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-4.6.3, py-1.8.0, pluggy-0.12.0 -- C:\Users\luabud\AppData\Local\Programs\Python\Python37\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('c:\\Users\\luabud\\Apps\\vscode\\.hypothesis\\examples')
rootdir: c:\Users\luabud\Apps\vscode\tests, inifile: pytest.ini
plugins: hypothesis-4.24.4
collecting ... collected 8 items

tests\one\test_a.py::test_a_1 PASSED
tests\one\test_a.py::test_a_2 PASSED
tests\one\test_b.py::test_b_1 PASSED
tests\one\test_b.py::test_b_2 PASSED
tests\two\test_c.py::test_c_1 PASSED
tests\two\test_c.py::test_c_2 PASSED
tests\two\test_d.py::test_d_1 PASSED
tests\two\test_d.py::test_d_2 FAILED

================================== FAILURES ===================================
__________________________________ test_d_2 ___________________________________

    def test_d_2():
>       assert 0 == 1
E       assert 0 == 1
E         -0
E         +1

tests\two\test_d.py:6: AssertionError
- generated xml file: C:\Users\luabud\AppData\Local\Temp\tmp-15700c0uP9mjmCb59.xml -
===================== 1 failed, 7 passed in 0.08 seconds ======================
Error: Error: cannot open file:///c%3A/Users/luabud/Apps/vscode/two/test_d.py. Detail: Unable to read file (Error: File not found (c:\Users\luabud\Apps\vscode\two\test_d.py))

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

image

It turns out this is the defined behavior for pytest. It will walk up the directory tree, starting at the given directory/file, and set the first ancestor directory with a pytest.ini file as the root. That is exactly what everyone is seeing. The problem is that the extension isn’t coping well with this behavior. It’s going to take some cleverness to get this right.

fwiw, I was seeing the issue https://github.com/microsoft/vscode-python/issues/6990 but then i downgraded to pytest 5.01 and that issue went away. but i still have this issue (file not found on all tests i execute).