前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >软件测试/测试开发/全日制|pytest如何标记测试用例

软件测试/测试开发/全日制|pytest如何标记测试用例

原创
作者头像
霍格沃兹测试开发Muller老师
发布2024-01-10 18:59:22
1560
发布2024-01-10 18:59:22
举报

前言

在pytest中,有时候我们并不需要对所有的用例全部执行。pytest提供了一种机制:有选择的挑选用例去执行,即标记测试函数。下面详细介绍几种方法给函数标记。

使用pytest.mark在函数上进行标记

标记格式

@表示这是一个装饰器,pytest.mark是pytest固定的写法,mark_name可以使用自定义标记和内置标记。如下:

代码语言:python
复制
@pytest.mark.mark_name

常用内置标记

示例如下:

代码语言:python
复制
import pytest

def test_01():
    print('hello')

@pytest.mark.skip()
def test_add():
    print('happy')

def test_02():
    print('fun')

if __name__ == '__main__':
    pytest.main(['-s', '-v','test_02.py'])

执行结果如下:

代码语言:python
复制
============================= test session starts =============================
collecting ... collected 3 items

test_02.py::test_01 PASSED                                                 [ 33%]hello

test_02.py::test_add SKIPPED (unconditional skip)                          [ 66%]
Skipped: unconditional skip

tes_t02.py::test_02 PASSED                                                 [100%]fun


======================== 2 passed, 1 skipped in 0.02s =========================

自定义标记

注册标签名

通过pytest.ini配置文件注册标签,格式如下:

代码语言:python
复制
[pytest] # 固定的section名

markers= # 固定的option名称

  标签名1: 标签名的说明内容。

  标签名2

  标签名N

在测试用例/测试类中给用例打标记(只能使用已注册的标记名)

在测试用例的前面加上:@pytest.mark.已注册标签名。运行时,根据用例标签过滤(-m 标签名)。

示例如下:

代码语言:python
复制
import pytest

def test_01():
    print('hello')

@pytest.mark.do
def test_add():
    print('happy')

def test_02():
    print('fun')

再创建pytest.ini文件,不需要同目录,内容如下:

代码语言:python
复制
[pytest]
markers =
    do: do
    undo: undo

注:Windows系统中pytest.ini文件不可添加注释,否则将会报错。

命令行执行,命令及输出如下:

代码语言:python
复制
pytest -s -v -m do test_02.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\my_selenium_project\testcases\pytest, configfile: pytest.ini
collected 3 items / 2 deselected / 1 selected                                                                                                                        

tes_t02.py::test_add happy
PASSED

================================================================== 1 passed, 2 deselected in 0.02s ==================================================================

conftest.py中定义钩子函数

示例如下:

代码语言:python
复制
def pytest_configure(config):
    marker_list = [
        "smoke: marks test as smoke",
        "login",
        "order: 下单场景"
    ]
    for marker in marker_list:
        config.addinivalue_line("markers", marker)

注:使用该方法需注意定义的格式,不能轻易修改函数名及入参。

使用示例:

代码语言:python
复制
import pytest
 
# 标记测试函数
@pytest.mark.smoke
def test_01():
    print("执行test_01")
 
def test_02():
    print("执行test_02")
 
    
# 标记测试类
@pytest.mark.order
class TestOrder:
    
    def test_order(self):
        print("下单")
 
    def test_pay(self):
        print("支付")
 
        
# 多个标签 
@pytest.mark.smoke
@pytest.mark.login
def test_login():
    print("登录")

给测试类打标签,还有另外一种方式,如下:

代码语言:python
复制
# 标记测试类(单个标签)
class TestOrder:
    
    # 给类中的所有测试方法打上order标签
    pytestmark = pytest.mark.order
    
    def test_order(self):
        print("下单")
 
    def test_pay(self):
        print("支付")
    
    
# 标记测试类(多个标签)
class TestOrder:
    
    # 给类中的所有测试方法打上order、smoke标签
    pytestmark = [pytest.mark.order, pytest.mark.smoke]
    
    def test_order(self):
        print("下单")
 
    def test_pay(self):
        print("支付")

执行的时候加上参数-m加标签名即可。

注:在测试模块中直接使用pytest.main()执行当前模块中的被打标签的用例是无效的,我们需要将执行代码分离出来,放在单独的执行模块里面,如放在run.py里,代码如下:

代码语言:python
复制
# run.py
 
import pytest
 
if __name__ == '__main__':
    pytest.main(["-s", "-m", "smoke or order"])

总结

pytest的标记功能让我们能够为测试用例添加元数据,使得测试用例能够更灵活地进行分类和选择性地运行。合理地使用标记,可以提高测试的组织性和可维护性,并且让测试执行更具效率。通过标记,你可以更好地管理和执行测试,提高代码质量和稳定性。希望本文能够帮到大家!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 使用pytest.mark在函数上进行标记
  • 自定义标记
  • conftest.py中定义钩子函数
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档