Pytest測(cè)試框架基本使用方法詳解
pytest介紹
pytest是一個(gè)非常成熟的全功能的Python測(cè)試框架,主要特點(diǎn)有以下幾點(diǎn):
1、簡(jiǎn)單靈活,容易上手,文檔豐富;
2、支持參數(shù)化,可以細(xì)粒度地控制要測(cè)試的測(cè)試用例;
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ā))、
- pytest--ordering(控制測(cè)試運(yùn)行的順序)
5、測(cè)試用例的skip和xfail處理;
6、可以很好的和CI工具結(jié)合,例如jenkins
編寫(xiě)規(guī)則:
- 測(cè)試文件以test_開(kāi)頭(以_test結(jié)尾也可以)
- 測(cè)試類以Test開(kāi)頭,并且不能帶有 init 方法
- 測(cè)試函數(shù)以test_開(kāi)頭
斷言使用基本的assert即可
快速示例
test_pyexample.py
import pytest
class TestClass:
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
def test_three(self):
a = "hello"
b = "hello world"
assert a in b
通過(guò)命令行運(yùn)行:
1、cd 到代碼所在的目錄,執(zhí)行命令:py.test test_pyexample.py
2、安裝pytest-sugar插件可以看到進(jìn)度條
Pycharm配置運(yùn)行:
1.file->Setting->Tools->Python Integrated Tools->項(xiàng)目名稱->Default test runner->選擇py.test
import pytest
class TestClass:
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
def test_three(self):
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main('-q test_class.py')
Console常用參數(shù)介紹:
- -v 用于顯示每個(gè)測(cè)試函數(shù)的執(zhí)行結(jié)果
- -q 只顯示整體測(cè)試結(jié)果
- -s 用于顯示測(cè)試函數(shù)中print()函數(shù)輸出
- -x, --exitfirst, exit instantly on first error or failed test
- -m 只運(yùn)行帶有裝飾器配置的測(cè)試用例
- -h 幫助
py.test # run all tests below current dir py.test test_mod.py # run tests in module file test_mod.py py.test somepath # run all tests below somepath like ./tests/ py.test -k stringexpr # only run tests with names that match the # the "string expression", e.g. "MyClass and not method" # will select TestMyClass.test_something # but not TestMyClass.test_method_simple py.test test_mod.py::test_func # only run tests that match the "node ID", # e.g "test_mod.py::test_func" will be selected # only run test_func in test_mod.py
pytest參數(shù)化
使用裝飾器:@pytest.mark.parametrize()
單個(gè)參數(shù):
import pytest
import random
@pytest.mark.parametrize('x',[(1),(2),(6)])
def test_add(x):
print(x)
assert x==random.randrange(1,7)
多個(gè)參數(shù):
import pytest
@pytest.mark.parametrize('x,y',[
(1+2,3),
(2-0,1),
(6*2,12),
(10*2,3),
("test","test"),
])
def test_add(x,y): #必須與上面保持一致,只能用x,y不能用其他字母
assert x==y
控制測(cè)試運(yùn)行順序
安裝pytest-ordering
pip install pytest-ordering
借助于裝飾器@pytest.mark.run(order=1)控制測(cè)試運(yùn)行的順序
import pytest
import time
value=0
@pytest.mark.run(order=2) #后執(zhí)行order=2
def test_add2():
print("I am 2")
time.sleep(2)
assert value==10
@pytest.mark.run(order=1) #先執(zhí)行order=1
def test_add():
print("I am add")
global value
value=10
assert value==10
運(yùn)行后生成測(cè)試報(bào)告(htmlReport)
安裝pytest-html:
pip install -U pytest-html
如何使用:
py.test test_pyexample.py --html=report.html
更詳細(xì)的測(cè)試報(bào)告
安裝 pytest-cov:
pip install pytest-cov
如何使用
py.test --cov-report=html --cov=./ test_code_target_dir Console參數(shù)介紹 --cov=[path], measure coverage for filesystem path (multi-allowed), 指定被測(cè)試對(duì)象,用于計(jì)算測(cè)試覆蓋率 --cov-report=type, type of report to generate: term, term-missing, annotate, html, xml (multi-allowed), 測(cè)試報(bào)告的類型 --cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件 --no-cov-on-fail, do not report coverage if test run fails, default: False,如果測(cè)試失敗,不生成測(cè)試報(bào)告 --cov-fail-under=MIN, Fail if the total coverage is less than MIN. 如果測(cè)試覆蓋率低于MIN,則認(rèn)為失敗
多進(jìn)程運(yùn)行
安裝pytest-xdist:
pip install -U pytest-xdist
如何使用:
py.test test_pyexample.py -n NUM
其中NUM填寫(xiě)并發(fā)的進(jìn)程數(shù)。
重新運(yùn)行失敗的用例
安裝pytest- rerunfailures:
import random
def add(x,y):
return x+y
def test_add():
random_value=random.randint(2,7)
print('random_value:'+str(random_value))
assert add(1,3)==random_value
如何使用:
命令:pytest --reruns 重試次數(shù)
比如:pytest --reruns 3 表示:運(yùn)行失敗的用例可以重新運(yùn)行3次
命令:pytest --reruns 重試次數(shù) --reruns-delay 次數(shù)之間的延時(shí)設(shè)置(單位:秒)
比如:pytest --reruns 3 --reruns-delay 5 表示:(譯:瑞軟四、地類)運(yùn)行失敗的用例可以重新運(yùn)行3次,第一次和第二次的間隔時(shí)間為5秒鐘
另外也可以通過(guò)裝飾器的方式配置:
@pytest.mark.flaky(reruns=3, reruns_delay=5)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python人工智能深度學(xué)習(xí)RNN模型結(jié)構(gòu)流程
這篇文章主要為大家介紹了Python人工智能深度學(xué)習(xí)RNN的模型流程結(jié)構(gòu),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
利用Python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的示例代碼
最近學(xué)習(xí)了字符串,運(yùn)算符,條件語(yǔ)句,循環(huán)語(yǔ)句,我在想可以用我最近學(xué)的東西做什么? 看到運(yùn)算我就想到了可以做一個(gè)簡(jiǎn)易的計(jì)算器,感興趣的可以了解一下2022-11-11
利用python清除移動(dòng)硬盤(pán)中的臨時(shí)文件
本篇文章的目的是在移動(dòng)硬盤(pán)插入到電腦的同時(shí),利用Python自動(dòng)化和Windows服務(wù)刪除掉這些臨時(shí)文件。感興趣的朋友可以了解下2020-10-10
解決Django部署設(shè)置Debug=False時(shí)xadmin后臺(tái)管理系統(tǒng)樣式丟失
這篇文章主要介紹了解決Django部署設(shè)置Debug=False時(shí)xadmin后臺(tái)管理系統(tǒng)樣式丟失的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python和Plotly實(shí)現(xiàn)3D圖形繪制
在當(dāng)今的數(shù)據(jù)分析和可視化領(lǐng)域,Python已經(jīng)成為一種不可或缺的工具,Plotly作為一種高級(jí)的繪圖庫(kù),特別擅長(zhǎng)于創(chuàng)建交互式和3D圖形,下面我們就來(lái)看看Python如何利用Plotly實(shí)現(xiàn)3D圖形繪制吧2024-11-11

