前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest Hooks方法之pytest_collection_modifyitems改变测试用例执行顺序

Pytest Hooks方法之pytest_collection_modifyitems改变测试用例执行顺序

作者头像
王大力测试进阶之路
发布2020-05-25 10:19:30
2K0
发布2020-05-25 10:19:30
举报
文章被收录于专栏:橙子探索测试橙子探索测试

pytest默认执行用例顺序是根据项目下文件名称按ascii码去收集运行的,文件里的用例是从上往下按顺序执行的.

pytest_collection_modifyitems 这个函数顾名思义就是收集测试用例、改变用例的执行顺序的。

一、pytest_collection_modifyitems 是测试用例收集完成后,可以改变测试用例集合(items)的顺序,items是用例对象的一个列表,改变items里面用例的顺序就可以改变用例的执行顺序了。源码如下:

代码语言:javascript
复制
def pytest_collection_modifyitems(session, config,items):
    '''called after collection is completed. 
    you can modify the ``items`` list
    :param _pytest.main.Session session: the pytest session object
    :param _pytest.config.Config config: pytest config object
    :param List[_pytest.nodes.Item] items: list of item objects
    '''

二、pytest执行全部文件,默认执行顺序

代码语言:javascript
复制
conftest.py
 
import pytest
def pytest_collection_modifyitems(session, items):
     print("收集的测试用例:%s"%items)
代码语言:javascript
复制
test_02.py
 
import pytest
 
class Test(object):
 
    def test_three_03(self):
        """用例描述"""
        print("用例3——橙子")
 
    def test_four_04(self):
        """用例描述"""
        print("用例4——橙子")
 
if __name__ == '__main__':
    pytest.main(['-s', ''])
代码语言:javascript
复制
test_C_01.py
 
import pytest
 
class Test(object):
 
    def test_two_02(self):
        """用例描述"""
        print("用例2——橙子")
 
    def test_one_01(self):
        """用例描述"""
        print("用例1——橙子")
 
if __name__ == '__main__':
    pytest.main(['-s', ''])

在test_02.py或test_C_01.py里执行,结果如下,可以看出pytest默认执行顺序是文件按照ascii码去收集运行的,文件里的用例是按从上到下顺序执行的

代码语言:javascript
复制
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
收集的测试用例:[<Function test_three_03>, <Function test_four_04>, <Function test_two_02>, <Function test_one_01>]
collected 4 items
 
test_02.py 用例3——橙子
.用例4——橙子
.
test_C_01.py 用例2——橙子
.用例1——橙子
.
 
============================== 4 passed in 0.80s ==============================
 
Process finished with exit code 0

三、pytest执行部分文件,指定文件执行顺序

代码语言:javascript
复制
conftest.py
 
import pytest
def pytest_collection_modifyitems(session, items):
     print("收集的测试用例:%s"%items)
代码语言:javascript
复制
test_02.py
 
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest
 
class Test(object):
 
    def test_three_03(self):
        """用例描述"""
        print("用例3——橙子")
 
    def test_four_04(self):
        """用例描述"""
        print("用例4——橙子")
 
if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py','test_02.py'])
代码语言:javascript
复制
test_C_01.py
 
#!/usr/bin/env python
# _*_coding:utf-8_*_
 
import pytest
 
class Test(object):
 
    def test_two_02(self):
        """用例描述"""
        print("用例2——橙子")
 
    def test_one_01(self):
        """用例描述"""
        print("用例1——橙子")
 
if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py','test_02.py'])

在test_02.py或test_C_01.py里执行了文件pytest.main(['-s', 'test_C_01.py','test_02.py']),结果如下,可以看出pytest指定部分文件执行时,文件执行顺序是按指定顺序执行的,文件里用例是按从上到下顺序执行的。

代码语言:javascript
复制
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
收集的测试用例:[<Function test_two_02>, <Function test_one_01>, <Function test_three_03>, <Function test_four_04>]
collected 4 items
 
test_C_01.py 用例2——橙子
.用例1——橙子
.
test_02.py 用例3——橙子
.用例4——橙子
.
 
============================== 4 passed in 0.11s ==============================
 
Process finished with exit code 0

四、按照文件里用例名称ascii码排序(升序或降序)

代码语言:javascript
复制
conftest.py
 
 
import pytest
def pytest_collection_modifyitems(session, items):
    print("收集的测试用例:%s" % items)
    items.sort(key=lambda x: x.name)
    print("排序后收集的测试用例:%s" % items)
代码语言:javascript
复制
test_02.py
 
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest
 
class Test(object):
 
    def test_c_03(self):
        """用例描述"""
        print("用例3——橙子")
 
    def test_d_04(self):
        """用例描述"""
        print("用例4——橙子")
 
if __name__ == '__main__':
    pytest.main(['-s', ''])
代码语言:javascript
复制
test_C_01.py
 
#!/usr/bin/env python
# _*_coding:utf-8_*_
 
import pytest
 
class Test(object):
 
    def test_b_02(self):
        """用例描述"""
        print("用例2——橙子")
 
    def test_a_01(self):
        """用例描述"""
        print("用例1——橙子")
 
if __name__ == '__main__':
    pytest.main(['-s', ''])

在test_02.py或test_C_01.py里执行了文件pytest.main(['-s', '']),结果如下,可以看出按照用例名升序执行了

代码语言:javascript
复制
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
收集的测试用例:[<Function test_c_03>, <Function test_d_04>, <Function test_b_02>, <Function test_a_01>]
排序后收集的测试用例:[<Function test_a_01>, <Function test_b_02>, <Function test_c_03>, <Function test_d_04>]
collected 4 items
 
test_C_01.py 用例1——橙子
.用例2——橙子
.
test_02.py 用例3——橙子
.用例4——橙子
.
 
============================== 4 passed in 0.96s ==============================
 
Process finished with exit code 0
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-20,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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