首页 技术 正文
技术 2022年11月8日
0 收藏 620 点赞 1,761 浏览 4358 个字

pytest中提供的mark模块,可以实现很多功能,如:

  1. 标记用例,即打标签
  2. skipskipif标记跳过,skip跳过当前用例,skipif符合情况则跳过当前用例
  3. xfail标记为预期失败

标记用例

有时候我们可能并不需要执行项目中的所有用例,而只执行其中的某些用例,即指定执行某一类或某个场景的测试用例,比如只执行冒烟用例,那么这个时候就需要使用@pytest.mark.标签名来进行打标签过滤。标签名需要先注册,然后才能使用。

注册标签

官方文档里提供了三种注册标签的方法,这里只介绍pytest.iniconftest.py,有兴趣的可以去查看官方文档

方法一,在项目根目录新建pytest.ini,并在其中注册、管理标签。示例如下:

[pytest]markers =
smoke: marks test as smoke
login
order: 下单场景

这里定义了三个标签,分别是:smoke、login、order,冒号后面的是标签说明,可不加。

方法二,在conftest.py中定义钩子函数进行标签注册,格式如下:

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

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

使用方法

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("登录")

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

# 标记测试类(单个标签)
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("支付")

同样可以使用pytestmark标记模块,给模块中所有的测试类、测试函数打上标签,如下:

import pytest# 模块中的所有测试函数、测试类都会被打上order、smoke标签
pytestmark = [pytest.mark.order, pytest.mark.smoke]def test_01():
print("执行test_01")def test_02():
print("执行test_02")class TestOrder:
def test_order(self):
print("下单") def test_pay(self):
print("支付")

执行方法

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

命令行

# 执行被标记为smoke的用例
pytest -m smoke# 执行被标记为smoke且被标记为login的用例
pytest -m "smoke and login"# 执行被标记为smoke或login的用例
pytest -m "smoke or login"

代码执行

# 执行被标记为smoke的用例
pytest.main(['-m smoke'])# 执行被标记为smoke或order的用例
pytest.main(["-m", "smoke or order"])# 执行被标记为smoke同时被标记为login的用例
pytest.main(["-m", "smoke and login"])# 执行被标记为smoke且不被标记为login的用例
pytest.main(["-m", "smoke and not login"])

这里需要注意,在测试模块中直接使用pytest.main()执行当前模块中的被打标签的用例是无效的,这样会执行当前模块中的所有测试用例。如下示例:

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("登录")if __name__ == '__main__':
pytest.main(['-s', '-m smoke'])

运行该模块,结果如下:

从结果中可以看出,虽然代码中写了只执行标记为smoke的用例,但所有5条用例都被执行了,不能进行过滤。

我们需要将执行代码分离出来,放在单独的执行模块里面,如放在run.py,代码如下:

# run.pyimport pytestif __name__ == '__main__':
pytest.main(["-s", "-m", "smoke or order"])

运行结果如下:

从结果可以看出来,这里只运行了标记为smokeorder的测试用例。

标记跳过

有时候我们需要跳过某些测试用例不去执行,如代码更新后老的用例不需要执行了,或者在某些特定场景下不需要执行某些用例,这时就需要给对应的测试用例做标记跳过处理。

pytest中提供了两种标记跳过的方法,如下:

  1. 直接跳过,@pytest.mark.skip(reason="跳过原因"),reason可写可不写。
  2. 条件跳过,即满足某个条件则跳过不执行,@pytest.mark.skipif(b>3, reason="跳过原因")

示例如下:

import pytest@pytest.mark.skip(reason="不需要执行test_01")
def test_01():
print("执行test_01")@pytest.mark.skip(2>1, reason="如果2大于1则跳过不执行")
def test_02():
print("执行test_02")if __name__ == '__main__':
pytest.main(['-s'])

运行结果:

从运行结果中可以看到,这2条用例都被跳过。如果想要 跳过测试类 或 测试模块,方法同上面给测试类、测试模块打标签的方法一样,不做过多说明。

xfail

有些场景下需要将测试用例标记为预期失败,如对尚未实现的功能或尚未修复的错误进行测试,使用@pytest.mark.xfail可以将测试用例标记为预期失败。

pytest.mark.xfail(condition=None, reason=None, raises=None, run=True, strict=False),参数说明如下:

  1. condition,预期失败的条件,默认值为None,表示只有满足条件时才标记用例为预期失败。

  2. reason,失败原因,默认值为None,说明标记用例的原因。

  3. strict关键字参数,默认值为False。

    当 strict=False 时,如果用例执行失败,则结果标记为xfail,表示符合预期的失败;如果用例执行成功,结果标记为XPASS,表示不符合预期的成功;

    当strict=True时,如果用例执行成功,结果将标记为failed。

  4. raises关键字参数,默认值为None,可以上报指定的一个或多个异常。如果用例的失败不是因为所期望的异常导致的,pytest将会把测试结果标记为failed。

  5. run关键字参数,默认值为True。当run=False时,pytest不会再执行测试用例,直接将结果标记为xfail。

常用的参数示例如下:

import pytest# run、strict都为默认,因为用例执行是失败的,所以该用例执行结果会被标记为xfail
@pytest.mark.xfail(reason="bug待修复")
def test_01():
print("执行test_01")
a = "hello"
b = "hi"
assert a == b# run、strict都为默认,因为用例执行是通过的,所以该用例执行结果会被标记为xpass
@pytest.mark.xfail(condition=lambda: True, reason="bug待修复")
def test_02():
print("执行test_02")
a = "hello"
b = "hi"
assert a != b# run=False,该用例不执行,直接将结果标记为xfail
@pytest.mark.xfail(reason="功能尚未开发完成", run=False)
def test_03():
print("执行test_03")
a = "hello"
b = "hi"
assert a == b# strict=True,因为用例执行是通过的,所以结果会被标记为failed
@pytest.mark.xfail(reason="功能尚未开发完成", strict=True)
def test_04():
print("执行test_04")
a = "hello"
b = "he"
assert b in aif __name__ == '__main__':
pytest.main(['-s'])

运行结果:

从结果中可以看出来,test_01结果展示为xfailtest_02结果展示为xpasstest_03没有执行而是直接展示为xfailtest_04结果展示为failed

总结

以上示例仅仅只是为了说明@pytest.mark提供的这些功能的使用方法,实际自动化过程中需要灵活选用。

在一般的自动化测试过程中,通过打标签的方式标记某个场景用例会比较多,如标记冒烟测试用例用于冒烟测试。跳过或条件跳过测试用例也经常会用到。而需要将用例标记为预期失败的场景则比较少。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731