pytest是Python最流程的單測框架之一。
在本文中,我們將會介紹pytest的特點,功能和使用。
Demo
安裝Python依賴庫:
pip3 install pytest
pip3 install pytest-allure-adapto
文件目錄:
三個pytest測試腳本(注:一定要以test_或xx_test格式,函數名稱最好也參照)
@pytest.mark.parametrize 函數裝飾器傳參用的
@pytest.mark.parametrize("a,b,expected", testdata) def test_timedistance_v0(a, b, expected): diff = a - b assert diff == expected @pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"]) def test_timedistance_v1(a, b, expected): diff = a - b print(diff) assert diff == expected @pytest.mark.parametrize("a,b,c", testdata1,ids=['1']) def test_from(a,b,c): test_f=a+b assert test_f==c @pytest.mark.parametrize("test_input,expected",[("3+5",8),("3+9",12),("3*5",8),]) def test_evel(test_input,expected): assert eval(test_input)==expected
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/html']) #執行改目錄下的所有測試腳本且生成測試報告,生成報告肯定是非易懂的所以后面用到allure
test()
在項目目錄dos執行這個命令也可以生成測試報告
pytest -s -q --alluredir report====代碼里'-s', '-q', '--alluredir', './report/html'
allure部分
首先要安裝allure
pip install pytest-allure-adaptor
allure-commandline下載鏈接
https://github.com/allure-framework/allure1/releases/download/allure-core-1.5.2/allure-commandline.zip
主要找兩個目錄:首先進入allure的bin目錄下執行
在dos目錄進入bin目錄下執行(我沒有設置環境變量,時間充足的童鞋或者常用的童鞋建議path下加上bin目錄的環境變量)
allure generate report/ -o report/html
allure generate【reprot目錄】-0 【report目錄下加上html目錄】
E:\untitled3\test\report -o E:\untitled3\test\report\html(這個是我的見笑~~)
執行后發現生成生成 Report文件的html文件里在使用pycharm打開可以看到生成的易懂的測試報告
(到這里pytest編寫了測試腳本,allure展示了完美的測試結果報告,但是小編也是一名新生發現變更pytest代碼后allure沒有自動更新測試報告
比如:我之前寫了10條case后來接口改造,需要做關聯變成15條case但是打開測試報告后仍舊是之前10條,無奈之下小編寫了一個test_api
方法,主要用來重新生成報告,自動在dos里面重新執行E:\untitled3\test\report -o E:\untitled3\test\report\html這條命令。
有解決的方案的童鞋和大神請下方評論賜教
)
可以生成pytest自己的hmtl報告 不加路徑是該目錄下所有測試腳本的html報告,但是小編認為不夠allure完美(給領導看這個可能會大概被罵吧)
精彩文章:https://www.cnblogs.com/yrxns/p/8386267.html
https://www.cnblogs.com/xiaoxi-3-/p/9492534.html
http://www.cnblogs.com/Raul2018/p/9469323.html