我是靠谱客的博主 平淡朋友,这篇文章主要介绍单元测试界的高富帅,Pytest框架,用例标记和测试执行怎么做1.1、pytest.mark.parametrize:用例参数化的标记1.2、pytest.mark.skip:跳过用例执行1.3、pytest.mark.skipif:根据条件跳过用例1.4、pytest.mark.xfail:标记预期失败的用例1.5、pytest.mark.usefixtures:给测试类或模块设置测试夹具二、自定义标记2.1、注册标记2.2、标记函数2.3、标记类三、通过标记筛选用例执行3.1、通过单个标记筛,现在分享给大家,希望可以做个参考。

一、pytest 中内置的标记

单元测试界的高富帅,Pytest框架,用例标记和测试执行怎么做

pytest 标记使用需要通过 pytest.mark.标记 来使用,pytest 中为应对各种测试场景也内置了很多的标记。

1.1、pytest.mark.parametrize:用例参数化的标记

通过 parametrize 可以将用例数据和用例执行的逻辑代码分离,并实现根据用例,自动生成测试用例。

Demo:

复制代码
1
2
3
@pytest.mark.parametrize('item',[11,22,33,44,55,66]) def test_demo(item) assert item > 50

1.2、pytest.mark.skip:跳过用例执行

通过 skip 装饰的用例,在执行的时候会无条件跳过,

参数 reason:跳过测试函数的原因。

Demo

复制代码
1
2
3
4
5
6
7
8
9
10
# 不写跳过原因 @pytest.mark.skip def test_demo() assert item > 50 # 写跳过原因 @pytest.mark.skip(reason='不需要执行') def test_demo() assert item > 50

1.3、pytest.mark.skipif:根据条件跳过用例

skipif 可以根据条件来决定是否跳过用例的执行, 如果条件为 True 则跳过测试函数执行。

参数 :condition —跳过的条件

参数 :reason —跳过的原因

Demo

复制代码
1
2
3
4
a = 10 @pytest.mark.skipif(a > 20,reason='条件不成立,不执行') def test_demo() assert item > 50

1.4、pytest.mark.xfail:标记预期失败的用例

xfail 可以将测试函数标记为预期执行失败的用例。

参数 :condition — 将测试函数标记为 xfail 的条件(True/False )

参数 :reason — 测试函数被标记为 xfail 的原因

参数 :raises — 预期失败的异常类型

参数 :run — 是否应该实际执行测试函数。如果 False,该函数将始终 xfail 并且不会被执行 。

参数 :strict — 严格模式(True/False )

Demo

复制代码
1
2
3
4
a = 10 @pytest.mark.xfail(a > 20,reason='条件不成立,不执行' raises=AssertionError ) def test_demo() assert item > 50

1.5、pytest.mark.usefixtures:给测试类或模块设置测试夹具

usefixtures 标记一般用于给测试类下面的测试方法统一设置测试夹具。

Demo

复制代码
1
2
3
4
5
6
7
8
9
10
11
# TestDome这个测试类的所有测试用例均执行my_fixture这个夹具 @pytest.mark.usefixtures('my_fixture这个夹具') class TestDome: # 函数用例 指定测试夹具 def test_02(self): print('----测试用例:test_01------') # 函数用例 指定测试夹具 def test_03(self): print('----测试用例:test_02------')

二、自定义标记

pytest 支持通过 pytest.ini 文件注册自定义的标记。以满足执行用例时,通过标记对用例进行筛选。,

2.1、注册标记

pytest.ini 文件注册标记的语法如下:

复制代码
1
2
3
4
5
6
[pytest] markers = 标记1 标记2

2.2、标记函数

Demo:

复制代码
1
2
3
4
# 用例前面加载标签:@pytest.mark.标签名 @pytest.mark.main def test_demo(): pass

2.3、标记类

Demo:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
# 方式一:直接类上面打标记 @pytest.mark.main class TestClass(object): def test_demo1(self): assert 10 > 20 # 方式二:通过类属性pytestmark,可以同时添加多个标记 class TestClass(object): pytestmark = [pytest.mark.main, pytest.mark.main] def test_demo1(self): assert 10 > 20

三、通过标记筛选用例执行

Demo:现有用例如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pytest @pytest.mark.yuze @pytest.mark.musen def test_01(): print("用例一") def test_02(): print("用例二") @pytest.mark.musen def test_03(): print("用例三") @pytest.mark.musen def test_04(): print("用例四") @pytest.mark.yuze def test_05(): print("用例五") @pytest.mark.yuze def test_06(): print("用例六")

上面 Demo 中有 6 条测试用例,分别通过 pytest.mark.yuze 和 pytest.mark.musen 进行标记了,接下来我们一起来看看如何通过标记选择用例执行。

3.1、通过单个标记筛选

语法:pytest -m '标签名'

Demo: pytest -m musen

执行结果如下:

复制代码
1
2
3
4
5
6
7
========================== test session starts ========================== platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 rootdir: C:project, inifile: pytest.ini plugins: allure-pytest-2.8.15, Faker-8.11.0, metadata-1.9.0, parallel-0.0.8, repeat-0.8.0, rerunfailures-9.0, testreport-1.1.2 collected 6 items / 3 deselected / 3 selected test_mode.py ... [100%] ========================== 3 passed, 3 deselected in 0.29s ==========================

可以看到执行结果执行了 3 条用例,3 条未选中。

3.2、同时选中多个标记

语法:pytest -m "标记 1 or 标记 2"

命令:pytest -m "musen ro yuze"

执行通过 musen 或者 yuze 标记的的用例。执行结果如下:

复制代码
1
2
3
4
5
6
7
========================== test session starts ========================== platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 rootdir: C:project, inifile: pytest.ini plugins: allure-pytest-2.8.15, Faker-8.11.0, metadata-1.9.0, parallel-0.0.8, repeat-0.8.0, rerunfailures-9.0, testreport-1.1.2 collected 6 items / 1 deselected / 5 selected test_mode.py ..... [100%] ========================== 5 passed, 1 deselected in 0.29s ==========================

从上述结果可以看到,只要加了 musen 或 yuze 这两个标记中的任意一个

语法:pytest -m "标记 1 and 标记 2"

命令:pytest -m "musen and yuze"

执行通过 musen 和 yuze 这两个标记同时标记的用例。执行结果如下

复制代码
1
2
3
4
5
6
7
========================== test session starts ========================== platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 rootdir: C:project, inifile: pytest.ini plugins: allure-pytest-2.8.15, Faker-8.11.0, metadata-1.9.0, parallel-0.0.8, repeat-0.8.0, rerunfailures-9.0, testreport-1.1.2 collected 6 items / 5 deselected / 1 selected test_mode.py . [100%] ========================== 1 passed, 5 deselected in 0.29s ===================

 

最后

以上就是平淡朋友最近收集整理的关于单元测试界的高富帅,Pytest框架,用例标记和测试执行怎么做1.1、pytest.mark.parametrize:用例参数化的标记1.2、pytest.mark.skip:跳过用例执行1.3、pytest.mark.skipif:根据条件跳过用例1.4、pytest.mark.xfail:标记预期失败的用例1.5、pytest.mark.usefixtures:给测试类或模块设置测试夹具二、自定义标记2.1、注册标记2.2、标记函数2.3、标记类三、通过标记筛选用例执行3.1、通过单个标记筛的全部内容,更多相关单元测试界内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部