pytest跳过测试父类中测试方法

        pytest selenium/appium测试用例组织方式有很多种,笔者采用的是一个类启动一次webdriver,并在最后退出webdriver。每个类中指定登录的用户,有些测试是需要覆盖不同的用户,但执行的操作基本相同,就可以提取出父类,在父类中定义测试方法,子类中只需要指定登录的用户,以及必要时重写父类中的测试方法。

        但是按照继承关系写好测试用例后,有一个问题:测试父类的测试方法也被收集了,加上在测试父类中一般不会指定登录用户,会导致测试失败。

        在chatgpt中提问,答案主要有以下几种方法,但实验下来并不都奏效:

方法 1:在 TestBase 中标记 pytest.mark.abstract

这种方法通过使用 pytest 提供的 @pytest.mark.usefixtures@pytest.mark.abstract 来跳过 TestBase 直接被收集为测试类。

代码示例
import pytest

@pytest.mark.abstract  # 标记为抽象测试类,pytest 不会收集这个类的用例
class TestBase:
    def test_base_example(self):
        assert 1 == 1

class TestChild(TestBase):
    def test_child_example(self):
        assert 2 == 2
解释
  • @pytest.mark.abstract 告诉 pytest 不要收集 TestBase 中的测试用例。
  • TestChild 继承了 TestBase 的方法,但 TestBase 本身不会执行。
实验结果

不起作用

方法 2:使用 __test__ = False 来标记 TestBase 不作为测试类

你可以在 TestBase 中使用 __test__ = False 属性,这样 pytest 不会将其作为测试类。

代码示例
class TestBase:
    __test__ = False  # 告诉 pytest 这不是一个测试类
    
    def test_base_example(self):
        assert 1 == 1

class TestChild(TestBase):
    def test_child_example(self):
        assert 2 == 2
解释
  • __test__ = Falsepytest 识别的特殊标志,表示 TestBase 不是一个可收集的测试类。
  • 这样,pytest 不会将 TestBase 中的用例收集为测试用例。
实验结果

父类的测试方法是不收集了,但是子类中的测试方法也不会被收集了

方法 3:通过 conftest.py 过滤掉特定的类

如果你想对多个基类进行这种控制,而不想在每个类中手动设置 __test__ = False,你可以使用 conftest.py 文件,通过钩子 pytest_collection_modifyitems 过滤掉指定的类。

代码示例

conftest.py

def pytest_collection_modifyitems(config, items):
    items[:] = [item for item in items if "TestBase" not in item.nodeid]
解释
  • pytest_collection_modifyitems 钩子允许在测试收集阶段对收集到的测试用例进行过滤。
  • 这里的逻辑会从所有收集的 items 中排除类名为 TestBase 的测试用例。
实验结果

有效。最终采用此方法

方法 4:使用 pytest.mark.skip 标记 TestBase 中的所有方法

如果你不想设置 __test__pytest.mark.abstract,你也可以简单地跳过 TestBase 中的所有用例。

代码示例
import pytest

class TestBase:
    @pytest.mark.skip(reason="这是一个基类,不应该直接运行")
    def test_base_example(self):
        assert 1 == 1

class TestChild(TestBase):
    def test_child_example(self):
        assert 2 == 2
解释
  • @pytest.mark.skipTestBase 中的 test_base_example 用例标记为跳过。
  • 但是,如果 TestChild 继承并重写了这个方法,pytest 仍然会运行它。
  • 如果你有很多方法,这种方式就显得繁琐了。

因此方法需标记的地方太多,不考虑采用此方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值