python-pytest配置文件運行測試


通過之前的學習 我們已經知道了 pytest 運行測試用例 主要 以下兩種方式:

1、主函數 傳參運行 pytest.main(['-vs ./xxxx.py'])

2、命令行輸入 運行pytest -vs './xxx.py'

 

在我們實際項目應用中 通過以上兩種方式 去運行測試用例 維護成本較高

故pytest 還提供了第三種執行測試用例 的方式:通過配置文件pytest.ini的方式運行測試用例

pytest.ini配置文件的格式要求:

  • 編碼格式必須是ANSI
  • 配置文件的存放目錄必須是項目的根目錄

 

這里我們通過nodepad++編輯器修改配置文件的編碼格式

具體配置參數內容如下:

[pytest]

# 命令參數 ,多個參數用空格分開
addopts = -vs

#執行測試的路徑

testpaths = ./pytest_demo66/pyini

#匹配路徑下的測試模塊

python_files = test_*.py

#匹配具體測試類,比如以Test開頭
python_classes = Test*

#匹配要運行的測試函數
python_functions = test

#對測試用例進行分組分類,比如區分冒煙用例,回歸用例等
marks =
smoke:冒煙用例
master:回歸
pre:預發
online:線上

配置pytest.ini后,我們后續運行測試的時候,pytest會優先讀取這個配置文件里的配置,並按照配置參數去執行測試

我們前面講了 pytest編寫測試用例時默認的的命名規則:

  • 文件名以 test_*.py 文件和*_test.py
  • 以  test_ 開頭的函數
  • 以  Test 開頭的類,且不能包含 __init__ 方法
  • 以  test_ 開頭的類里面的方法
  • 所有的包 package 必須要有__init__.py 文件

 

但此時我們是通過配置文件的方式去運行測試,

故我們的測試模塊名,方法名,函數名,類名 的命名規則就可以不按這個默認的規則去書寫了

比如我們定義一個 模塊hello.py,Login的測試類,abc_1的測試方法

hello.py

class Login:
    def abc_1(self):
        print('abc-1')

    def test_2(self):
        print('test_2')

    def test_3(self):
        print('test_3')

我們通過修改pytest.ini配置文件去運行這個測試用例

[pytest]

addopts = -vs
testpaths = ./pytest_demo66
python_files = hello_*.py
python_classes = Login*
python_functions = abc_*

 

命令行輸入pytest 執行測試

結果如下:

========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- d:\python\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0',
'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}}
rootdir: D:\pytest_demo, configfile: pytest.ini, testpaths: ./pytest_demo66/pyini
plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0
collected 1 item                                                                                                                                                          

pytest_demo66/pyini/hello_world.py::Login::abc_1 test_1
PASSED

=========================================================================== 1 passed in 0.03s ============================================================================

我們看到只有abc_1 這條用例被執行了 其他 測試方法並沒有被執行,所以通過配置文件的方式 去執行用例,我們就可以靈活的命名自己想要的測試方法,類,以及模塊名。

除此之外,我們在實際的項目種還有可能需要給用例做標記或分類

比如a用例是冒煙用例,b用例是回歸用例等,現在通過配置文件的方式,我們就可以實現

test_2.py

import pytest

@pytest.mark.smoke
def test_3():
    print('冒煙用例3')

def test_1():
    print('冒煙用例1')

@pytest.mark.master
def test_2():
    print('回歸用例2')

配置文件我們更改為:

[pytest]
addopts = -vs
testpaths = ./pytest_demo66/pyini
python_files = test_*.py
python_classes = Login*
python_functions = test_*
marks = 
    smoke:冒煙用例
    masster:回歸
    pre:預發
    online:線上

命令行運行

pytest -m "smoke"

運行結果

D:\pytest_demo>pytest -m "smoke"
========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- d:\python\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0',
'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}}
rootdir: D:\pytest_demo, configfile: pytest.ini, testpaths: ./pytest_demo66/pyini
plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0
collected 3 items / 2 deselected / 1 selected                                                                                                                             

pytest_demo66/pyini/test_1.py::test_3 冒煙用例3
PASSED

我們看到 只有 標記了smoke的這個測試方法被執行了

修改命令行參數繼續執行測試:

pytest -m "smoke or master"

運行結果:

D:\pytest_demo>pytest -m "smoke or master"
========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1 -- d:\python\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.5', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0',
'html': '3.1.1', 'metadata': '1.11.0', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.4.0'}}
rootdir: D:\pytest_demo, configfile: pytest.ini, testpaths: ./pytest_demo66/pyini
plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0
collected 3 items / 1 deselected / 2 selected                                                                                                                             

pytest_demo66/pyini/test_1.py::test_2 回歸用例2
PASSED
pytest_demo66/pyini/test_1.py::test_3 冒煙用例3
PASSED
============================================================== 2 passed, 1 deselected, 5 warnings in 0.03s =========================================================

我們看到標記了smoke 和master 的兩條測試用例都被執行了 

也就說針對不同標記 的測試用例,pytest可以組合一起運行測試

這極大的提高了運行測試的自由度

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM