目錄:
- 安裝及入門
- 使用和調(diào)用方法
- 原有TestSuite使用方法
- 斷言的編寫和報(bào)告
- Pytest fixtures:清晰 模塊化 易擴(kuò)展
- 使用Marks標(biāo)記測(cè)試用例
- Monkeypatching/對(duì)模塊和環(huán)境進(jìn)行Mock
- 使用tmp目錄和文件
- 捕獲stdout及stderr輸出
- 捕獲警告信息
- 模塊及測(cè)試文件中集成doctest測(cè)試
- skip及xfail: 處理不能成功的測(cè)試用例
- Fixture方法及測(cè)試用例的參數(shù)化
- 緩存: 使用跨執(zhí)行狀態(tài)
- unittest.TestCase支持
- 運(yùn)行Nose用例
- 經(jīng)典xUnit風(fēng)格的setup/teardown
- 安裝和使用插件
- 插件編寫
- 編寫鉤子(hook)方法
- 運(yùn)行日志
- API參考
- 優(yōu)質(zhì)集成實(shí)踐
- 片狀測(cè)試
- Pytest導(dǎo)入機(jī)制及sys.path/PYTHONPATH
- 配置選項(xiàng)
- 示例及自定義技巧
- Bash自動(dòng)補(bǔ)全設(shè)置
模塊及測(cè)試文件中集成doctest測(cè)試
默認(rèn)情況下窖张,Pytest按照python doctest
模塊標(biāo)準(zhǔn)test*.txt
模式進(jìn)行匹配啦扬。你也可以通過(guò)使用以下命令更改匹配模式:
pytest --doctest-glob='*.rst'
在命令行上计雌。從版本開始2.9
,--doctest-glob
可以在命令行中多次使用。
3.1版中的新增功能:您可以使用doctest_encoding
ini選項(xiàng)指定將用于這些doctest文件的編碼:
# content of pytest.ini
[pytest]
doctest_encoding = latin1
默認(rèn)編碼為UTF-8余素。
您還可以在所有python模塊(包括常規(guī)python測(cè)試模塊)中從docstrings觸發(fā)doctests的運(yùn)行:
pytest --doctest-modules
您可以將這些更改永久保存在項(xiàng)目中蒙兰,方法是將它們放入pytest.ini文件中,如下所示:
# content of pytest.ini
[pytest]
addopts = --doctest-modules
如果你有一個(gè)這樣的文本文件:
# content of example.rst
hello this is a doctest
>>> x = 3
>>> x
3
和另一個(gè)這樣的:
# content of mymodule.py
def something():
""" a doctest in a docstring
>>> something()
42
"""
return 42
那么你可以在pytest
沒(méi)有命令行選項(xiàng)的情況下調(diào)用:
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
collected 1 item
mymodule.py . [100%]
========================= 1 passed in 0.12 seconds =========================
可以使用getfixture
幫助器使用Fixture方法:
# content of example.rst
>>> tmp = getfixture('tmpdir')
>>> ...
>>>
此外茵乱,在執(zhí)行文本doctest文件時(shí)茂洒,支持使用類,模塊或項(xiàng)目中的Fixture和Autouse Fixture(類上的xUnit設(shè)置)Fixture瓶竭。
標(biāo)準(zhǔn)doctest
模塊提供了一些設(shè)置標(biāo)志來(lái)配置doctest測(cè)試的嚴(yán)格性督勺。在pytest中,您可以使用配置文件啟用這些標(biāo)志斤贰。要使pytest忽略尾隨空格并忽略冗長(zhǎng)的異常堆棧跟蹤智哀,您只需編寫:
[pytest]
doctest_optionflags= NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
pytest還引入了新的選項(xiàng),允許doctests在Python 2和Python 3中運(yùn)行不變:
-
ALLOW_UNICODE
:?jiǎn)⒂脮r(shí)荧恍,u
前綴將從預(yù)期doctest輸出中的unicode字符串中刪除瓷叫。 -
ALLOW_BYTES
:?jiǎn)⒂脮r(shí),b
前綴將從預(yù)期doctest輸出中的字節(jié)字符串中刪除送巡。
與任何其他選項(xiàng)標(biāo)志一樣摹菠,可以pytest.ini
使用doctest_optionflags
ini選項(xiàng)啟用這些標(biāo)志:
[pytest]
doctest_optionflags = ALLOW_UNICODE ALLOW_BYTES
或者,可以通過(guò)doc測(cè)試本身中的內(nèi)聯(lián)注釋啟用它:
# content of example.rst
>>> get_unicode_greeting() # doctest: +ALLOW_UNICODE
'Hello'
默認(rèn)情況下骗爆,pytest僅報(bào)告給定doctest的第一次失敗次氨。如果您想在即使遇到故障時(shí)繼續(xù)測(cè)試,請(qǐng)執(zhí)行以下操作:
pytest --doctest-modules --doctest-continue-on-failure
'doctest_namespace' Fixture方法
3.0版中的新功能摘投。
該doctest_namespace
Fixture方法可用于注入到項(xiàng)目中煮寡,你的文檔測(cè)試運(yùn)行的命名空間。它旨在用于您自己的燈具中犀呼,以提供將它們與上下文一起使用的測(cè)試幸撕。
doctest_namespace
是一個(gè)標(biāo)準(zhǔn)dict
對(duì)象,您可以將要放置在doctest命名空間中的對(duì)象放入其中:
# content of conftest.py
import numpy
@pytest.fixture(autouse=True)
def add_np(doctest_namespace):
doctest_namespace['np'] = numpy
然后可以直接在你的doctests中使用它:
# content of numpy.py
def arange():
"""
>>> a = np.arange(10)
>>> len(a)
10
"""
pass
請(qǐng)注意圆凰,與正常情況一樣conftest.py
杈帐,在conftest所在的目錄樹中發(fā)現(xiàn)了fixture。意味著如果將doctest與源代碼放在一起,則相關(guān)的conftest.py需要位于同一目錄下挑童。在同級(jí)目錄樹中不會(huì)發(fā)現(xiàn)Fixtures累铅!
輸出格式
3.0版中的新功能。
您可以通過(guò)使用選項(xiàng)標(biāo)準(zhǔn)文檔測(cè)試模塊格式的一個(gè)更改失敗您的文檔測(cè)試diff的輸出格式(見doctest.REPORT_UDIFF
站叼,doctest.REPORT_CDIFF
娃兽, doctest.REPORT_NDIFF
,doctest.REPORT_ONLY_FIRST_FAILURE
):
pytest --doctest-modules --doctest-report none
pytest --doctest-modules --doctest-report udiff
pytest --doctest-modules --doctest-report cdiff
pytest --doctest-modules --doctest-report ndiff
pytest --doctest-modules --doctest-report only_first_failure```