一、pytest介紹
pytest介紹 - unittest\nose
pytest:基于unittest之上的單元測試框架
自動發(fā)現(xiàn)測試模塊和測試方法
斷言使用assert+表達式即可
可以設(shè)置測試會話級、模塊級、類級、函數(shù)級的fixtures 數(shù)據(jù)準(zhǔn)備 + 清理工作
unittest:setUp、teardown、setUpClass、tearDownClass
共享前置后置 – conftest.py
有豐富的插件庫,目前在900個以上 allure
二、安裝命令
pip install pytest
安裝html報告的插件
pip install pytest-html
pytest插件地址
http://plugincompat.herokuapp.com/
pytest之mark功能
pytest - 收集測試用例
pytest收集測試用例的規(guī)則
默認從當(dāng)前目錄中搜集測試用例,即在哪個目錄下運行pytest命令,則從哪個目錄當(dāng)中搜索
三、搜索規(guī)則
符合命名規(guī)則 test_*.py 或者 *_test.py的文件
以test開頭的函數(shù)名
以Test開頭的測試類(沒有__init__函數(shù))當(dāng)中,以test_開頭的函數(shù)
對測試用例打標(biāo)簽。在運行測試用例的時候,可根據(jù)標(biāo)簽名來過濾要運行的用例
四、使用方法
注冊標(biāo)簽名
在測試用例/測試前面加上:@pytest.mark.已注冊的標(biāo)記名
注冊方式
創(chuàng)建pytest.ini文件,在文件中按如下形式添加標(biāo)簽名:
[pytest] markers= slow:marks tests as slow(deselect with ‘-m “not slow”’) serial
注:冒號之后是可選的描述信息
在conftest.py文件當(dāng)中,通過hook注冊
def pytest_configure(config): config.addinivalue_line(“markers”,“smoke1:標(biāo)記只運行冒煙用例”) config.addinivalue_line(“markers”,“demo1:示例運行”)
給用例打標(biāo)記
方式1
打標(biāo)記范圍:測試用例、測試類、模塊文件
在測試用例/測試類前面加上:@pytest.mark.標(biāo)記名
@pytest.mark.slow
可在一個用例上打多個標(biāo)簽,多次使用@pytest.mark.標(biāo)記名即可
@pytest.mark.slow
@pytest.mark.serial
方式2
打標(biāo)記范圍:測試用例、測試類、模塊文件
在測試類里,使用以下申明(測試類下,所有用例都被打上該標(biāo)簽)
class TestClass(object): pytestmark = pytest.mark.已注冊標(biāo)簽名 pytestmark = [pytest.mark.標(biāo)簽1, pytest.mark.標(biāo)簽2] # 多標(biāo)簽?zāi)J?/p>
在模塊文件里,同理(py文件下,所有測試函數(shù)和測試類里的測試函數(shù),都有該標(biāo)簽)
import pytest pytestmark = pytest.mark.已注冊標(biāo)簽名 pytestmark = [pytest.mark.標(biāo)簽1, pytest.mark.標(biāo)簽2] # 多標(biāo)簽?zāi)J?/p>
pytest之命令行運行用例
安裝后,pytest.exe在python安裝目錄的Scripts目錄下,因為配置了環(huán)境變量后,可以之間運行pytest
腳本里面是,效果通命令行
import pytest if name == ‘main’: pytest.main()
只運行某個標(biāo)記
pytest -m slow
pytest -m slow -s -v # 詳細輸出
pytest之fixture功能
pytest之fixture參數(shù)化 - 多運行、pytest層級覆蓋。測試用例與其同級或者在其子目錄
共享前置后置 – conftest.py
文件名不可更改,不需要引入就可以使用其中的fixture
一個函數(shù):前置+后置
yield分隔前置后置
設(shè)置作用域:中間的夾的是什么,默認"function"
@pytest.fixture def init_driver(): # 前置 pass # 分隔線 yield 返回值寫在這 # 后置 pass
調(diào)用fixture的三種方式
在測試用例中直接調(diào)用它
將fixture的函數(shù)名作為測試用例的參數(shù)
如果fixture有返回值,那么測試用例中的fixture函數(shù)名字就接收返回值
eg
def test_xxx(self,myfixture): myfixture.find_element_by_xpath(“xxx”) # 函數(shù)名代表了fixture的返回值,即driver
用fixture裝飾器調(diào)用fixture
在測試用例/測試類前面加上@pytest.mark.usefixtures(“fixture函數(shù)名字”)
ps:定義conftest.py文件,在此文件中可定義多個fixture,pytest會自動搜索此文件
@pytest.mark.usefixtures(“init_driver”) class TestLogin: @pytest.mark.slow def test_login_success(self, init_driver): init_driver[1]. #返回值直接用,這里返回元組
用autos調(diào)用fixture
在定義fixture時,有一個參數(shù)是autouse,默認設(shè)置為False
當(dāng)默認為False,就可以選擇用上面兩種方式來試用fixture
當(dāng)設(shè)置為True時,在一個session內(nèi)的所有test都會自動調(diào)用這個fixture(權(quán)限大,責(zé)任也大,所以用該功能時也要謹(jǐn)慎小心)
五、pytest之參數(shù)化
在測試用例的前面加上
@pytest.mark.parametrize(“參數(shù)名”,列表數(shù)據(jù))
參數(shù)名:用來接收每一項數(shù)據(jù),并作為測試用例的參數(shù)
列表數(shù)據(jù):一組測試數(shù)據(jù)
@pytest.mark.parametrize(“參數(shù)1,參數(shù)2”,[(數(shù)據(jù)1,數(shù)據(jù)2),(數(shù)據(jù)1,數(shù)據(jù)2)])
示例
@pytest.mark.parametrize(“aa,b,c”, [(1, 3, 4), (10, 35, 45), (22.22, 22.22, 44.44)]) def test_add(self, a, b, c): res = a + b assert res == c
組合參數(shù)化:多組參數(shù),依次組合
使用多個@pytest.mark.parametrize
示例
@pytest.mark.parametrize(“x”,[1,2]) @pytest.mark.parametrize(“y”,[2,3]) def test_foo(x,y): pass
用例有四個1,2/1,3/2,2/2,3 笛卡爾積
pytest之重運行
pytest提供了失敗重試機制
插件名稱rerunfailures
安裝方法
pip install pytest-rerunfailures
使用方式
命令行參數(shù)形式
命令:pytest --reruns 重試次數(shù)
比如:pytest --reruns 2 表示:運行失敗的用例可以重新運行2次
命令:pytest --reruns 重試次數(shù) --reruns-delay 次數(shù)之間的延時設(shè)置(單位:秒)
pytest --reruns 2 --reruns-delay5
表示失敗的用例可以重新運行2次,第一次和第二次的間隔時間為5秒鐘
pytest之html測試報告
需要安裝pytest-html插件
pytest可以生成多種樣式的結(jié)果
生成JunitXML格式的測試報告,命令
–junitxml=path
生成result log格式的測試報告,命令
–resultlog=report\log.txt
生成html格式的測試報告,命令
–html=report\test_one_func.html(相對路徑)
import pytest if name == ‘main’: pytest.main([“–reruns”, “3”, “–reruns-delay”, “5”, “-m”, “fail”, “–html=Reports\report.html”, “–junitxml=Reports\report.xml”])
pytest之a(chǎn)llure測試報告
安裝allure
下載allure.zip
下載地址
alure-github:GitHub - allure-framework/allure2: Allure Report is a flexible, lightweight multi-language test reporting tool. It provides clear graphical reports and allows everyone involved in the development process to extract the maximum of information from the everyday testing process
解壓到本地目錄,配置allure.bat的環(huán)境變量ALLURE_HOME
在命令行中運行allure,確認環(huán)境變量配置成功
pytest插件安裝
pip install allure-pytest
pytest生成allure測試報告的命令參數(shù)
–alluredir=/XXX/my_allure_results
查看allure的測試報告命令
allure serve allure報告目錄 相對/絕對
eg:allure serve D:\reports\allure
pytest之jenkins集成
安裝插件Allure Jenkins Plugin
配置工具路徑D:\allure-2.13.5
配置時構(gòu)建后操作生成allure報告,選擇allure report并配置路徑(相對)
六、分布式
master/slave模式
分擔(dān)jenkins服務(wù)器的壓力,任務(wù)分配到其它執(zhí)行機來執(zhí)行
master:jenkins服務(wù)器
slave:執(zhí)行機(奴隸機),執(zhí)行master分配的任務(wù),并返回任務(wù)的進度和結(jié)果
master
管理節(jié)點
分配任務(wù)
slave
反饋狀態(tài)
反饋任務(wù)進度
反饋任務(wù)結(jié)果
master/slave
slave向master注冊
slave的狀態(tài),空閑/忙碌
slave的能力,可并行執(zhí)行任務(wù)
七、配置
節(jié)點管理新建節(jié)點
全局設(shè)置–代理–選擇”隨機選取“
節(jié)點管理新建節(jié)點
名字 - 可以唯一指定
執(zhí)行器數(shù)量 - 可以同時執(zhí)行的任務(wù)數(shù)
遠程工作目錄 - 執(zhí)行機的目錄,會自動在該目錄下創(chuàng)建workspace,并建相應(yīng)的job目錄
標(biāo)簽 - 可以指定一組中隨機一個執(zhí)行
用法 - 指定
啟動方式 - Launch agent by connecting it to the master(利用java web連接)
可用性 - 盡可能使用
節(jié)點屬性
可以設(shè)置執(zhí)行機的環(huán)境變量和工具
連接
連接處下載slave-agent.jnlp直接在執(zhí)行機運行
可以安裝為系統(tǒng)服務(wù),這樣的化可以靜默執(zhí)行
連接后就可以運行了
或者在命令行中啟動節(jié)點
可以執(zhí)行了
????????????? 【下面是我整理的2023年最全的軟件測試工程師學(xué)習(xí)知識架構(gòu)體系圖】
一、Python編程入門到精通
二、接口自動化項目實戰(zhàn)??
三、Web自動化項目實戰(zhàn)
四、App自動化項目實戰(zhàn)??
五、一線大廠簡歷
六、測試開發(fā)DevOps體系??
七、常用自動化測試工具
八、JMeter性能測試??
九、總結(jié)(尾部小驚喜)
生命不息,奮斗不止。每一份努力都不會被辜負,只要堅持不懈,終究會有回報。珍惜時間,追求夢想。不忘初心,砥礪前行。你的未來,由你掌握!
生命短暫,時間寶貴,我們無法預(yù)知未來會發(fā)生什么,但我們可以掌握當(dāng)下。珍惜每一天,努力奮斗,讓自己變得更加強大和優(yōu)秀。堅定信念,執(zhí)著追求,成功終將屬于你!文章來源:http://www.zghlxwxcb.cn/news/detail-629226.html
只有不斷地挑戰(zhàn)自己,才能不斷地超越自己。堅持追求夢想,勇敢前行,你就會發(fā)現(xiàn)奮斗的過程是如此美好而值得。相信自己,你一定可以做到!?文章來源地址http://www.zghlxwxcb.cn/news/detail-629226.html
到了這里,關(guān)于Pytest簡介及jenkins集成的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!