7

With pytest and vscode-python I would like to run tests which were previously implemented with the unittest framework.

Therefore, I successfully ran the tests using pytest in the respective directory tests.

pytest

I also set up vscode-python and tested almost all tests succesfully.

However, those tests, which load data from the subdirectory tests/data fail, because vscode-python seems to run pytest from another directory than the tests directory tests.

abc/
|-- tests/
    |-- test_function.py
    |-- data/

How can I set up vscode-python, such that all data files from the already implemented tests are read successfully?

r-beer
  • 121
  • 1
  • 1
  • 6

2 Answers2

12

Navigate to "Settings" (in VS Code 1.35.0 on macOS):

Menu 'Code' >> 'Preferences' >> 'Settings'

In the 'Settings' pane, search for 'python.testing.cwd'.

The docs say that 'cwd' 'Specifies an optional working directory for unit tests.'

For your set-up, one way to generate a relative path for 'python.testing.cwd' would be to:

  1. open one of the data files under 'abc/tests/data/' in the VS Code editor
  2. right-click on the open file's tab in the editor and select 'Copy Relative Path' from the pulldown menu
  3. paste that relative path into the 'python.testing.cwd' configuration field (& edit it appropriately before saving)

Alternatively, you may want to investigate specifying a path using one or more VS Code Variables.

References:

  1. [https://code.visualstudio.com/docs/python/unit-testing#_test-configuration-settings]
  2. [https://code.visualstudio.com/docs/editor/variables-reference]
GJoe
  • 185
  • 7
0

In .vscode/settings.json you can put

{
    ...
    "python.testing.cwd": "${workspaceFolder}/tests",
}
jakob-r
  • 6,824
  • 3
  • 29
  • 47