VS Code not finding pytest tests
Asked Answered
M

13

30

I have PyTest setup in vs-code but none of the tests are being found even though running pytest from the command line works fine.

(I'm developing a Django app on Win10 using MiniConda and a Python 3.6.6 virtual env. VS Code is fully updated and I have the Python and Debugger for Chrome extensions installed)

Pytest.ini:

[pytest]
DJANGO_SETTINGS_MODULE = callsign.settings
python_files = tests.py test_*.py *_tests.py

vs-code workspace settings:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "python.pythonPath": "C:\\ProgramData\\Miniconda3\\envs\\callsign\\python.exe",
        "python.unitTest.unittestEnabled": false,
        "python.unitTest.nosetestsEnabled": false,
        "python.unitTest.pyTestEnabled": true,
        "python.unitTest.pyTestArgs": ["--rootdir=.\\callsign", "--verbose"]
    }
}

Finally, the output from the Python Test Log inside VS code:

============================= test session starts =============================
platform win32 -- Python 3.6.6, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
Django settings: callsign.settings (from ini file)
rootdir: c:\Users\benhe\Projects\CallsignCopilot\callsign, inifile: pytest.ini
plugins: django-3.4.5
collected 23 items
<Package c:\Users\benhe\Projects\CallsignCopilot\callsign\transcription>
  <Module test_utils.py>
    <Function test_n_digits>
    <Function test_n_alpha>
    <Function test_n_hex>
    <Function test_n_digits_in_range>
    <Function test_v1_audiofilename>
    <Function test_v2_audiofilename>
    <Function test_v1_bad_int_filename>
    <Function test_v1_bad_non_int_filename>
    <Function test_bad_format>
    <Function test_no_format>
    <Function test_too_many_segments>
    <Function test_too_few_segments>
    <Function test_good_v2_filename>
    <Function test_bad_year_v2_filename>
    <Function test_bad_month_v2_filename>
    <Function test_bad_day_v2_filename>
    <Function test_bad_date_v2_filename>
    <Function test_bad_short_serial_v2_filename>
    <Function test_bad_long_serial_v2_filename>
    <Function test_good_v3_filename>
    <Function test_good_lowercase_block_v3_filename>
    <Function test_bad_non_alpha_block_v3_filename>
    <Function test_real_filenames>

======================== no tests ran in 1.12 seconds =========================

Am I missing any steps to get vs-code to find the tests?

Monoplegia answered 27/1, 2019 at 11:4 Comment(0)
O
28

If anyone comes across this post-2020, this issue in the vscode-python repo saved my life. Basically, just do the following:

  1. Uninstall the Python extension
  2. Delete the file that contains the extension from your ~/.vscode folder (mine looked like ms-python.python-[YEAR].[MONTH].[VERSION])
  3. Reinstall the extension

Worked like a charm.

Oversexed answered 2/4, 2020 at 9:57 Comment(6)
@Peter Did you reload the VSCode window after uninstalling/deleting and again after reinstalling?Oversexed
Still worked for me in 2022!Toritorie
Yes...Worked for me too in 2022Justiciable
I can't believe this works! wtf, thank youConsort
Dec 23 still the answerDogtired
I had to delete the 3 repository beginning with ms-python... But it works perfectly !!Laundry
S
12

VScode needs to specify python configure:

  1. Open Command Palette by Ctrl + shift + p OR command + shift + p for mac users.
  2. and write this >python: configure tests And follow the program directions

Note: You may need (Mostly you will not need) to do this first before following the previous steps

Stapleton answered 16/12, 2021 at 12:50 Comment(0)
F
8

My Python plugin and Test Explorer works just fine.

In my case naming the class without test_.py was the issue. In other words, naming the test file without it starting with "test_", not "tests_", makes it so the explorer does not see the issue. Ex: test_.py works but tests_.py, 12345abcde.py don't work.

Financial answered 20/4, 2020 at 18:53 Comment(1)
Ugh, finally, something that worked. I usually test_, but had just fat fingered it. Thanks!!!Inimical
D
7

Another thing to check, if vscode fails to discover the tests, is to make sure it doesn't do so because of the coverage module being enabled. In my case the test cases were being discovered correctly but the discovery eventually kept failing due to low test coverage, as described here. So first, make sure that the tests could actually be collected by pytest as suggested here:

pytest --collect-only

and then make sure you're not forcing coverage check (e.g. in setup.cfg) with e.g.

addopts= --cov <path> -ra
Ducal answered 4/6, 2020 at 14:36 Comment(3)
You sir, saved me lots of hours! Thanks!Gothard
I had a syntax error - "expected indentation" or similar (I had a dangling if __name__ == "__main__": statement). The error didn't seem to show up until I ran pytest --collect-only (though maybe it would have if I just did pytest instead of clicking on the VS Code buttons). Thank you!Phelgon
For me it was mypy set in pytest.ini killing the test discovery. Unfortunately there is no option to disable mypy (github.com/realpython/pytest-mypy/issues/138) and I had to use github.com/jaraco/pytest-enablerBodiless
B
5

In September 2021, I was able to get VS code to find the test directory again by downgrading the VS Code Python extension from version 2021.9.1191016588 to version v2021.8.1159798656. To downgrade, right-click the extension and click "Install another version..."

Biocatalyst answered 2/9, 2021 at 22:9 Comment(3)
This worked for me on the latest version of visual studio.Procurable
Worked for me too, was on now-latest v2021.10.1336267007. Thanks! (No idea what the cause is, can't see any error logged anywhere with that newer extension version, just acts as if the project has no tests while pytest command line discovers and runs the tests just fine.)Rorrys
This works for me today 06/02/2023. I reinstalled the Python extension to a version of 2 weeks ago.Custom
A
4

EDIT: I downgraded to Pytest 4.0.1 after reading issue 3911 and Test Discovery now works.


Me too. When I blow away .pytest_cache and rerun Python: Discover Unit Tests, I see that the freshly generated .pytest_cache/v/cache/nodeids contains all the tests, but I still get the dialog complaining about No tests discovered.

  • Python 3.7.2
  • macOS 10.13.6
  • VS Code 1.30.2
  • Python Extension 2018.12.1
  • Pytest 4.1.0

.vscode/settings.json:

{
    "python.linting.enabled": false,
    "python.unitTest.unittestEnabled": false,
    "python.unitTest.nosetestsEnabled": false,
    "python.unitTest.pyTestEnabled": true,
    "python.pythonPath": "venv3/bin/python"
}

Tests are in a top-level subdirectory called test. Running pytest manually works.

Accept answered 27/1, 2019 at 23:50 Comment(2)
Do the tests show up in the Test Explorer for you after rerunning Python: Discover Tests? I still don't see anything after downgrading.Yun
Update for 9th of october: you should be using: json "python.testing.unittestEnabled": false, "python.testing.nosetestsEnabled": false, "python.testing.pytestEnabled": true, Ayah
S
4

An obvious thing that caught me...

The test folder hierarchy needs __init__.py files in each folder.

I refactored my tests introducing an extra folder layer and forgot to add __init__.py files.
Command line didn't complain but vscode showed no tests found

Surtax answered 31/3, 2022 at 12:10 Comment(0)
E
3

The following steps worked for me (assuming latest PyTest installed in Visual Studio Code):

  1. Make sure there are no errors (bottom left of Visual Studio Code) enter image description here
  2. In Visual Studio Code from File menu choose: File > Close Folder (i.e. close current project folder) and close Visual Studio Code.
  3. Re-open Visual Studio Code and choose: File > Open Folder (re-open the folder that contains your project/test files).
  4. Choose Testing and should have visibility of tests from here ...

enter image description here

Enclasp answered 15/11, 2021 at 14:1 Comment(0)
C
2

In my case the problem was that I had a syntax error in a source file which caused the pytest discovery to fail. Also, note that you get the traceback from Output (tab) -> Python (dropdown).

Corked answered 22/6, 2022 at 9:22 Comment(0)
S
0

If everything here fails and you checked all the basic configurations, try specifying the path to your test folder in your workspace's settings.ini file like so. The three configurations below it are from the majority of other solutions.

"python.testing.pytestArgs": [
    "${workspaceFolder}/tests"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
Saluki answered 22/11, 2022 at 2:19 Comment(0)
A
0

I changed

"python.testing.pytestEnabled": true

on .vscode to false and reload VScode, and them before trying to run the tests selected the proper python interpreted by using command pallete on python: select interpreter, and it worked properly.

So was pretty much wrong interpreted selected at VScode at the time I tried to run the tests on the extension.

Also, I'm using a conda environment, which may be relevant.

Avow answered 16/6, 2023 at 1:51 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Middleman
T
0

I closed the file and opened the parent file of the one I was using on vscode and it worked.

Thibeault answered 13/6, 2024 at 17:8 Comment(0)
S
0

In my case, I got this problem because I mess up with the package names

mystudy
|- study01
   |- service <-- problem here
      |- customer_service.py
      |- test_customer_service.py
|- study02
   |- service <-- problem here
      |- user_service.py
      |- test_user_service.py

it makes python confuses with the name service and cannot find the test.

To fix that, I rename one folder to other name. For example

mystudy
|- study01
   |- service01
      |- customer_service.py
      |- test_customer_service.py
|- study02
   |- service
      |- user_service.py
      |- test_user_service.py

and it can find the test.

And about the settings.json, I delete it and follow this answer, it helps me to regenerate the file. Please consider to backup the file if you have too many settings. I am just a newbie learning Python and my settings is quite simple.

Last but not least, I also run export PYTHONPATH=$(pwd) at mystudy

Strapping answered 10/7, 2024 at 14:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.