Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a sub-folder as a directory containing tests in Python extension for Visual studio code

I am using VScode with python code and I have a folder with sub-directories (2-levels deep) containing python tests.

When I try "Python: Discover Tests" it asks for a test framework (selected pytest) and the directory in which tests exist. At this option, it shows only the top-level directories and does not allow to select a sub-directory.

I tried to type the directory path but it does not accept it.

Can someone please help on how to achieve this?

like image 490
Sasidhar Kasturi Avatar asked Sep 06 '25 06:09

Sasidhar Kasturi


1 Answers

I solved it by putting this in my settings.json:

"python.testing.cwd": "${workspaceFolder}/my-project/all-tests/unit-tests"

In the GUI, the setting name is "Python > Testing: Cwd".

If you then get the error No file or directory "unit-tests", try this:

  1. Open the test configuration dialogue (Ctrl/Cmd + Shift + P -> "Python: Configure Tests")
  2. Select pytest or unittest
  3. Select . (Root Directory) - Earlier I had selected the unit-tests directory instead

If it still isn't working, you might be running into this open bug with ${workspaceFolder}.

If it still isn't working, open up the "Output" window and select "Python" to see more info.

Also, this conversation was helpful: https://github.com/microsoft/vscode-python/issues/11587

like image 172
Dominus.Vobiscum Avatar answered Sep 07 '25 18:09

Dominus.Vobiscum