python單測框架之pytest常見用法
單測框架的作用
- 測試發(fā)現(xiàn):從多個文件中尋找測試用例。
- 測試執(zhí)行:按照一定順序去執(zhí)行并且生成結(jié)果。
- 測試斷言:判斷最終結(jié)果與實際結(jié)果的差異。
- 測試報告:統(tǒng)計測試進度、耗時、通過率,生成測試報告。
pytest簡介
pytest是python的單測框架,使用靈活,插件豐富,以下是pytest常用的插件
- pytest
- pytest-html:生成html測試報告插件
- pytest-xdist:多線程執(zhí)行用例插件
- pytest-ordering:自定義用例順序插件
- pytest-rerunfailures:失敗重跑插件
- allure-pytest:生成allure美觀測試報告插件
pip install 就行,只有有這些插件,下面的某些命令行才生效
pytest默認規(guī)則
- 模塊名必須以test_開頭或者_test結(jié)尾
- 類名必須以Test開頭
- 測試方法必須以test開頭
- pytest用例運行順序默認從上到下(代碼中可以使用裝飾器@pytest.mark.run(order=1)來指定執(zhí)行順序)
使用pytest.ini文件可以修改默認規(guī)則
pytest的運行方式
主函數(shù)模式
import pytest if __name__ == '__main__': pytest.main(["-vs", "./test_demo/test_demo1.py"])
這樣就可以運行所有用例
命令行模式
pytest -vs ./test_demo/test_demo1.py
參數(shù)詳解
- -s:輸出調(diào)試的信息
- -v:表示詳細的方式輸出
- ./test_demo/test_demo1.py表示運行指定模塊,相對路徑表示
- ./test_demo/test_demo1.py::TestCase1::test_case1 nodeid表示,代表運行./test_demo/test_demo1.py模塊下的TestCase1類的test_case1 方法
- -n分布式運行測試用例,-n num,參數(shù)num代表幾個線程運行用例
- –reruns=2表示用例失敗重跑2次,常用于一些不穩(wěn)定的用例,如web自動化
- -x只要有一個用例報錯,那么就會停止
- –maxfail=2,有2個用例失敗就會停止
- -k根據(jù)測試用例部分字符串指定測試用例,如 -k “ao”,代表會執(zhí)行帶有ao名稱的字符串
讀取pytest.ini配置文件運行
不論是主函數(shù)模式還是命令行模式都會讀取這個配置文件,該文件需要使用gbk編碼,下面是這個配置文件的例子
[pytest] # 命令行參數(shù),用空格分隔 addopts = -vs # 測試用例文件夾,可以自己配置 testpaths = ./test_demo # 配置測試搜索的模塊文件名稱 python_files = test*.py # 配置測試搜索的類名 python_classes = Test* # 配置搜索的函數(shù)名 python_functions = test
分組執(zhí)行
定義三個組,冒煙:smoke,用戶管理:user_manager,作業(yè)管理:worker_manager
目前有幾個用例給加個分組的裝飾器
import pytest class TestDemo: @pytest.mark.somke def test_case1(self): print("1") @pytest.mark.user_manage def test_case2(self): print("2") @pytest.mark.worker_manage def test_case3(self): print("3")
配置文件中加入分組信息
markers = smoke:冒煙測試 user_manage:用戶管理 worker_manage:作業(yè)管理
運行
運行多組
import pytest if __name__ == '__main__': pytest.main(["-vs", "-m smoke or usermanage"])
運行單組
import pytest if __name__ == '__main__': pytest.main(["-vs", "-m smoke"])
忽略執(zhí)行
無條件忽略
直接使用裝飾器@pytest.mark.skip(reason=“原因填寫”)
有條件忽略
使用裝飾器@pytest.mark.skipif(條件, 原因)
例子:
import pytest class TestDemo: age = 18 @pytest.mark.smoke def test_case1(self): print("1") @pytest.mark.usermanage @pytest.mark.skipif(age < 18, "未成年") def test_case2(self): print("2") @pytest.mark.workermanage @pytest.mark.skip(reason="原因填寫") def test_case3(self): print("3")
pytest中的前后置處理
為什么需要前后置?比如執(zhí)行用例前需要做一些準備工作,比如打開瀏覽器,在執(zhí)行用例后需要一些后置工作,比如關(guān)閉瀏覽器
模塊級別
在每個模塊執(zhí)行前會調(diào)用setup_module方法,在每個模塊執(zhí)行后會使用teardown_module方法。
例子:
import pytest def setup_module(): print("模塊用例前執(zhí)行") def teardown_module(): print("模塊用例后執(zhí)行") class TestDemo: def test_case1(self): print("1") def test_case2(self): print("2") def test_case3(self): print("3") class TestDemo2: def test_case4(self): print("4")
結(jié)果:
test_demo/test_demo2.py::TestDemo::test_case1 模塊用例前執(zhí)行 1 PASSED test_demo/test_demo2.py::TestDemo::test_case2 2 PASSED test_demo/test_demo2.py::TestDemo::test_case3 3 PASSED test_demo/test_demo2.py::TestDemo2::test_case4 4 PASSED模塊用例后執(zhí)行
類級別
類級別函數(shù) setup_class/teardown_class 對類有效,位于類中,在測試類中前后調(diào)用一次。
class TestDemo: def setup_class(self): print("類級別前置") def test_case1(self): print("1") def test_case2(self): print("2") def test_case3(self): print("3") def teardown_class(self): print("類級別后置")
test_demo/test_demo2.py::TestDemo::test_case1 模塊用例前執(zhí)行 類級別前置 1 PASSED test_demo/test_demo2.py::TestDemo::test_case2 2 PASSED test_demo/test_demo2.py::TestDemo::test_case3 3 PASSED類級別后置 模塊用例后執(zhí)行
方法級別
方法級別函數(shù) setup_method/teardown_method和setup/teardown對類有效,也位于類中,這兩個效果一樣,在測試類中每個測試方法前后調(diào)用一次。
class TestDemo: def setup_method(self): print("方法級別前置") def test_case1(self): print("1") def test_case2(self): print("2") def test_case3(self): print("3") def teardown_method(self): print("方法級別后置")
test_demo/test_demo3.py::TestDemo::test_case1 方法級別前置 PASSED方法級別后置 test_demo/test_demo3.py::TestDemo::test_case2 方法級別前置 PASSED方法級別后置 test_demo/test_demo3.py::TestDemo::test_case3 方法級別前置 PASSED方法級別后置
部分用例的前后置 pytest.fixture裝飾器
import pytest @pytest.fixture(scope="function", params=["1", "2", "3"], autouse=False, ids=None, name="new_name") def my_feature(request): i = request.param print("前置") yield i print("后置") class TestDemo: def test_case1(self, new_name): print(new_name) print("1")
結(jié)果
test_demo/test_demo4.py::TestDemo::test_case1[1] 前置
1
1
PASSED后置test_demo/test_demo4.py::TestDemo::test_case1[2] 前置
2
1
PASSED后置test_demo/test_demo4.py::TestDemo::test_case1[3] 前置
3
1
PASSED后置
- scope:表示作用域
- params:表示參數(shù)化,與yield使用會調(diào)用len(params)次用例,如例子所示,一般用于數(shù)據(jù)驅(qū)動
- autouse:默認使用,一般設(shè)置為false
- ids:params參數(shù)化時,給每個參數(shù)起名字
- name:給該方法取別名
pytest.fixture+conftest
fixture為session級別是可以跨.py模塊調(diào)用的,也就是當我們有多個.py文件的用例的時候,如果多個用例只需調(diào)用一次fixture,那就可以設(shè)置為scope=“session”,并且寫到conftest.py文件里。
conftest.py文件名稱時固定的,pytest會自動識別該文件。放到項目的根目錄下就可以全局調(diào)用了,如果放到某個package下,那就在改package內(nèi)有效。
例子:
在包下創(chuàng)建conftest.py,注意,該配置只在本包生效
和之前一樣使用
結(jié)果還是和之前一樣。
pytest生成測試報告
pytest-html插件生成報告
pytest -vs --html ./report/report.html
參數(shù)化與數(shù)據(jù)驅(qū)動
主要用的裝飾器是@pytest.mark.parametrize(argnames, argvalues)
不帶名字數(shù)據(jù)驅(qū)動
import pytest class TestDemo: @pytest.mark.parametrize("args",[(4399, 'AAAA'), (2012, 'BBBB')]) def test_case1(self, args): print(args)
結(jié)果:
test_demo/test_demo4.py::TestDemo::test_case1[args0] (4399, ‘AAAA')
PASSED
test_demo/test_demo4.py::TestDemo::test_case1[args1] (2012, ‘BBBB')
PASSED 帶名字的數(shù)據(jù)驅(qū)動
import pytest class TestDemo: @pytest.mark.parametrize("arg1,arg2", [(4399, 'AAAA'), (2012, 'BBBB')]) def test_case1(self, arg1, arg2): print(arg1, arg2)
結(jié)果:
test_demo/test_demo4.py::TestDemo::test_case1[4399-AAAA] 4399 AAAA
PASSED
test_demo/test_demo4.py::TestDemo::test_case1[2012-BBBB] 2012 BBBB
PASSED
到此這篇關(guān)于python單測框架之pytest慣用法的文章就介紹到這了,更多相關(guān)python單測框架pytest內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV 使用imread()函數(shù)讀取圖片的六種正確姿勢
這篇文章主要介紹了OpenCV 使用imread()函數(shù)讀取圖片的六種正確姿勢,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-07-07python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例詳解
數(shù)據(jù)清洗是指發(fā)現(xiàn)并糾正數(shù)據(jù)文件中可識別的錯誤的最后一道程序,包括檢查數(shù)據(jù)一致性,處理無效值和缺失值等,數(shù)據(jù)清洗與處理的目的是提高數(shù)據(jù)的質(zhì)量,提高實驗結(jié)果的可靠度,本文給大家介紹了python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例,需要的朋友可以參考下2024-08-08Python實現(xiàn)列表轉(zhuǎn)換成字典數(shù)據(jù)結(jié)構(gòu)的方法
這篇文章主要介紹了Python實現(xiàn)列表轉(zhuǎn)換成字典數(shù)據(jù)結(jié)構(gòu)的方法,結(jié)合實例形式分析了Python數(shù)值類型轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2016-03-03利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的方法
這篇文章主要給大家介紹了關(guān)于利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-03-03Pycharm Terminal 與Project interpreter 安裝
本文主要介紹了Pycharm Terminal 與Project interpreter 安裝包不同步問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-02-02Python flask框架實現(xiàn)瀏覽器點擊自定義跳轉(zhuǎn)頁面
這篇文章主要介紹了Python flask框架實現(xiàn)瀏覽器點擊自定義跳轉(zhuǎn)頁面,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2020-06-06