Pytest Hooks方法之pytest_collection_modifyitems改變測試用例執行順序

         pytest默認執行用例順序是根據項目下文件名稱按ascii碼去收集運行的,文件裏的用例是從上往下按順序執行的.
pytest_collection_modifyitems 這個函數顧名思義就是收集測試用例、改變用例的執行順序的。

一、pytest_collection_modifyitems 是測試用例收集完成後,可以改變測試用例集合(items)的順序,items是用例對象的一個列表,改變items裏面用例的順序就可以改變用例的執行順序了。源碼如下:

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執行全部文件,默認執行順序

conftest.py

import pytest
def pytest_collection_modifyitems(session, items):
     print("收集的測試用例:%s"%items)
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', ''])
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碼去收集運行的,文件裏的用例是按從上到下順序執行的

"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執行部分文件,指定文件執行順序

conftest.py

import pytest
def pytest_collection_modifyitems(session, items):
     print("收集的測試用例:%s"%items)
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'])
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指定部分文件執行時,文件執行順序是按指定順序執行的,文件裏用例是按從上到下順序執行的。

"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碼排序(升序或降序)

conftest.py


import pytest
def pytest_collection_modifyitems(session, items):
    print("收集的測試用例:%s" % items)
    items.sort(key=lambda x: x.name)
    print("排序後收集的測試用例:%s" % items)
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', ''])
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', '']),結果如下,可以看出按照用例名升序執行了

"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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章