Pythons:python 3.5, 3.6, 3.7, PyPy3
平臺:Linux和Windows
PyPI包名:pytest
PDF文檔:最新下載
pytest是一個方便構建簡單的和可擴展的測試用例的測試框架。其測試用例不再需要冗余的代碼,它們更直觀,可讀性更好。
1.1 pytest安裝
pytest安裝非常簡單,只要用python自帶的安裝工具pip,在命令行中執行如下命令即可:
```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 創建你的第一個用例
用4行代碼寫一個簡單的測試用例
```python
# content of test_sample.py
def func(x):
??? return x + 1
def test_answer():
??? assert func(3) == 5
```
然后我們來運行這個用例
```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 =============================
```
運行結果會生成一個失敗的報告,因為很明顯,func(3)返回的是4,而不是5。
說明:您可以使用assert語句來驗證測試的預期結果。pytest的高級斷言自省功能會自動幫助您打印出assert語句的中間值,這樣您就可以不需要像JUnit一樣,使用大量的不同的assert語句。
1.3 運行多個用例
pytest會運行當前目錄以及其子目錄下的所有以test_*.py或*_test.py的文件,例如:在當前目錄下或其子目錄下有test_sampy.py或者sampy_test.py文件,pytest就可以找出這個文件,并運行這個文件里的測試用例。如果想要了解更詳細的查找test的規則,可以參看23.2節。
1.4 如何對拋出的異常進行斷言
有時候程序會拋出一些異常來表示程序執行到了某些非正常的流程,這時候,我們如何實現用例來預期或斷言這些拋出的異常呢?
pytest中可以使用raises來做到這點。
```python
# content of test_sysexit.py
import pytest
# 功能函數,拋出一個SystemExit的異常
def f():
??? raise SystemExit(1)
# 測試用例,用ptest.raises期盼SystemExit異常
def test_mytest():
??? with pytest.raises(SystemExit):
???????? f()
```
以"quiet"模式運行以上用例,得到如下結果:
```python
$ pytest -q test_sysexit.py
. [100%]
1 passed in 0.01s
1.5 用類來分組多個用例
在我們為某個場景寫了多個測試用例的時候,我們也許會覺得給這些測試用例分個組會更好些。用類來進行分組也許是一個好的選擇。pytest可以很容易的建一個類并包含多個測試用例:
```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")
```
根據pytest用例收集規則,如下命令就可以執行前面類里面的兩個用例。
```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
```
第一個用例過了,但第二個失敗了。從assert給出的中間值可以看出,我們預期的是"check", 但實際的確是"hello",實際與預期不符,所以失敗了。
1.6 為測試用例申請一個其專用的臨時目錄
pytest提供了一些內建的fixture/function參數作為測試用例專用的資源,專用臨時目錄(tmpdir)就是其中之一 。
```python
# content of test_tmpdir.py
def test_needsfiles(tmpdir):
??? print(tmpdir)
??? assert 0
```
pytest一旦檢測到測試用例的參數列表中帶有tmpdir這個關鍵字,就會去建一個相應的專用目錄,并將這個目錄的路徑通過tmpdir這個參數傳給用例。當測試用例執行完之后,這個目錄就會被刪除。
```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
```
想要對tmpdir有更多的了解,可以參看《第8章 臨時目錄和文件》。
下面這條命令可以查看pytest所有的內置的固有(fixtures)參數:
```python
pytest --fixtures # shows builtin and custom fixtures
```
如果想要查看下劃線(_)開頭的fixtures參數,需要在上面命令后面加上-v。
附一:pytest用例收集規則
1. 如果沒有指定文件,pytest會從當前目錄及其子目錄收集所有的test_*.py或*_test.py文件;
2. 如果指定文件,pytest從指定文件中去收集用例,文件名的格式需要遵循test_*.py或*_test.py;
3. 類名需要以Test作為前綴;
4. 用例名需要以test_作為前綴。