安裝
pip install pytest
// 自動(dòng)生成HTML格式測(cè)試報(bào)告
pip install pytest-html
PyCharm配置pytest
- 配置
點(diǎn)擊PyCharm->Preferences->Tools->Python Intergrated Tools->Testing->Default test runner->選擇pytest->點(diǎn)擊OK
- 運(yùn)行
在有測(cè)試函數(shù)的文件中右鍵點(diǎn)擊綠色小三角打厘,可運(yùn)行自動(dòng)化測(cè)試
pytest檢查項(xiàng)目
配置:點(diǎn)擊PyCharm—>Preferences—>Tools—>Externals Tools—>點(diǎn)擊+
image
參數(shù)如下:
- Name:
pytest
- Program:
/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest
(pytest所在目錄刽辙,可通過which pytest查看路徑) - Working directory:
$FileDir$
(表示文件路徑蓖议,不包含文件名)
配置完成是牢,使用時(shí)點(diǎn)擊PyCharm—>Tools—>External Tools—>pytest
pytest報(bào)告生成html
配置:點(diǎn)擊PyCharm—>Preferences—>Tools—>Externals Tools—>點(diǎn)擊+
image
參數(shù)如下:
- Name:
pytest
(可自行制定) - Program:
/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest
(pytest所在目錄慎恒,可通過which pytest查看路徑) - Arguments:--html=testReport.html(具體路徑可自行執(zhí)行)
- Working directory:
$FileDir$
(表示文件路徑稳衬,不包含文件名)
配置完成初橘,使用時(shí)點(diǎn)擊PyCharm—>Tools—>External Tools—>pytest
pytest檢查當(dāng)前文件
配置:點(diǎn)擊PyCharm—>Preferences—>Tools—>Externals Tools—>點(diǎn)擊+
image
參數(shù)如下:
- Name:
pytest Current
- Program:
/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest
(pytest所在目錄耐朴,可通過which pytest查看路徑) - Arguments:
(表示當(dāng)前右鍵選中的文件)
- Working directory:
$FileDir$
(表示文件路徑衡楞,不包含文件名)
配置完成吱雏,使用時(shí)點(diǎn)擊PyCharm—>Tools—>External Tools—>pytest Current
測(cè)試樣例規(guī)范
- 測(cè)試文件以 test_ 開頭(或以 _test 結(jié)尾)
- 測(cè)試類以Test開頭(不能包含init方法)
- 測(cè)試函數(shù)以test_開頭
批量執(zhí)行規(guī)則
- pytest會(huì)查找當(dāng)前以及遞歸查找子文件夾下的所有以test_ 開頭(或以 _test 結(jié)尾)的文件
- 在測(cè)試文件中
- pytest會(huì)查找以Test開頭的類,以test_開頭的方法
- pytest會(huì)查找不在類中的以test_開頭的方法或函數(shù)
指定測(cè)試用例
- 指定測(cè)試文件路徑
pytest /path/to/test/file.py
- 指定測(cè)試類
pytest /path/to/test/file.py:TestCase
- 指定測(cè)試方法
pytest another.test::TestClass::test_method
- 指定測(cè)試函數(shù)
pytest /path/to/test/file.py:test_function