pytest學習筆記(控制用例的執行順序)

1、pytest加載全部的用例都是亂序的,若是想指定用例的順序,能夠使用pytest-ordering插件,指定用例的執行順序只須要在測試用例的方法前面加上裝飾器@pytest.mark.run(order=[num])設置order的對應的num值,它就能夠按照num的大小順序來執行。html

應用場景:有時運行測試用例要指定它的順序,好比有些場景要先須要登入,才能執行後面的流程好比購物流程,下單流程,這時就須要指定用例的執行順序。經過pytest-ordering這個插件能夠完成用例順序的指定。python

 

2、安裝session

pip install pytest-ordering測試

 

3、實例spa

#!/usr/bin/env python
# _*_coding: utf-8 _*_
import pytest


class Testpytest(object):

    @pytest.mark.run(order=-1)
    def test_two(self):
        print("test_two, 測試用例")

    @pytest.mark.run(order=3)
    def test_one(self):
        print("test_one, 測試用例")

    @pytest.mark.run(order=1)
    def test_three(self):
        print("test_three, 測試用例")

四運行結果插件

Testing started at 15:51 ...
C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items

test_order.py                                                         [100%]

============================== 3 passed in 0.06s ==============================

Process finished with exit code 0
.test_three, 測試用例
.test_one, 測試用例
.test_two, 測試用例
相關文章
相關標籤/搜索