VSCode pytest test discovery fails

后端 未结 10 667
南方客
南方客 2020-12-15 16:25

Pytest test discovery is failing. The UI states:

Test discovery error, please check the configuration settings for the tests

The output window s

相关标签:
10条回答
  • 2020-12-15 16:59

    Seems like a bug in the latest version of VS Code Python extension. I had the same issue, then I downgraded the Python extension to 2019.3.6558 and then it works again. So we should go to our VS Code extensions list, select the Python extension and "Install another version..." from the setting of that extension.

    I hope this works for you too.

    0 讨论(0)
  • 2020-12-15 17:03

    I resolved the issue by upgrading pytest to the latest version: 4.4.1 with "pip install --upgrade pytest". I was apparently running an old version 3.4.2

    0 讨论(0)
  • 2020-12-15 17:05

    Looking at https://docs.pytest.org/en/latest/usage.html#possible-exit-codes it seems pytest itself is falling over do to some internal error.

    0 讨论(0)
  • 2020-12-15 17:12

    I spent ages trying to decipher this unhelpful error after creating a test that had import errors. Verify that your test suite can actually be executed before doing any deeper troubleshooting.

    pytest --collect-only is your friend.

    0 讨论(0)
  • 2020-12-15 17:12

    In my case the problem with vscode being unable to discover tests was the coverage module being enabled in the setup.cfg (alternatively this could be a pytest.ini), i.e.

    addopts= --cov <path> -ra
    

    which caused the test discovery to fail due to low coverage. The solution was to remove that line from the config file.

    Also, as suggested in the documentation:

    You can also configure testing manually by setting one and only one of the following settings to true: python.testing.unittestEnabled, python.testing.pytestEnabled, and python.testing.nosetestsEnabled.

    0 讨论(0)
  • 2020-12-15 17:18

    This is not a complete answer as I do not know why this is happening and may not relate to your problem, depending how you have your tests structured.

    I resolved this issue by putting an __init__.py file in my tests folder

    E.G.:

    
    ├───.vscode
    │       settings.json
    │
    ├───app
    │       myapp.py
    │
    └───tests
            test_myapp.py
            __init__.py
    

    this was working a few days ago without this but the python extension was recently updated. I am not sure if this is the intended behavior or a side effect of how discoveries are now being made

    https://github.com/Microsoft/vscode-python/blob/master/CHANGELOG.md
    Use Python code for discovery of tests when using pytest. (#4795)

    0 讨论(0)
提交回复
热议问题