pytest

1)py -3 -m pip install pytest

2)py.test --version --驗證安裝

2、簡單實例

https://github.com/luopan001/pytest_learn

3、如何編寫pytest測試樣例

通過上面的實例,我們發現編寫pytest測試樣例非常簡單,只需要按照下面的規則:

測試文件以test_開頭(以_test結尾也可以)

測試類以Test開頭,并且不能帶有?__init__ 方法

測試函數以test_開頭

斷言使用基本的assert即可

4、如何執行pytest測試樣例

1)無參數:運行目錄中符合條件的測試函數

>py.test

2)參數是文件名:參數文件中符合條件的測試函數

>py.test 文件名

3)參數是目錄地址:參數目錄中符合條件的測試函數

>py.test 目錄

4)參數是文件地址:參數文件中符合條件的測試函數

>py.test 文件路徑

5)參數是字符串:運行目錄中類名或者函數名稱包含參數字符串的測試函數

>py.test -k two

6)代碼中執行

5、測試報告

pytest可以方便的生成測試報告,即可以生成HTML的測試報告,也可以生成XML格式的測試報告用來與持續集成工具集成。

1)生成xml格式報告

py.test?test_class.py??--junitxml=./log/log.xml

2)生成txt格式報告

py.test?test_class.py??--resultlog=./log/log.txt

3)生成html格式報告。需要安裝pytest的擴展--pytest-html

3.1)安裝 py -3 -m pip install pytest-html

3.2)生成報告 py.test?test_class.py?--html=./log/report.html

4)第3方插件pytest-allure-adaptor--待研究

4.1)安裝

4.2)運行py.test --alluredir ./log

6、fixtures

fixtures不太好翻譯,可看作是夾心餅干最外層的兩片餅干。通常用setup/teardown來表示。它主要用來包裹測試用例,為什么需要這樣的餅干呢?我們以web自動化測試為例,例如,要測試的某系統需要登錄/退出。那么每一條用例執行前都需要登錄,執行完又都需要退出,這樣每條用例重復編寫登錄和退出就很麻煩,當然,你也可以把登錄和退出封裝為方法調用,但是每個用例中都寫調用也很麻煩。有了fixtures就變得簡便很多。

6.1)簡單實例(test_fixtures.py)

#coding=utf-8

import pytest

# 功能函數

def multiply(a,b):

? ? return a * b

# =====fixtures========

def setup_module(module):

? ? print ("\n")

? ? print ("setup_module================>")

def teardown_module(module):

? ? print ("teardown_module=============>")

def setup_function(function):

? ? print ("setup_function------>")

def teardown_function(function):

? ? print ("teardown_function--->")

# =====測試用例========

def test_numbers_3_4():

? ? print ('test_numbers_3_4')

? ? assert multiply(3,4) == 12

def test_strings_a_3():

? ? print('test_strings_a_3')

? ? assert multiply('a',3) == 'aaa'

if __name__ == '__main__':

? ? pytest.main("-s test_fixtures.py")

運行結果:


總結:1)setup_module/teardown_module ? ? ?在所有測試用例執行之后和之后執行。

? ? ? ? ? 2)setup_function/teardown_function ? ?在每個測試用例之后和之后執行。

6.2)第二個實例(test.py)

# coding=utf-8

import pytest

# 功能函數

def multiply(a, b):

? ? return a * b

class TestUM:

? ? # =====fixtures========

? ? def setup(self):

? ? ? ? print("setup----->")

? ? def teardown(self):

? ? ? ? print("teardown-->")

? ? def setup_class(cls):

? ? ? ? print("\n")

? ? ? ? print("setup_class=========>")

? ? def teardown_class(cls):

? ? ? ? print("teardown_class=========>")

? ? def setup_method(self, method):

? ? ? ? print("setup_method----->>")

? ? def teardown_method(self, method):

? ? ? ? print("teardown_method-->>")

? ? # =====測試用例========

? ? def test_numbers_5_6(self):

? ? ? ? print('test_numbers_5_6')

? ? ? ? assert multiply(5, 6) == 30

? ? def test_strings_b_2(self):

? ? ? ? print('test_strings_b_2')

? ? ? ? assert multiply('b', 2) == 'bb'

if __name__ == '__main__':

? ? pytest.main("-s test.py")

運行結果:


總結:1)setup_class/teardown_class??在當前測試類的開始與結束執行。

? ? ? ? ? 2)setup/treadown ? ? ? ? ? ? ? ? ??在每個測試方法開始與結束執行。

? ? ? ? ? 3)setup_method/teardown_method ? ??在每個測試方法開始與結束執行,與setup/treadown級別相同。

7、斷言

1)使用python的assert進行斷言

2)assert可以使用直接使用“==”、“!=”、“<”、“>”、“>=”、"<=" 等符號來比較相等、不相等、小于、大于、大于等于和小于等于。示例:assert add(3,4) == 7

3)assert 可以直接使用 in 和not in 來比較包含與不包含。示例:assert b in a

4)通過assert不需要任何輔助符號,直接判斷對象是否為ture,而assert not 用于判斷是否為false。示例:assert is_prime(13)

8、pycharm如何設置為pytest

1)pycharm中,缺省用的是unittest

2)設置:files-》settings-》tools=》python integrated tools=》設定default test runner為py.test

9、常用參數

pytest -q 靜默模式,只輸出異常case

pytest -v 詳細,顯示明細及case結果標志燈

pytest casefile.py 指定case文件執行

pytest casedir 指定路徑運行

pytest casedir/casefile::caseclass::casefunc 運行具體的case方法、類等

pytest --pyargs pkgname 指定包執行,根據系統文件路徑定位case,后期可以用pip安裝包的方法部署執行case

pytest -k "key_words_1 and not key_words_1" 執行符合key_words_1命名規則的文件、類及方法,忽略key_words_2命名規則的文件、類及方法

pytest -m "mark_name" 需要在指定case方法上添加@pytest.mark.mark_name來指定方法屬于哪個mark

pytest --junitxml=file.xml 生成xml報告,方便對執行結果進一步分析,后期可以通過xmltodict庫轉成json格式分析入庫及自定義報告

10、Pytest框架集成Allure

1)安裝pytest-allure-adaptor

2)使用Allure Pytest Adaptor改造基于Pytest的測試用例

2.1)生成測試報告所需的數據

pytest -s -q --alluredir D:\project_pro\InterfaceTestModel\report\xml

2.2)安裝allure

步驟一,下載zip安裝包:https://bintray.com/qameta/generic/allure2

步驟二,將bin路徑配置到環境變量PATH

步驟三,驗證安裝,allure --version

2.3)生成測試報告

allure generate D:\project_pro\InterfaceTestModel\report\xml -o D:\project_pro\InterfaceTestModel\report\html

2.4)可在pycharm中打開index.html文件即可看到

2.5)定制報告

Feature: 標注主要功能模塊

Story: 標注Features功能模塊下的分支功能

Severity: 標注測試用例的重要級別

Step: 標注測試用例的重要步驟

Issue和TestCase: 標注Issue、Case,可加入URL

@allure.feature('test_module_01')

@allure.story('test_story_01')

@allure.severity('blocker')

@allure.issue("http://www.baidu.com")

@allure.testcase("http://www.testlink.com")

11、allure運行報錯

修改源文件utils.py:


12、進入虛擬環境運行

1)進入工程所在的虛擬環境:

cd D:\project_pro\InterfaceTestModel\venv\Scripts

2)執行

3)生成allure-xml文件

py.test D:\project_pro\InterfaceTestModel\TestCase --alluredir D:\project_pro\InterfaceTestModel\report\xml

4)生成html文件

allure generate D:\project_pro\InterfaceTestModel\report\xml -o D:\project_pro\InterfaceTestModel\report\html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容