Pytest 不收集嵌套的 Test_* 类

问题描述 投票:0回答:2

我正在使用 pytest 编写一个测试套件来覆盖 API,测试的组织方式如下:

class Test_Endpoint:
    def test_A(self):
        ...
    def test_B(self):
        ...

    class Test_SubEndpoint:
        def test_C(self):
            ...

Pytest 之前已在 Test_SubEndpoint 类中收集并运行测试,但突然停止,并且只会收集和运行不是类函数或根级别测试类成员的测试。以前,test_A、test_B 和 test_C 都被收集,但现在只收集 test_A 和 test_B,并且我不知道我所做的任何更改会导致此情况。

它也不再在 VSCode 的测试查看器中对参数化测试进行分组,但我不确定这是否是 pytest 或 VSCode 的问题。

pytest.ini 文件内容是

[pytest]
addopts = --self-contained-html
filterwarnings =
    ignore::DeprecationWarning
pythonpath = .

并且当这个问题开始发生时没有改变。当我为

python_files
python_classes
python_functions
添加与我的命名约定与 pytest.ini 文件相匹配的显式定义时,它不会影响我的问题。我的文件、类和函数名称符合 pytest 默认命名约定。

扁平化我的测试文件中的类结构确实可以收集所有测试,但它失去了我在开发过程中一直依赖的组织帮助,并且不能修复参数化测试分组的缺乏,所以我真的更喜欢能够保留嵌套结构。

visual-studio-code testing pytest vscode-extensions
2个回答
0
投票

您可以在

pytest
文件中设置
pytest.ini
发现规则:

[pytest]
# Set pytest discovery rules:
# (Most of the rules here are similar to the default rules.)
# (Inheriting unittest.TestCase could override these rules.)
python_files = test_*.py *_test.py *_tests.py *_suite.py
python_classes = Test* *Test* *Test *Tests *Suite
python_functions = test_*

此外,如果不使用 pytest 固定装置,您可以让您的测试类继承

unittest.TestCase
,这会自动使它们可被发现。


0
投票

这篇文章已被编辑,以将其从收集并馈送到人工智能训练的数据中删除。我不同意我的任何数据被用于人工智能训练。

© www.soinside.com 2019 - 2024. All rights reserved.