feetwet
feetwet

Reputation: 3446

Pytest Discovery Error in VSCode after renaming folder

I have pytest configured and working in VSCode, with automatic test discovery enabled.

But renaming a folder in the project that contains tests breaks both those tests and auto-discovery, instead producing a Pytest Discovery Error. How can I get the Testing to fully refresh – removing tests under the old folder name and adding those in the new name?

Simple steps to reproduce problem:

  1. Under project folder hello is a folder foo containing x.py and test_x.py.
  2. Open the VSCode Testing sidebar and configure to run pytest.
  3. Now the Testing pane shows the test hierarchy hello >> foo >> test_x.py >> test_*
  4. In the VSCode Explorer pane rename foo to bar.
  5. Refresh Testing >> Test Explorer, and/or re-run all tests.

Result:

How can I force the Testing configuration to refresh so that it removes the hello >> foo tests and adds the hello >> bar tests?


Verbose output after renaming foo to bar and refreshing/re-running tests:

Python interpreter path: ~\AppData\Local\Programs\Python\Python39\python.exe
> ~\AppData\Local\Programs\Python\Python39\python.exe -c "import pytest"
> ~\AppData\Local\Programs\Python\Python39\python.exe ~\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir . -s --cache-clear foo
cwd: .
> ~\AppData\Local\Programs\Python\Python39\python.exe ~\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir . -s --cache-clear foo
cwd: .
> ~\AppData\Local\Programs\Python\Python39\python.exe ~\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir . -s --cache-clear foo
cwd: .
> ~\AppData\Local\Programs\Python\Python39\python.exe -m pytest --rootdir . --override-ini junit_family=xunit1 --junit-xml=~\AppData\Local\Temp\tmp-13840MhzPL4CPuKOe.xml ./foo
cwd: .
> ~\AppData\Local\Programs\Python\Python39\python.exe -m pytest --rootdir . --override-ini junit_family=xunit1 --junit-xml=~\AppData\Local\Temp\tmp-13840ITbhhDmK8Ah1.xml
cwd: .
> ~\AppData\Local\Programs\Python\Python39\python.exe ~\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir . -s --cache-clear foo
cwd: .
[ERROR 2022-8-18 12:2:6.129]: Error discovering pytest tests:
 [n [Error]: ============================= test session starts =============================
platform win32 -- Python 3.9.2, pytest-7.1.3, pluggy-1.0.0
rootdir: c:\Users\Me\VSCode\hello
collected 0 items

========================= no tests collected in 0.00s =========================
ERROR: file or directory not found: foo

Upvotes: 4

Views: 817

Answers (1)

Alexander Khomenko
Alexander Khomenko

Reputation: 597

I just had the same issue. I read on some threads the idea just to remove all the cache files (especially __pycache__ and .pytest_cache). See Pytests import fails after renaming project folder for details.

But I figured out where the problem was without removing anything. There is a file settings.json in the .vscode folder. So, it contained an entry python.testing.unittestArgs with the old name of the renamed project folder. After I updated that entry VSCode works as expected again.

So you should try both:

  • Remove **/__pycache__, **/*.pyc, and .pytest_cache
  • Check .vscode/settings.json for outdated config.

I hope that helps!

Upvotes: 3

Related Questions