Pytest:如何发现以不同前缀开头的文件名?

2024-06-26 15:00:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用VS代码+Python测试资源管理器扩展

目前,只有命名约定为test_<somefilename>.py的测试文件被自动发现

是否有可以更改的设置,以便它可以检查以不同前缀开头的文件,例如moduletest_<somefilename>.py

我真的不想更改这些文件的文件名

谢谢


Tags: 文件代码pytest文件名命名资源管理vs
1条回答
网友
1楼 · 发布于 2024-06-26 15:00:38

以下是我的解决方案:

  1. 在根目录中创建pytest.ini,测试位于root/tests。在pytest.ini中,添加以下配置:

     [pytest]
     testpaths= tests
     python_files = moduletest_*.py
    
  2. 在Settings.json中,添加

    "python.testing.pytestArgs": [
        "-c",
        "pytest.ini"
     ],
     "python.testing.unittestEnabled": false,
     "python.testing.pytestEnabled": true,
    

然后打开测试资源管理器,moduletest_*.py应该被检测到并显示在那里:

enter image description here

相关问题 更多 >