7

I am trying to get my unit tests to show up in VSCode's test explorer, but it does not seem to be working. The test discovery does not fail with any errors in the output, but also does not show any of my tests.

This is the repo I am working in, you can see the file structure there.

This may or may not have anything to do with me using poetry to manage my virtual environment, though I'm sure the python interpreter is set correctly. I can get my tests to run perfectly well manually by running pytest or poetry run pytest in my base directory. I do have an empty __init__.py in my tests directory. Also of note is that I'm running in Ubuntu on WSL 2.

enter image description here

I set this up environment with the following process:

  • Install WSL2 on Windows
  • Install VSCode on Windows
  • Install the 'Remote - WSL' VSCode extension
  • Open a new WSL Window in VSCode
  • Install Anaconda in Ubuntu using this guide
  • Install the VSCode Python extension
  • Set the VSCode python interpreter to the anaconda install, and open a new terminal
  • pip install poetry
  • Go to my project root folder
  • poetry install
  • Set the VSCode python interpreter to the poetry environment, and open a new terminal
  • Done, I can run pytest via the command line

Here's my workspace setting.json:

{
    "restructuredtext.confPath": "${workspaceFolder}/docs/source",
    "python.testing.pytestEnabled": true,
}

Here's what the output shows when I try to refresh tests:

~/.cache/pypoetry/virtualenvs/monaco-bfS1OgpY-py3.9/bin/python ~/.vscode-server/extensions/ms-python.python-2021.11.1422169775/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/coding/monaco -s --cache-clear
cwd: ~/coding/monaco

If I run the above command manually in the terminal, it prints out a large dict which looks like it describes all my tests. (Below, truncated but it goes on like this for a while).

[{"rootid": ".", "root": "/mnt/c/Users/Scott/Documents/Documents/Coding/monaco", "parents": [{"id": "./tests", "kind": "folder", "name": "tests", "parentid": ".", "relpath": "./tests"}, {"id": "./tests/test_MCCase.py", "kind": "file", "name": "test_MCCase.py", "parentid": "./tests", "relpath": "./tests/test_MCCase.py"}, {"id": "./tests/test_MCSim.py", "kind": "file", "name": "test_MCSim.py", "parentid": "./tests", "relpath": "./tests/test_MCSim.py"}, {"id": "./tests/test_MCSim_io.py", "kind": "file", "name": "test_MCSim_io.py", "parentid": "./tests", "relpath": "./tests/test_MCSim_io.py"}, {"id": "./tests/test_MCVal.py", "kind": "file", "name": "test_MCVal.py", "parentid": "./tests", "relpath": "./tests/test_MCVal.py"},
Scott
  • 504
  • 6
  • 17

2 Answers2

2

I figured this out. It turns out that the only reason pytest was working at all, was because I had installed the package locally and the test imports were keying off the local files via that package installed in editable mode (this happens with poetry install or pip install -e).

I started from a clean python environment, and got pytest working by using the "conftest" answer to this stackoverflow question. My tests show up now!

Edit 2022-01-24: I am running into this issue again, and unfortunately a clean environment is not working any more.

Edit 2023-07-06: I have run into this off and on again, and updating the repo from Python 3.6/3.7 to a newer version usually fixes it. I think it’s a compatibility thing with the Python extension in VS Code.

Scott
  • 504
  • 6
  • 17
0

prima facie it seems that either

  • the directory saved in settings.json is different from your actual working directory or
  • Something wrong with the installation of pytest which the VS code can access, while the one from the terminal seems to be working as you said.

Anyway, to help you we need more info.

  • your folder structure, files list, etc
  • how you installed python, pytest and linked it with VS code
  • output of
> ~/.cache/pypoetry/virtualenvs/myproject-bfS1OgpY-py3.9/bin/python ~/.vscode-server/extensions/ms-python.python-2021.11.1422169775/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/coding/myproject/tests -s --cache-clear .
cwd: ~/coding/myproject/tests

even if it is gibberish

  • any other info you think could help and have forgotten to include here
Tejas Shetty
  • 685
  • 6
  • 30