Pytest測(cè)試框架知識(shí)

1、pytest介紹

pytest是一個(gè)非常成熟的全功能的Python測(cè)試框架,主要有以下幾個(gè)特點(diǎn):

1.簡(jiǎn)單靈活,容易上手

2.支持參數(shù)化

3.能夠支持簡(jiǎn)單的單元測(cè)試和復(fù)雜的功能測(cè)試,還可以用來(lái)做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ù)來(lái)執(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的大小來(lái)決定執(zhí)行的順序

pytest框架:默認(rèn)從上到下

如果要改變默認(rèn)的執(zhí)行順序,使用mark標(biāo)記
@pytest.mark.run(order=2)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容