1佑吝、pytest介紹
pytest是一個(gè)非常成熟的全功能的Python測(cè)試框架熙宇,主要有以下幾個(gè)特點(diǎn):
1.簡(jiǎn)單靈活鳖擒,容易上手
2.支持參數(shù)化
3.能夠支持簡(jiǎn)單的單元測(cè)試和復(fù)雜的功能測(cè)試,還可以用來做selenium/appnium等自動(dòng)化測(cè)試烫止、接口自動(dòng)化測(cè)試(pytest+requests)
4.pytest具有很多第三方插件蒋荚,并且可以自定義擴(kuò)展,比較好用的如pytest-selenium(集成selenium)烈拒、pytest-html(完美html測(cè)試報(bào)告生成)圆裕、pytest-rerunfailures(失敗case重復(fù)執(zhí)行)广鳍、pytest-xdist(多CPU分發(fā))等
5.測(cè)試用例的skip和xfail處理
6.可以很好的和jenkins集成
7.report框架----allure 也支持了pytest
2荆几、pytest安裝
使用命令
pip install -U pytest
查看是否安裝成功及安裝的版本信息
pytest --version # 會(huì)展示當(dāng)前已安裝版本
pytest官方文檔:
官方文檔:https://docs.pytest.org/en/latest/contents.html
注意pytest有如下約束:
- 所有的單測(cè)文件名都需要滿足test_.py格式或_test.py格式。
- 在單測(cè)文件中赊时,測(cè)試類以Test開頭吨铸,并且不能帶有 init 方法(注意:定義class時(shí),需要以T開頭祖秒,不然pytest是不會(huì)去運(yùn)行該class的)
- 在單測(cè)類中诞吱,可以包含一個(gè)或多個(gè)test_開頭的函數(shù)。
- 在執(zhí)行pytest命令時(shí)竭缝,會(huì)自動(dòng)從當(dāng)前目錄及子目錄中尋找符合上述約束的測(cè)試函數(shù)來執(zhí)行房维。
3、Pytest命名規(guī)范:
1抬纸、測(cè)試模塊文件(.py文件)命名應(yīng)該以 test_開頭或者以_test結(jié)尾咙俩。
2、測(cè)試類命名應(yīng)當(dāng)以Test開頭。表示一個(gè)項(xiàng)目或者一個(gè)模塊的用例集合
3阿趁、測(cè)試用例(函數(shù))命名膜蛔,測(cè)試函數(shù)的方法命名應(yīng)該以test_開頭。
注意:測(cè)試類的不應(yīng)該有構(gòu)造函數(shù)脖阵,也就是不能有__init__
出現(xiàn)
案例新建一個(gè)python文件皂股,命名為:test_example1:
import pytest
class Test_Login:
def test_login(self):
print("測(cè)試登錄")
def test_logout(self):
print("測(cè)試退出")
def test_insert(self):
print("測(cè)試插入")
class Test_Login2:
def test_login(self):
print("測(cè)試登錄2")
def test_logout(self):
print("測(cè)試退出2")
def test_insert(self):
print("測(cè)試插入2")
if __name__ == '__main__':
pytest.main(['-s','test_exmaple.py']) # 調(diào)用pytest的main函數(shù)執(zhí)行測(cè)試
執(zhí)行后,可以看到命黔,運(yùn)行了test_example1文件下Test_Login測(cè)試類和Test_Login2測(cè)試類中的三個(gè)測(cè)試用例
4呜呐、pytest用例的運(yùn)行方式
1、主函數(shù)模式
運(yùn)行所有:pytest.main()
指定模塊:pytest.main(['-vs','test_login.py])
指定目錄:pytes.main(['-vs','./interface_testcase'])
-
通過nodeid指定用例運(yùn)行:nodeid由模塊名悍募,分割符卵史,類名,方法名搜立,函數(shù)名組成
pytest.main(['-vs','./interface_testcase/test_interface.py::test_04_func'])
pytest.main(['-vs','./interface_testcase/test_interface.py::Testinterface::test_04_func'])
2以躯、命令行模式
運(yùn)行所有:pytest
指定模塊:pytest -vs test_login.py
指定目錄:pytes -vs ./interface_testcase
-
通過nodeid指定用例運(yùn)行:nodeid由模塊名,分割符啄踊,類名忧设,方法名,函數(shù)名組成
pytest -vs ./interface_testcase/test_interface.py::test_04_func
pytest -vs ./interface_testcase/test_interface.py::Testinterface::test_04_func
參數(shù)詳解:
-s: 表示輸出調(diào)試信息颠通,包括print打印的信息
-v: 顯示更詳細(xì)的信息
-vs:兩個(gè)參數(shù)一起用
-n:支持多線程或分布式運(yùn)行用例
如:pytest -vs ./testcase/test_login.py -n 2
-return NUM:失敗用例重跑址晕,num失敗后重跑的次數(shù)
-x:表示只要有一個(gè)用例報(bào)錯(cuò),那么測(cè)試停止
--maxfall=2 :出現(xiàn)兩個(gè)用例失敗就停止
-k:根據(jù)測(cè)試用例的步伐字符串指定測(cè)試用例
如:pytest -vs ./testcase -k "ao"
--html ./report/report.html :會(huì)在之指定路徑下生成html的報(bào)告
3顿锰、通過讀取pytest.ini配置文件運(yùn)行
pytest.ini這個(gè)文件它是pytest單元測(cè)試框架的核心配置文件
1谨垃、位置:一般放在項(xiàng)目的跟目錄下
2、編碼:必須是ANSI硼控,可以使用notpad++修改編碼格式
3刘陶、作用:改變pytest默認(rèn)的行為
4、運(yùn)行規(guī)則:不管是主函數(shù)的模式運(yùn)行牢撼,命令模式運(yùn)行匙隔,都會(huì)去讀取這個(gè)配置文件
5、pytest執(zhí)行用例的順序:
unittes框架 :是根據(jù)ASCII的大小來決定執(zhí)行的順序
pytest框架:默認(rèn)從上到下
如果要改變默認(rèn)的執(zhí)行順序熏版,使用mark標(biāo)記
@pytest.mark.run(order=2)