Pytest 分组测试

有时候需要针对不同的测试环境跑不同的测试用例,如:冒烟测试、sit、uat、prd,所以给自动化测试用例做标记分组是很有必要的,pytest.mark 可以轻松实现这个功能。首先需要注册自定义标记。

通过使用pytest.mark帮助程序,可以轻松地设置测试函数的元数据。可以在API参考中找到内置标记的完整列表。或者,您可以使用CLI-pytest-标记列出所有标记,包括内置标记和自定义标记。

以下是一些内置标记:

usefixtures-在测试函数或类上使用fixtures

filterwarnings-筛选测试函数的某些警告

skip-始终跳过测试函数

skipif-如果满足特定条件,则跳过测试函数

xfail-如果满足特定条件,则产生“预期失败”结果

parameterize-对同一测试函数执行多个调用。

创建自定义标记或将标记应用于整个测试类或模块都很容易。这些标记可以由插件使用,也通常用于通过-m选项在命令行上选择测试。

注册marks
有3中方法注册marks,第一种事在pytest.ini文件

通过pytest.ini文件注册自定义标记
如下:

[pytest]
markers =
    sit: 标记测试为sit (deselect with '-m "not sit"')
    uat: 标记测试为uat
    prd: 标记测试为prd 
    serial

选择分组

-m + 分组标记如:

-m 'sit'

排除分组

m not + 分组,如:

-m 'not sit'

通过 pyproject.toml文件注册自定义标记

[tool.pytest.ini_options]
markers = [
    "sit: 标记测试为sit (deselect with '-m \"not sit\"')",
    "serial",
]

注意标记名:后面的为可选的标记描述

通过pytest_configure hook动态注册标记

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "env(name): mark test to run only on named environment"
    )

给未知标记抛出异常

使用@pytest.mark.name_of_the_mark装饰器应用的未注册标记将始终发出警告,以避免由于键入错误的名称而导致的意外行为。可以通过在pytest.ini文件中注册自定义标记或使用自定义pytest_configure钩子来禁用自定义标记的警告。

当传递--strict marks命令行标志时,使用@pytest.mark.name_of_the_mark装饰器应用的任何未知标记都将触发错误。您可以在项目中通过向addopts添加--strict标记来强制执行此验证:

[pytest]
addopts = --strict-markers
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    serial

标记测试功能并选择它们进行运行
可以将测试运行限制为仅运行标记为sit的测试

pytest -v -s -m  sit test_sample.py

输出结果:

  或者相反,运行除sit之外的所有测试:

 pytest -v -s -m "not sit" test_sample.py

输出结果: 

  选择一个分组和不选中另一个分组:

 pytest -v -s -m "sit and not uat" test_sample.py

输出结果如下:

 代码中使用自定义标记

import pytest
 
 
class TestPractise:
 
    @pytest.mark.sit
    def test_sit(self):
        print('sit环境测试')
 
    @pytest.mark.uat
    def test_uat(self):
        print('uat环境测试')
 
 
if __name__ == "__main__":
    pytest.main(["-v", "-s", "-m sit and not uat"])

以上测试选择sit标记的测试,不选中uat标记的测试

输出如下:

从图片看出运行结果与期望一致~

标记整个类或模块

import pytest
@pytest.mark.sit
class TestPractise:
 
    def test_sit(self):
        print('sit环境测试')
 
    def test_uat(self):
        print('uat环境测试')

这相当于将decorator直接应用于两个测试函数。要在模块级别应用标记,请使用pytestmark全局变量:

import pytest
pytestmark = pytest.mark.sit


或多个标记:

pytestmark = [pytest.mark.sit, pytest.mark.uat]

由于遗留的原因,在引入类装饰器之前,可以在测试类上设置pytestmark属性,如下所示:

import pytest
 
 
class TestClass:
    pytestmark = pytest.mark.sit

注意:

标记只能应用于测试,对fixture没有影响。
————————————————
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
借鉴与:https://blog.csdn.net/TalorSwfit20111208/article/details/131134921

猜你喜欢

转载自blog.csdn.net/asuf1364/article/details/131316182
今日推荐