前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest-ordering自定义用例执行顺序

Pytest-ordering自定义用例执行顺序

作者头像
王大力测试进阶之路
发布2020-05-28 22:55:01
8830
发布2020-05-28 22:55:01
举报
文章被收录于专栏:橙子探索测试橙子探索测试

我们一般在做自动化测试时,用例设计之间应该是可以相互独立执行的,没有一定的前后依赖关系的,如果我们真的有前后依赖,想指定用例的先后顺序,可以用到pytest-ordering插件解决这个问题

1、安装依赖包

pip install pytest-ordering

2、运用

用例方法上添加装饰器@pytest.mark.run(order=2),用例执行顺序会以order值大小升序去调用执行

3、先按Pytest默认执行顺序(根据用例的先后顺序)先执行了用例1(test_login_01)再执行了用例2(test_login_02)

代码语言:javascript
复制
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest
 
 
class Test(object):
 
    def test_login_01(self):
        """用例1"""
        print('执行用例test_login_01断言1')
        pytest.assume(1 == 1)
        print('执行用例test_login_01断言2')
        pytest.assume(2 == 2)
 
    def test_login_02(self):
        """用例2"""
        print('执行用例test_login_02断言1')
        pytest.assume(3 == 3)
        print('执行用例test_login_02断言2')
        pytest.assume(True)
 
 
if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py'])
 
 
 
C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test
plugins: assume-2.2.1, ordering-0.6
收集的测试用例:[<Function test_login_01>, <Function test_login_02>]
collected 2 items
 
test_C_01.py 执行用例test_login_01断言1
执行用例test_login_01断言2
.执行用例test_login_02断言1
执行用例test_login_02断言2
.
 
============================== 2 passed in 0.04s ==============================
 
Process finished with exit code 0

4、设置了用例先后顺序为est_login_01(@pytest.mark.run(order=2))、test_login_02(@pytest.mark.run(order=1)),调用后先执行了用例2(test_login_02)再执行了用例1(test_login_01)

代码语言:javascript
复制
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest
 
 
class Test(object):
 
    @pytest.mark.run(order=2)
    def test_login_01(self):
        """用例1"""
        print('执行用例test_login_01断言1')
        pytest.assume(1 == 1)
        print('执行用例test_login_01断言2')
        pytest.assume(2 == 2)
 
    @pytest.mark.run(order=1)
    def test_login_02(self):
        """用例2"""
        print('执行用例test_login_02断言1')
        pytest.assume(3 == 3)
        print('执行用例test_login_02断言2')
        pytest.assume(True)
 
 
if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py'])
 
 
C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test
plugins: assume-2.2.1, ordering-0.6
收集的测试用例:[<Function test_login_01>, <Function test_login_02>]
collected 2 items
 
test_C_01.py 执行用例test_login_02断言1
执行用例test_login_02断言2
.执行用例test_login_01断言1
执行用例test_login_01断言2
.
 
============================== 2 passed in 0.06s ==============================
 
Process finished with exit code 0
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 橙子探索测试 微信公众号,前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档