Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code not finding pytest tests

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?

like image 449
Ben Avatar asked Jan 27 '19 11:01

Ben


People also ask

How do I enable pytest in Visual Studio?

Open a Python project. Once the project is loaded in Visual Studio, right-click your project in Solution Explorer and select the unittest or pytest framework from the Properties Test tab. If you use the pytest framework, you can specify test location and filename patterns using the standard pytest .

Where do I put the pytest test?

While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.

What is the difference between VS Code and pytest?

VS Code displays test output in the Python Test Log panel, including errors caused when a test framework is not installed. With pytest, failed tests also appear in the Problems panel. (If you're already familiar with unit testing, you can skip to the walkthroughs .)

Can you test Python code in Visual Studio Code?

Python testing in Visual Studio Code The Python extension supports testing with Python's built-in unittest framework as well as pytest. Nose is also supported, although the framework itself is in maintenance mode.

Why is unittest not working in VSCode?

It is because some of the imports in the test is not discoverable. when running python -m unittest -h, the last line of the output is For test discovery all test modules must be importable from the top level directory of the project. it is likely that VSCode is running the command without the right PYTHONPATH and other environment variables.

How to open pytest files in Visual Studio Code?

The following steps worked for me (assuming latest PyTest installed in Visual Studio Code): In Visual Studio Code from File menu choose: File > Close Folder (i.e. close current project folder) and close Visual Studio Code. Re-open Visual Studio Code and choose: File > Open Folder (re-open the folder that contains your project/test files).


Video Answer


2 Answers

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.

like image 189
Arcsector Avatar answered Oct 21 '22 23:10

Arcsector


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.

like image 4
George V. Reilly Avatar answered Oct 21 '22 22:10

George V. Reilly