VSCode pytest test discovery fails

后端 未结 10 668
南方客
南方客 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 17:18

    I just thought I would add my answer here as this might well affect someone who uses a .env file for their project's environment settings since it is such a common configuration for 12 factor apps.

    My example assumes that you're using pipenv for your virtual environment management and that you have a .env file at the project's root directory.

    My vscode workspace settings json file looks like below. The crucial line for me here was "python.envFile": "${workspaceFolder}/.env",

    {
        "python.pythonPath": ".venv/bin/python",
        "python.linting.enabled": true,
        "python.linting.pylintEnabled": true,
        "python.linting.pycodestyleEnabled": false,
        "python.linting.flake8Enabled": false,
        "python.linting.pylintPath": ".venv/bin/pylint",
        "python.linting.pylintArgs": [
            "--load-plugins=pylint_django",
        ],
        "python.formatting.provider": "black",
        "python.formatting.blackArgs": [
            "--line-length",
            "100"
        ],
        "python.testing.unittestEnabled": false,
        "python.testing.nosetestsEnabled": false,
        "python.testing.pytestEnabled": true,
        "python.testing.pytestPath": ".venv/bin/pytest",
        "python.envFile": "${workspaceFolder}/.env",
        "python.testing.pytestArgs": [
            "--no-cov"
        ],
    }
    

    I hope this saves someone the time I spent figuring this out.

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

    Before begin the tests discovery, check that python.testing.cwd points correctly to your tests dir and your python.testing.pytestEnabled is set to true.

    Once those requirements are set correctly, run tests discovery and its output (see OUTPUT window). You should see something like this:

    python $HOME/.vscode/extensions/ms-python.python-$VERSION/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir $ROOT_DIR_OF_YOUR_PROJECT -s --cache-clear
    Test Discovery failed: 
    Error: ============================= test session starts ==============================
    <SETTINGS RELATED TO YOUR MACHINE, PYTHON VERSION, PYTEST VERSION,...>
    collected N items / M errors
    ...
    

    It's important to highlight here the last line: collected N items / M errors. The following lines will contain info about the tests discovered by pytest. So your tests are discoverable but there are errors related to their correct execution.

    The following lines will contain the errors which in most of the cases will be related to an incorrect import.

    Check that all your dependencies have been downloaded previously. If you are working with specific versions of dependencies (on therequirements.txt file you have something like your_package == X.Y.Z) be sure that it's the right version that you need to.

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

    If you are having trouble with pytest I was struggling with the discovery test part.

    Reading some open issue (other) in vscode I found a workaround using the test-adapter extension

    market_place_link

    The extention works like a charm. (and solve my discovery problem)

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

    I had this same issue and tracked it down to my pytest.ini file. Removing most of the addopts from that file fixed the issue.

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