pytest-第一次學習梳理
內容全部參考:https://cloud.tencent.com/developer/search/article-pytest文件50
目錄
- 1-環境準備與入門
- 2-用例執行規則
- 3-pycharm執行pytest
- 4-測試用例setup和teardown
- 5-fixture之conftest.py
- 6-fixture之yield實現teardown
- 7-生成html報告
- 8-html報告報錯截圖+失敗重跑
- 9-引數化parametrize
- 10-命令列傳參addoption
- 11-assert斷言
- 12-skip跳過用例
- 16-標記失敗xfail
- 18-配置檔案pytest.ini
- 19-doctest測試框架
- 26-conftest.py作用範圍
- 27-執行上次失敗用例(--lf 和 --ff)
- 28-重複執行用例(pytest-repeat)
- 29-allure-pytest(報告形式)
- 30-功能用例與自動化用例完美對接(allure)
- 31-allure標記用例級別severity
- 32-allure描述用例詳細講解
- 33-Hooks函式獲取用例執行結果(pytest_runtest_makereport)
- 34-Hooks函式改變用例執行順序(pytest_collection_modifyitems)
- 35-Hooks函式之統計測試結果(pytest_terminal_summary)
- 36-斷言失敗後還能繼續執行pytest-assume
- 37-自定義用例順序(pytest-ordering)
- 38-allure.setp新增測試用例步驟
- 39-引數化(parametrize)結合allure.title()生成不同標題報告
- 40-pytest.ini配置用例查詢規則(面試題)
- 41-引數化 ids 用例描述為中文時控制檯輸出unicode
- 42-fixture引數化params
- 43-元資料使用(pytest-metadata)
- 44-allure.dynamic動態生成用例標題
- 45-allure新增環境配置(environment)
- 46-關於https請求警告問題
- 47-allure報告新增用例失敗截圖
1-環境準備與入門
2-用例執行規則
檔名以test_.py檔案和
以test_開頭的函式
以Test開頭的類
以test_開頭的方法
所有的包pakege必須要有__init_.py檔案
3-pycharm執行pytest
pycharm設定pytest
新建一個工程後,左上角file->Setting->Tools->Python Integrated Tools->專案名稱->Default test runner->選擇py.test
4-測試用例setup和teardown
用例執行級別
模組級(setup_module/teardown_module)開始於模組始末,全域性的
函式級(setup_function/teardown_function)只對函式用例生效(不在類中)
類級(setup_class/teardown_class)只在類中前後執行一次(在類中)
方法級(setup_method/teardown_method)開始於方法始末(在類中)
類裡面的(setup/teardown)執行在呼叫方法的前後
5-fixture之conftest.py
firture相對於setup和teardown來說應該有以下幾點優勢
命名方式靈活,不侷限於setup和teardown這幾個命名
conftest.py 配置裡可以實現資料共享,不需要import就能自動找到一些配置
scope=”module” 可以實現多個.py跨檔案共享前置
scope=”session” 以實現多個.py跨檔案使用一個session來完成多個用例
6-fixture之yield實現teardown
7-生成html報告
安裝:pip install pytest-html
執行:開啟cmd,cd到需要執行pytest用例的目錄,執行指令:pytest —html=report.html
執行完之後,在當前目錄會生成一個report.html的報告檔案,顯示效果如下
8-html報告報錯截圖+失敗重跑
9-引數化parametrize
- pytest.mark.parametrize裝飾器可以實現測試用例引數化。
# content of test_expectation.py
# coding:utf-8
# ** 作者:上海-悠悠 QQ交流群:588402570**
import pytest
@pytest.mark.parametrize("test_input,expected",
[ ("3+5", 8),
("2+4", 6),
("6 * 9", 42),
])
def test_eval(test_input, expected):
assert eval(test_input) == expected
if __name__ == "__main__":
pytest.main(["-s", "test_canshu1.py"])
執行結果
================================== FAILURES ===================================
_____________________________ test_eval[6 * 9-42] _____________________________
test_input = '6 * 9', expected = 42
@pytest.mark.parametrize("test_input,expected",
[ ("3+5", 8),
("2+4", 6),
("6 * 9", 42),
])
def test_eval(test_input, expected):
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6 * 9')
test_canshu1.py:11: AssertionError
===================== 1 failed, 2 passed in 1.98 seconds ======================
- 也可以標記單個測試例項在引數化,例如使用內建的mark.xfail
- 獲得多個引數化引數的所有組合,可以堆疊引數化裝飾器
10-命令列傳參addoption
命令列引數是根據命令列選項將不同的值傳遞給測試函式,比如平常在cmd執行”pytest —html=report.html”,這裡面的”—html=report.html“就是從命令列傳入的引數
對應的引數名稱是html,引數值是report.html
contetest配置引數
11-assert斷言
pytest裡面斷言實際上就是python裡面的assert斷言方法,常用的有以下幾種
assert xx 判斷xx為真
assert not xx 判斷xx不為真
assert a in b 判斷b包含a
assert a == b 判斷a等於b
assert a != b 判斷a不等於b
12-skip跳過用例
pytest.mark.skip可以標記無法在某些平臺上執行的測試功能,或者您希望失敗的測試功能
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
...
def test_function():
if not valid_config():
pytest.skip("unsupported configuration")
概要
1.無條件地跳過模組中的所有測試:
pytestmark = pytest.mark.skip(“all tests still WIP”)
2.根據某些條件跳過模組中的所有測試
pytestmark = pytest.mark.skipif(sys.platform == “win32”, “tests for linux˓→ only”
3.如果缺少某些匯入,則跳過模組中的所有測試
pexpect = pytest.importorskip(“pexpect”)
16-標記失敗xfail
pytest裡面用xfail標記用例為失敗的用例,可以直接跳過。實現基本思路
把登入寫為前置操作
對登入的賬戶和密碼引數化,引數用canshu = [{“user”:”amdin”, “psw”:”111”}]表示
多個用例放到一個Test_xx的class裡
test_01,test_02, test_03全部呼叫fixture裡面的login功能
test_01測試登入用例
test_02和test_03執行前用if判斷登入的結果,登入失敗就執行,pytest.xfail(“登入不成功, 標記為xfail”)
18-配置檔案pytest.ini
pytest配置檔案可以改變pytest的執行方式,它是一個固定的檔案pytest.ini檔案,讀取配置資訊,按指定的方式去執行。
pytest裡面有些檔案是非test檔案
pytest.ini pytest的主配置檔案,可以改變pytest的預設行為
conftest.py 測試用例的一些fixture配置
init.py 識別該資料夾為python的package包
tox.ini 與pytest.ini類似,用tox工具時候才有用
setup.cfg 也是ini格式檔案,影響setup.py的行為
19-doctest測試框架
前言
doctest從字面意思上看,那就是文件測試。doctest是python裡面自帶的一個模組,它實際上是單元測試的一種。
官方解釋:doctest 模組會搜尋那些看起來像互動式會話的 Python 程式碼片段,然後嘗試執行並驗證結果doctest測試用例可以放在兩個地方
函式或者方法下的註釋裡面
模組的開頭
>>> multiply(4, 3)
12
>>> multiply('a', 3)
'aaa'
def multiply(a, b):
"""
fuction: 兩個數相乘
# 22-pytest分散式執行(pytest-xdist)
>>> multiply(4, 3)
12
>>> multiply('a', 3)
'aaa'
"""
return a * b
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)