pytest-第一次學習梳理
內(nèi)容全部參考:https://cloud.tencent.com/developer/search/article-pytest文檔50
- 1-環(huán)境準備與入門
- 2-用例運行規(guī)則
- 3-pycharm運行pytest
- 4-測試用例setup和teardown
- 5-fixture之conftest.py
- 6-fixture之yield實現(xiàn)teardown
- 7-生成html報告
- 8-html報告報錯截圖+失敗重跑
- 9-參數(shù)化parametrize
- 10-命令行傳參addoption
- 11-assert斷言
- 12-skip跳過用例
- 16-標記失敗xfail
- 18-配置文件pytest.ini
- 19-doctest測試框架
- 26-conftest.py作用范圍
- 27-運行上次失敗用例(--lf 和 --ff)
- 28-重復執(zhí)行用例(pytest-repeat)
- 29-allure-pytest(報告形式)
- 30-功能用例與自動化用例完美對接(allure)
- 31-allure標記用例級別severity
- 32-allure描述用例詳細講解
- 33-Hooks函數(shù)獲取用例執(zhí)行結(jié)果(pytest_runtest_makereport)
- 34-Hooks函數(shù)改變用例執(zhí)行順序(pytest_collection_modifyitems)
- 35-Hooks函數(shù)之統(tǒng)計測試結(jié)果(pytest_terminal_summary)
- 36-斷言失敗后還能繼續(xù)執(zhí)行pytest-assume
- 37-自定義用例順序(pytest-ordering)
- 38-allure.setp添加測試用例步驟
- 39-參數(shù)化(parametrize)結(jié)合allure.title()生成不同標題報告
- 40-pytest.ini配置用例查找規(guī)則(面試題)
- 41-參數(shù)化 ids 用例描述為中文時控制臺輸出unicode
- 42-fixture參數(shù)化params
- 43-元數(shù)據(jù)使用(pytest-metadata)
- 44-allure.dynamic動態(tài)生成用例標題
- 45-allure添加環(huán)境配置(environment)
- 46-關(guān)于https請求警告問題
- 47-allure報告添加用例失敗截圖
1-環(huán)境準備與入門
2-用例運行規(guī)則
文件名以test_.py文件和test.py
以test_開頭的函數(shù)
以Test開頭的類
以test_開頭的方法
所有的包pakege必須要有__init_.py文件
以下是測試功能的可能結(jié)果:
PASSED (.):測試成功。
FAILED (F):測試失敗(或XPASS + strict)。
SKIPPED (s): 測試被跳過。 你可以使用@pytest.mark.skip()或 pytest.mark.skipif()修飾器告訴pytest跳過測試
xfail (x):預期測試失敗。@pytest.mark.xfail()
XPASS (X):測試不應(yīng)該通過。
ERROR (E):錯誤
3-pycharm運行pytest
pycharm設(shè)置pytest
新建一個工程后,左上角file->Setting->Tools->Python Integrated Tools->項目名稱->Default test runner->選擇py.test

4-測試用例setup和teardown
用例運行級別
模塊級(setup_module/teardown_module)開始于模塊始末,全局的
函數(shù)級(setup_function/teardown_function)只對函數(shù)用例生效(不在類中)
類級(setup_class/teardown_class)只在類中前后運行一次(在類中)
方法級(setup_method/teardown_method)開始于方法始末(在類中)
類里面的(setup/teardown)運行在調(diào)用方法的前后
5-fixture之conftest.py
firture相對于setup和teardown來說應(yīng)該有以下幾點優(yōu)勢
命名方式靈活,不局限于setup和teardown這幾個命名
conftest.py 配置里可以實現(xiàn)數(shù)據(jù)共享,不需要import就能自動找到一些配置
scope=”module” 可以實現(xiàn)多個.py跨文件共享前置
scope=”session” 以實現(xiàn)多個.py跨文件使用一個session來完成多個用例
6-fixture之yield實現(xiàn)teardown
7-生成html報告
安裝:pip install pytest-html
執(zhí)行:打開cmd,cd到需要執(zhí)行pytest用例的目錄,執(zhí)行指令:pytest —html=report.html
執(zhí)行完之后,在當前目錄會生成一個report.html的報告文件,顯示效果如下
8-html報告報錯截圖+失敗重跑
9-參數(shù)化parametrize
- pytest.mark.parametrize裝飾器可以實現(xiàn)測試用例參數(shù)化。
# 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"])
運行結(jié)果
================================== 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 ======================
- 也可以標記單個測試實例在參數(shù)化,例如使用內(nèi)置的mark.xfail
- 獲得多個參數(shù)化參數(shù)的所有組合,可以堆疊參數(shù)化裝飾器
10-命令行傳參addoption
命令行參數(shù)是根據(jù)命令行選項將不同的值傳遞給測試函數(shù),比如平常在cmd執(zhí)行”pytest —html=report.html”,這里面的”—html=report.html“就是從命令行傳入的參數(shù)
對應(yīng)的參數(shù)名稱是html,參數(shù)值是report.html
contetest配置參數(shù)
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.根據(jù)某些條件跳過模塊中的所有測試
pytestmark = pytest.mark.skipif(sys.platform == “win32”, “tests for linux?→ only”
3.如果缺少某些導入,則跳過模塊中的所有測試
pexpect = pytest.importorskip(“pexpect”)
16-標記失敗xfail
pytest里面用xfail標記用例為失敗的用例,可以直接跳過。實現(xiàn)基本思路
把登錄寫為前置操作
對登錄的賬戶和密碼參數(shù)化,參數(shù)用canshu = [{“user”:”amdin”, “psw”:”111”}]表示
多個用例放到一個Test_xx的class里
test_01,test_02, test_03全部調(diào)用fixture里面的login功能
test_01測試登錄用例
test_02和test_03執(zhí)行前用if判斷登錄的結(jié)果,登錄失敗就執(zhí)行,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 代碼片段,然后嘗試執(zhí)行并驗證結(jié)果doctest測試用例可以放在兩個地方
函數(shù)或者方法下的注釋里面
模塊的開頭
>>> multiply(4, 3)
12
>>> multiply('a', 3)
'aaa'
def multiply(a, b):
"""
fuction: 兩個數(shù)相乘
# 22-pytest分布式執(zhí)行(pytest-xdist)
>>> multiply(4, 3)
12
>>> multiply('a', 3)
'aaa'
"""
return a * b
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)

浙公網(wǎng)安備 33010602011771號