Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vscode-python runs pytest in wrong directory such that relative paths get confused

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?

like image 995
r-beer Avatar asked Dec 30 '25 11:12

r-beer


1 Answers

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]
like image 184
GJoe Avatar answered Jan 02 '26 08:01

GJoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!