本文作者:劉春明
原文鏈接:http://blog.csdn.net/liuchunming033/
1、 Pytest 介紹
pytest 是 python 的一種單元測(cè)試框架,與 python 自帶的 unittest 測(cè)試框架類似,但是比 unittest 框架使用起來更簡(jiǎn)潔,效率更高。根據(jù) pytest 的官方網(wǎng)站介紹,它具有如下特點(diǎn):
非常容易上手,入門簡(jiǎn)單,文檔豐富,文檔中有很多實(shí)例可以參考
能夠支持簡(jiǎn)單的單元測(cè)試和復(fù)雜的功能測(cè)試
支持參數(shù)化
執(zhí)行測(cè)試過程中可以將某些測(cè)試跳過,或者對(duì)某些預(yù)期失敗的 case 標(biāo)記成失敗
支持重復(fù)執(zhí)行失敗的 case
支持運(yùn)行由 nose , unittest 編寫的測(cè)試 case
具有很多第三方插件,并且可以自定義擴(kuò)展
方便的和持續(xù)集成工具集成
由于網(wǎng)上 pytest 的中文文檔比較少,自己學(xué)習(xí)過程中,基本上看的就是英文的官方文檔,對(duì)于不想看英文的同學(xué)們,本系列文章希望能夠幫大家一馬。
2、安裝 pytest
與安裝其他的 python 軟件無異,直接使用 pip 安裝。
pip install - U pytest
安裝完成后,可以驗(yàn)證安裝的版本:
py.test - -version
3、一個(gè)實(shí)例
我們可以通過下面的實(shí)例,看看使用 py.test 進(jìn)行測(cè)試是多么簡(jiǎn)單。
## content of test_sample.py
def func(x):
? ?return x + 1
ef test_func():
? ?assert func(3) == 5
這里我們定義了一個(gè)被測(cè)試函數(shù) func ,該函數(shù)將傳遞進(jìn)來的參數(shù)加1后返回。我們還定義了一個(gè)測(cè)試函數(shù) test_func 用來對(duì) func 進(jìn)行測(cè)試。 test_func 中我們使用基本的斷言語句 assert 來對(duì)結(jié)果進(jìn)行驗(yàn)證。
下面來運(yùn)行這個(gè)測(cè)試:
$ py.test
== == == == == == == == == == == == == = test session starts == == == == == == == == == == == == == ==
platform linux - - Python 3.4.1 - - py - 1.4.27 - - pytest - 2.7.1rootdir:
? ?/tmp / doc - exec - 101, inifile:
collected 1 items
test_sample.py F
== == == == == == == == == == == == == == == == = FAILURES == == == == == == == == == == == == == == == == =
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5E
assert 4 == 5E + where 4 = func(3)
test_sample.py: ? ?5:
? ? ? ?AssertionError
== == == == == == == == == == == == = 1 failed in 0.01 seconds == == == == == == == == == == == == =
執(zhí)行測(cè)試的時(shí)候,我們只需要在測(cè)試文件 test_sample 所在的目錄下,運(yùn)行 py.test 即可。 pytest 會(huì)在當(dāng)前的目錄下,尋找以 test 開頭的文件(即測(cè)試文件),找到測(cè)試文件之后,進(jìn)入到測(cè)試文件中尋找 test_ 開頭的測(cè)試函數(shù)并執(zhí)行。
通過上面的測(cè)試輸出,我們可以看到該測(cè)試過程中,一個(gè)收集到了一個(gè)測(cè)試函數(shù),測(cè)試結(jié)果是失敗的(標(biāo)記為 F ),并且在 FAILURES 部分輸出了詳細(xì)的錯(cuò)誤信息,幫助我們分析測(cè)試原因,我們可以看到” assert
func(3) == 5”這條語句出錯(cuò)了,錯(cuò)誤的原因是 func(3)=4,然后我們斷言 func(3) 等于 5。
4、再一個(gè)實(shí)例
當(dāng)需要編寫多個(gè)測(cè)試樣例的時(shí)候,我們可以將其放到一個(gè)測(cè)試類當(dāng)中,如:
## content of test_class.py
class TestClass:
? ?def test_one(self):
? ? ? ?x = "this"
? ? ? ?assert 'h' in x ? ?
? ?def test_two(self):
? ? ? ?x = "hello"
? ? ? ?assert hasattr(x, 'check')
我們可以通過執(zhí)行測(cè)試文件的方法,執(zhí)行上面的測(cè)試:
$ py.test - q test_class.py
.F
== == == == == == == == == == == == == == == == = FAILURES == == == == == == == == == == == == == == == == =
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass object at 0x7fbf54cf5668 >
def test_two(self):x = "hello"> assert hasattr(x, 'check')
E assert hasattr('hello', 'check')
test_class.py: ? ?8:
? ? ? ?AssertionError1 failed, 1 passed in 0.01 seconds
從測(cè)試結(jié)果中可以看到,該測(cè)試共執(zhí)行了兩個(gè)測(cè)試樣例,一個(gè)失敗一個(gè)成功。同樣,我們也看到失敗樣例的詳細(xì)信息,和執(zhí)行過程中的中間結(jié)果。
5、如何編寫 pytest 測(cè)試樣例
通過上面2個(gè)實(shí)例,我們發(fā)現(xiàn)編寫 pytest 測(cè)試樣例非常簡(jiǎn)單,只需要按照下面的規(guī)則:
測(cè)試文件以 test_ 開頭(以 _test 結(jié)尾也可以)
測(cè)試類以 Test 開頭,并且不能帶有 __init__ 方法
測(cè)試函數(shù)以 test_ 開頭
斷言使用基本的 assert 即可
6、如何執(zhí)行 pytest 測(cè)試樣例
執(zhí)行測(cè)試樣例的方法很多種,上面第一個(gè)實(shí)例是直接執(zhí)行 py.test ,第二個(gè)實(shí)例是傳遞了測(cè)試文件給 py.test 。其實(shí) py.test 有好多種方法執(zhí)行測(cè)試:
py.test ? ? ? ? ? ? ? ## run all tests below current dir
py.test test_mod.py ? # run tests in module
py.test somepath ? ? ?# run all tests below somepath
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 select
? ? ? ?# only test_func in test_mod.py
7、測(cè)試報(bào)告
pytest 可以方便的生成測(cè)試報(bào)告,即可以生成 HTML 的測(cè)試報(bào)告,也可以生成 XML 格式的測(cè)試報(bào)告用來與持續(xù)集成工具集成。
生成 HTML 格式報(bào)告:
py.test - -resultlog = path
生成 XML 格式的報(bào)告:
py.test - -junitxml = path
8、如何獲取幫助信息
py.test - -version ?# shows where pytest was imported from
py.test - -fixtures ?# show available builtin function arguments
py.test - h | --help ?# show help on command line and config file options
9、最佳實(shí)踐
其實(shí)對(duì)于測(cè)試而言,特別是在持續(xù)集成環(huán)境中,我們的所有測(cè)試最好是在虛擬環(huán)境中。這樣不同的虛擬環(huán)境中的測(cè)試不會(huì)相互干擾的。
由于我們的實(shí)際工作中,在同一個(gè) Jekins 中,運(yùn)行了好多種不同項(xiàng)目?jī)?cè)的測(cè)試,因此,各個(gè)測(cè)試項(xiàng)目運(yùn)行在各自的虛擬環(huán)境中。
將 pytest 安裝在虛擬環(huán)境中:
1、將當(dāng)前目錄創(chuàng)建為虛擬環(huán)境
virtualenv . ? ? ? ?# create a virtualenv directory in the current directory
source bin/activate ?# on unix
2、在虛擬環(huán)境中安裝 pytest :
pip install pytest
點(diǎn)擊閱讀原文,查看更多 Python 教程和資源
閱讀原文:http://mp.weixin.qq.com/s?__biz=MzAwNDc0MTUxMw==&mid=2649639755&idx=1&sn=329ade4a4e48b9671a3fb6230e2d833e&chksm=833dabadb44a22bb37cfa1d542add99c07a9fdea2ab8dc67610ba2be78b23ad084249cd618e3#rd