參考:https://blog.csdn.net/liuchunming033/category_3193659.html
(一)Pytest簡介
pytest是python的一種單元測試框架,與python自帶的unittest測試框架類似,但是比unittest框架使用起來更簡潔,效率更高。根據pytest的官方網站介紹,它具有如下特點:
- 非常容易上手,入門簡單,文檔豐富,文檔中有很多實例可以參考
- 能夠支持簡單的單元測試和複雜的功能測試
- 支持參數化
- 執行測試過程中可以將某些測試跳過,或者對某些預期失敗的case標記成失敗
- 支持重複執行失敗的case
- 支持運行由nose, unittest編寫的測試case
- 具有很多第三方插件,並且可以自定義擴展
- 方便的和持續集成工具集成
(二)pytest框架的使用
1. 編寫的class和方法必須遵循以下規則
- 測試文件以test_開頭(以_test結尾也可以)
- 測試類以Test開頭(不能小寫test開頭),並且不能帶有 __init__ 方法
- 測試函數以test_開頭
- 斷言使用基本的assert即可
2.用例代碼
import pytest
#1.規定:
# 測試文件以test_開頭(以_test結尾也可以)
# 測試類以Test開頭,並且不能帶有 init 方法
# 測試函數以test_開頭
# 斷言使用基本的assert即可
#2.執行模塊命令:
# pytest 模塊名稱 例如:pytest test_123.py
#3.生成測試報告
# pytest --html=report.html
#4.運行模式
# 模式1:直接運行test_hello.py文件中的所有cases: pytest test_hello.py
#模式2:運行test_hello.py文件中的TestClassOne這個class下的兩個cases: pytest test_hello.py::TestClassOne
# 模式3:運行test_hello.py文件中的TestClassTwo這個class下的test_one: pytest test_hello.py::TestClassTwo::test_one
class TestClassOne(object):
def test_one(self):
x = "this"
assert 't'in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
class TestClassTwo(object):
def test_one(self):
x = "iphone"
assert 'p'in x
def test_two(self):
x = "apple"
assert hasattr(x, 'check')
3.用例運行總結爲以下三種運行模式
- 模式1:直接運行test_hello.py文件中的所有cases: pytest test_hello.py
- 模式2:運行test_hello.py文件中的TestClassOne這個class下的兩個cases: pytest test_hello.py::TestClassOne
- 模式3:運行test_hello.py文件中的TestClassTwo這個class下的test_one: pytest test_hello.py::TestClassTwo::test_on
4.生成測試報告
pytest --html=report.html
執行以上命令在項目report.html文件,現在我們用瀏覽器打開report.html看一下
(三) Pytest+Allure環境的搭建
現在我們想要用例執行完成後,使用可視化界面展示用例的執行情況,方便查看測試報告,我們要使用到可是化工具allure
環境代建步驟:
1.安裝JDK1.8,並配置環境變量:http://www.oracle.com/technetwork/java/javase/downloads/index.html
3.解壓好allure,把allure配置環境變量,例如D:\Program Files\allure-2.7.0\bin配置在系統環境變量中
4.驗證環境變量是否配置成功:allure --version
5.安裝pytest,命令:pip install pytest
完成以上步驟,現在我們驗證一下用例執行完成,allure是否可以展示用例的執行情況
測試代碼如下:
#!/usr/bin/env python
# coding=utf-8
import pytest
import allure
# allure generate --clean report 生成測試報告
@allure.feature('購物車功能') # 用feature說明產品需求,可以理解爲JIRA中的Epic
class TestShoppingTrolley(object):
@allure.story('加入購物車') # 用story說明用戶場景,可以理解爲JIRA中的Story
def test_add_shopping_trolley(self):
login('劉春明', '密碼') # 步驟1,調用“step函數”
with allure.step("瀏覽商品"): # 步驟2,step的參數將會打印到測試報告中
allure.attach('筆記本', '商品1') # attach可以打印一些附加信息
allure.attach('手機', '商品2')
with allure.step("點擊商品"): # 步驟3
pass
with allure.step("校驗結果"): # 步驟4
allure.attach('添加購物車成功', '期望結果')
allure.attach('添加購物車失敗', '實際結果')
assert 'success' == 'failed'
@allure.story('修改購物車')
def test_edit_shopping_trolley(self):
pass
@pytest.mark.skipif(reason='本次不執行')
@allure.story('刪除購物車中商品')
def test_delete_shopping_trolley(self):
pass
@allure.step('用戶登錄') # 將函數作爲一個步驟,調用此函數時,報告中輸出這個步驟,我把這樣的函數叫“step函數”
def login(user, pwd):
print(user, pwd)
上面使用了Allure的幾個特性:
- @allure.feature # 用於描述被測試產品需求
- @allure.story # 用於描述feature的用戶場景,即測試需求
- with allure.step # 用於描述測試步驟,將會輸出到報告中
- allure.attach # 用於向測試報告中輸入一些附加的信息,通常是一些測試數據,截圖等
- @pytest.allure.step # 用於將一些通用的函數作爲測試步驟輸出到報告,調用此函數的地方會向報告中輸出步驟
生成測試報告需要執行命令:
首先執行pytest -s --alluredir=report命令,生成的文件是json格式,怎麼辦?
執行命令:allure generate --clean report 另外生成一個allure-report文件夾,用瀏覽器打開index.html即可