代码之家  ›  专栏  ›  技术社区  ›  devlife

VSCode pytest测试发现失败

  •  0
  • devlife  · 技术社区  · 7 年前

    Pytest测试发现失败。UI声明:

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

    Test Discovery failed: 
    Error: Traceback (most recent call last):
      File "C:\Users\mikep\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\run_adapter.py", line 16, in <module>
        main(tool, cmd, subargs, toolargs)
      File "C:\Users\mikep\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\adapter\__main__.py", line 90, in main
        parents, result = run(toolargs, **subargs)
      File "C:\Users\mikep\.vscode\extensions\ms-python.python-2019.4.11987\pythonFiles\testing_tools\adapter\pytest.py", line 43, in discover
        raise Exception('pytest discovery failed (exit code {})'.format(ec))
    Exception: pytest discovery failed (exit code 3)
    

    以下是我的设置:

    {
        "python.pythonPath": ".venv\\Scripts\\python.exe",
        "python.testing.pyTestArgs": [
            "tests"
        ],
        "python.testing.unittestEnabled": false,
        "python.testing.nosetestsEnabled": false,
        "python.testing.pyTestEnabled": true
    }
    

    我可以从命令行FWIW成功运行pytest。

    0 回复  |  直到 7 年前
        1
  •  7
  •   helgso    7 年前

    我通过使用“pip install--upgrade pytest”将pytest升级到最新版本:4.4.1,从而解决了这个问题。我显然在运行一个旧版本3.4.2

        2
  •  12
  •   Autumn    6 年前

    在创建了一个包含导入错误的测试之后,我花了很长时间试图破译这个毫无帮助的错误。在进行任何更深入的故障排除之前,请验证您的测试套件是否可以实际执行。

    pytest --collect-only 是你的朋友。

        3
  •  8
  •   Niklas Rosencrantz    7 年前

    似乎是最新版本的VS代码Python扩展中的一个bug。我遇到了同样的问题,然后将Python扩展降级为 2019.3.6558号

        4
  •  7
  •   WildStriker    7 年前

    这不是一个完整的答案,因为我不知道为什么会发生这种情况,也可能与你的问题无关,这取决于你的测试结构。

    我把一个 __init__.py “我的测试”文件夹中的文件

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

    几天前还没有这个功能,但是python扩展最近更新了。我不确定这是有意的行为还是现在发现的副作用

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

        5
  •  2
  •   Wayne Lambert    6 年前

    我只是想在这里加上我的答案,因为这可能会影响使用 .env

    pipenv 对于您的虚拟环境管理 .env文件

    我的vscode工作区设置json文件如下所示。对我来说最重要的是 "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"
        ],
    }
    

    我希望这能节省我花在解决这个问题上的时间。

        6
  •  1
  •   Tomasz Bartkowiak    5 年前

    在我的例子中,vscode无法发现测试的问题是 coverage 在中启用的模块 setup.cfg (或者这可能是 pytest.ini

    addopts= --cov <path> -ra
    

    另外,正如 documentation :

    也可以手动配置测试 将以下设置中的一个设置为true: python.testing.unittestEnabled python.testing.pytestEnabled ,和 python.testing.nosetestsEnabled .

        7
  •  0
  •   Brett Cannon    7 年前

    看着 https://docs.pytest.org/en/latest/usage.html#possible-exit-codes 似乎pytest本身正在为一些内部错误而崩溃。

        8
  •  0
  •   Brendan Abel    6 年前

    pytest.ini 文件。移除大部分 addopts 从那个文件中修复了这个问题。

        9
  •  0
  •   cpinamtz    6 年前

    在开始测试发现之前,请检查 python.testing.cwd 正确指向测试目录和 python.testing.pytestEnabled 设置为 true .

    一旦正确设置了这些需求,请运行测试发现及其输出(请参阅 输出 窗口)。你应该看到这样的画面:

    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
    ...
    

    在这里强调最后一行很重要: collected N items / M errors . 以下几行将包含有关由发现的测试的信息 pytest 但也有一些与正确执行有关的错误。

    以下几行将包含 大多数情况都与错误的导入有关 .

    请检查您的所有依赖项是否已在以前下载。如果您正在使用依赖项的特定版本(在 requirements.txt your_package == X.Y.Z