question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

python.testing.cwd ignored when discovering or running tests

See original GitHub issue

Environment data

  • VS Code version: 1.40.1
  • Extension version (available under the Extensions sidebar): 2019.11.49689
  • OS and version: MacOS Catalina 10.15.1
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.4
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
  • Relevant/affected Python packages and their versions: pytest==5.3.0
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info #3977): Jedi

Expected behaviour

Tests are displayed in the test explorer according to python.testing.cwd setting and I am able to run single test or tests from single file

Actual behaviour

python.testing.cwd is ignored for displaying tests and I am unable to run single test or tests from single file because I am getting an error file not found.

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

Sample project: https://github.com/pbotsman/sample_project

  1. Open project
  2. Open tests/py/some_test.py to start exploring tests
  3. Try to execute single test test_something or tests in some_test.py file.
  4. Get the error ERROR: file not found: ./tests/py/some_test.py::test_something (Sometimes in order to reproduce the problem I had to relaunch my VSCode)

Logs

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

> ~/.virtualenvs/sample_project/bin/python -m pytest --rootdir ~/projects/sample_project --junitxml=/var/folders/0_/6w94lk1171vc_fpwbjqx02nw0000gn/T/tmp-4746yxdv7Lw3muwB.xml -v -s --log-cli-level INFO ./tests/py/some_test.py::test_something
cwd: ~/projects/sample_project/tests/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)

console.ts:137 [Extension Host] Info Python Extension: 2019-11-20 14:06:31: getActivatedEnvironmentVariables, Class name = b, completed in 1ms, Arg 1: <Uri:/Users/pavel/projects/sample_project>, Arg 2: undefined, Arg 3: undefined
console.ts:137 [Extension Host] Info Python Extension: 2019-11-20 14:06:31: > ~/.virtualenvs/sample_project/bin/python -m pytest --rootdir ~/projects/sample_project --junitxml=/var/folders/0_/6w94lk1171vc_fpwbjqx02nw0000gn/T/tmp-5598WI4lpp4kFEsC.xml -v -s --log-cli-level INFO ./tests/py/some_test.py::test_something
console.ts:137 [Extension Host] Info Python Extension: 2019-11-20 14:06:31: cwd: ~/projects/sample_project/tests/py

Additional info

image

Looks like something has changed in the last release:

  1. Previously tests would be displayed in the test explorer just as some_test.py would be root test. I mean that tests/py would be omitted in the test explorer since I have "python.testing.cwd": "tests/py" in my settings. Currently directories tests/py are displayed in the test explorer even though I have this setting.
  2. I can’t run single test because I am always getting an error file not found. It looks like something is wrong with the file paths. When the command ~/.virtualenvs/sample_project/bin/python -m pytest --rootdir ~/projects/sample_project --junitxml=/var/folders/0_/6w94lk1171vc_fpwbjqx02nw0000gn/T/tmp-5598WI4lpp4kFEsC.xml -v -s --log-cli-level INFO ./tests/py/some_test.py::test_something is called my current working directory is set to python.testing.cwd and that is why I am getting the error. So in the command mentioned above valid file path should be ./some_test.py::test_something, not ./tests/py/some_test.py::test_something.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:66
  • Comments:37 (9 by maintainers)

github_iconTop GitHub Comments

12reactions
danstreetercommented, Oct 20, 2020

Disclaimer: I have about zero days experience with Typescript, and the vscode-python extension… however I think the issue might have stemmed from this commit: “Force the pytest root dir to always be the workspace root.” https://github.com/microsoft/vscode-python/commit/9fe2a76322236bf752fe36e4e4230b2d2036ddff

11reactions
kimadelinecommented, Nov 22, 2019

Hi @pbotsman 👋 Thank you for reaching out and thoroughly filling out the issue template 👍

I can confirm that I could reproduce your issue in the latest release of the extension (2019.11.49689), and that it doesn’t happen in the previous release (2019.10.44104).

Seems like we don’t take python.testing.cwd into account anymore when discovering tests, or running single tests using pytest.

A possible workaround while waiting for a fix would be to move the content of python.testing.cwd in the list of pytest arguments, let me know if the following works for you:

{
    "python.pythonPath": "/path/to/python",
    "python.testing.pytestArgs": [        
        "./tests/py",
        "-v",
        "-s",
        "--log-cli-level", "INFO",
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

vscode-python runs pytest in wrong directory such that relative ...
In the 'Settings' pane, search for 'python.testing.cwd'. The docs say that 'cwd' 'Specifies an optional working directory for unit tests.'.
Read more >
Testing Python in Visual Studio Code
Testing Python in Visual Studio Code including the Test Explorer. ... To run all discovered tests, select the play button at the top...
Read more >
unittest — Unit testing framework — Python 3.11.1 ...
If setUp() succeeded, tearDown() will be run whether the test method succeeded or not. Such a working environment for the testing code is...
Read more >
VS Code Python Tool Now Debugs Jupyter Notebooks
python.testing.cwd setting is no longer ignored when discovering or running tests. (#8678); Upgraded to Jedi 0.18 and enabled it behind the ...
Read more >
Changing standard (Python) test discovery - Pytest
The --ignore-glob option allows to ignore test file paths based on Unix shell-style wildcards. If you want to exclude test-modules that end with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found