在Visual Studio Code上,如何为测试发现指定pytest.ini文件

我使用pytest进行测试。我的测试文件位于子目录tests中,它们分别命名为Foo.pyBar.py而不是test_Foo.pyTestFoo.py等。因此,请确保{{ 1}}找到它们,我在项目的根目录中有一个pytest文件,其内容如下:

pytest.ini

如何在Visual Studio Code中指定[pytest] python_files=tests/*py 文件的路径,以便pytest.ini插件可以正确/成功发现我的测试文件?无论我尝试什么,我都会得到vscode-python,没有给出任何理由。

dongjiahui 回答:在Visual Studio Code上,如何为测试发现指定pytest.ini文件

要将VS Code设置为使用特定的pytest.ini文件,您需要执行以下操作:

  1. 在VS Code(ctrl+k > ctrl+o)中打开目录
  2. 选择 Python解释器ctrl+shift+p > Python: Select Interpreter > Python interpreter
  3. 配置您要使用的测试框架,在这种情况下为 PyTest ctrl+shift+p > Python: Configure Tests > Pytest > {pytest rootdir}
  4. 打开在工作目录(您在步骤1中选择的目录)中创建的settings.json目录内生成的.vscode/文件
  5. 在文件中添加以下设置(如果在配置pytest时指定了rootdir,该设置可能已经存在):
    "python.testing.pytestArgs": [
        "-c","/path/to/your/pytest.ini"
    ],

就是这样! VS Code应该使用您在最后一个参数中指定的pytest.ini文件。您可以在此处指定任何CLI选项。

Source

,

Pytest要求测试函数名称以test开头或以test结尾。

ini文件指示py.test将所有* _test.py文件视为单元测试。

本文链接:https://www.f2er.com/2694005.html

大家都在问