Pythons:python 3.5拦坠, 3.6锭魔, 3.7梁厉, PyPy3
平臺(tái):Linux和Windows
PyPI包名:pytest
PDF文檔:最新下載
pytest是一個(gè)方便構(gòu)建簡(jiǎn)單的和可擴(kuò)展的測(cè)試用例的測(cè)試框架滑废。其測(cè)試用例不再需要冗余的代碼,它們更直觀寂屏,可讀性更好慌洪。
1.1 pytest安裝
pytest安裝非常簡(jiǎn)單,只要用python自帶的安裝工具pip凑保,在命令行中執(zhí)行如下命令即可:
```python
$ pip install -U pytest
```
安裝完成之后,使用下面的命令可以查看安裝的pytest的版本信息涌攻。你可以看到你所安裝的pytest版本是否是你所需要的版本欧引。
```
$ pytest --version
This is pytest version 5.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py
```
1.2 創(chuàng)建你的第一個(gè)用例
用4行代碼寫(xiě)一個(gè)簡(jiǎn)單的測(cè)試用例
```python
# content of test_sample.py
def func(x):
??? return x + 1
def test_answer():
??? assert func(3) == 5
```
然后我們來(lái)運(yùn)行這個(gè)用例
```python
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:6: AssertionError
============================ 1 failed in 0.05s =============================
```
運(yùn)行結(jié)果會(huì)生成一個(gè)失敗的報(bào)告,因?yàn)楹苊黠@恳谎,func(3)返回的是4芝此,而不是5。
說(shuō)明:您可以使用assert語(yǔ)句來(lái)驗(yàn)證測(cè)試的預(yù)期結(jié)果因痛。pytest的高級(jí)斷言自省功能會(huì)自動(dòng)幫助您打印出assert語(yǔ)句的中間值婚苹,這樣您就可以不需要像JUnit一樣,使用大量的不同的assert語(yǔ)句鸵膏。
1.3 運(yùn)行多個(gè)用例
pytest會(huì)運(yùn)行當(dāng)前目錄以及其子目錄下的所有以test_*.py或*_test.py的文件膊升,例如:在當(dāng)前目錄下或其子目錄下有test_sampy.py或者sampy_test.py文件,pytest就可以找出這個(gè)文件谭企,并運(yùn)行這個(gè)文件里的測(cè)試用例廓译。如果想要了解更詳細(xì)的查找test的規(guī)則评肆,可以參看23.2節(jié)。
1.4 如何對(duì)拋出的異常進(jìn)行斷言
有時(shí)候程序會(huì)拋出一些異常來(lái)表示程序執(zhí)行到了某些非正常的流程非区,這時(shí)候瓜挽,我們?nèi)绾螌?shí)現(xiàn)用例來(lái)預(yù)期或斷言這些拋出的異常呢?
pytest中可以使用raises來(lái)做到這點(diǎn)征绸。
```python
# content of test_sysexit.py
import pytest
# 功能函數(shù)久橙,拋出一個(gè)SystemExit的異常
def f():
??? raise SystemExit(1)
# 測(cè)試用例,用ptest.raises期盼SystemExit異常
def test_mytest():
??? with pytest.raises(SystemExit):
???????? f()
```
以"quiet"模式運(yùn)行以上用例管怠,得到如下結(jié)果:
```python
$ pytest -q test_sysexit.py
. [100%]
1 passed in 0.01s
1.5 用類(lèi)來(lái)分組多個(gè)用例
在我們?yōu)槟硞€(gè)場(chǎng)景寫(xiě)了多個(gè)測(cè)試用例的時(shí)候淆衷,我們也許會(huì)覺(jué)得給這些測(cè)試用例分個(gè)組會(huì)更好些。用類(lèi)來(lái)進(jìn)行分組也許是一個(gè)好的選擇排惨。pytest可以很容易的建一個(gè)類(lèi)并包含多個(gè)測(cè)試用例:
```python
# 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")
```
根據(jù)pytest用例收集規(guī)則吭敢,如下命令就可以執(zhí)行前面類(lèi)里面的兩個(gè)用例。
```python
$ pytest -q test_class.py
.F [100%]
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass object at 0xdeadbeef>
def test_two(self):
x = "hello"
> assert hasattr(x, "check")
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_class.py:8: AssertionError
1 failed, 1 passed in 0.05s
```
第一個(gè)用例過(guò)了暮芭,但第二個(gè)失敗了鹿驼。從assert給出的中間值可以看出,我們預(yù)期的是"check"辕宏, 但實(shí)際的確是"hello"畜晰,實(shí)際與預(yù)期不符,所以失敗了瑞筐。
1.6 為測(cè)試用例申請(qǐng)一個(gè)其專(zhuān)用的臨時(shí)目錄
pytest提供了一些內(nèi)建的fixture/function參數(shù)作為測(cè)試用例專(zhuān)用的資源凄鼻,專(zhuān)用臨時(shí)目錄(tmpdir)就是其中之一 。
```python
# content of test_tmpdir.py
def test_needsfiles(tmpdir):
??? print(tmpdir)
??? assert 0
```
pytest一旦檢測(cè)到測(cè)試用例的參數(shù)列表中帶有tmpdir這個(gè)關(guān)鍵字聚假,就會(huì)去建一個(gè)相應(yīng)的專(zhuān)用目錄块蚌,并將這個(gè)目錄的路徑通過(guò)tmpdir這個(gè)參數(shù)傳給用例。當(dāng)測(cè)試用例執(zhí)行完之后膘格,這個(gè)目錄就會(huì)被刪除峭范。
```python
$ pytest -q test_tmpdir.py
F [100%]
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
def test_needsfiles(tmpdir):
print(tmpdir)
> assert 0
E assert 0
test_tmpdir.py:3: AssertionError
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
1 failed in 0.05s
```
想要對(duì)tmpdir有更多的了解,可以參看《第8章 臨時(shí)目錄和文件》瘪贱。
下面這條命令可以查看pytest所有的內(nèi)置的固有(fixtures)參數(shù):
```python
pytest --fixtures # shows builtin and custom fixtures
```
如果想要查看下劃線(xiàn)(_)開(kāi)頭的fixtures參數(shù)纱控,需要在上面命令后面加上-v。
附一:pytest用例收集規(guī)則
1. 如果沒(méi)有指定文件菜秦,pytest會(huì)從當(dāng)前目錄及其子目錄收集所有的test_*.py或*_test.py文件甜害;
2. 如果指定文件,pytest從指定文件中去收集用例球昨,文件名的格式需要遵循test_*.py或*_test.py尔店;
3. 類(lèi)名需要以Test作為前綴;
4. 用例名需要以test_作為前綴。