目录
1、前言
在自动化测试工作中我们有时候并不需要测试所有的测试用例,比如在冒烟测试阶段,我们只需要测试基本功能是否正常就可以了。在pytest中提供了mark标记功能来实现分组执行。
2、mark的使用
步骤:
- 在pytest.ini中注册标记(名称可自定义)
- 使用@pytest.mark.上一步注册的名称标记需要执行的用例
- 执行
(一)注册自定义标记
在pytest.ini中添加markers- [pytest] # 固定的section名
- markers= # 固定的option名称,注意缩进。
- 标签名1: 标签名的说明内容。
- 标签名2: 不写也可以
- 标签名N
复制代码 示例:- [pytest]
- markers =
- smoke: 冒烟测试
- back
复制代码 (二)在测试用例上标记
通过@pytest.mark.标记名标记要测试的用例。
示例:- import pytest
- @pytest.mark.smoke
- def test_1():
- print("测试1")
- @pytest.mark.back
- def test_2():
- print("测试2")
- def test_3():
- print("测试3")
复制代码 (三)执行
通过在命令行增加-m参数指定要测试的分组。
示例:
执行冒烟用例:pytest -vs xxx.py -m smoke- """
- 执行结果
- collected 3 items / 2 deselected / 1 selected
- mark/mark/mark.py::test_1 测试1
- PASSED
- """
复制代码 执行回归用例:pytest -vs xxx.py -m back- """
- 执行结果
- collected 3 items / 2 deselected / 1 selected
- mark/mark/mark.py::test_2 测试2
- PASSED
- """
复制代码 3、扩展
(一)在同一个测试用例上使用多个标记
- import pytest
- @pytest.mark.back
- def test_register():
- # 注册
- print("注册")
- @pytest.mark.smoke
- @pytest.mark.back
- def test_login():
- # 登录
- print("登录")
- @pytest.mark.back
- def test_logout():
- # 注销
- print("注销")
- @pytest.mark.smoke
- def test_add_cart():
- # 加购物车
- print("加购物车")
- @pytest.mark.smoke
- def test_place_order():
- # 下单
- print("下单")
- @pytest.mark.smoke
- @pytest.mark.pay
- def test_pay_order():
- # 支付订单
- print("支付订单")
复制代码 在执行时支持通过and,or,or来连接多个标记,如下
- pytest -vs -m "smoke or pay" xxx.py,此时只有登录,加购物车,下单,支付订单这4个用例执行
- pytest -vs -m "smoke and pay" xxx.py,此时只有支付订单这个用例执行
- pytest -vs -m "not smoke" xxx.py,此时只有注册,注销这2个用例执行
(二)在测试类上使用标记
- import pytest
- @pytest.mark.smoke
- class TestLogin:
- def test_login(self):
- print("登录")
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |