[翻译]pytest测试框架(二):使用
此文已由作者吳琪惠授權(quán)網(wǎng)易云社區(qū)發(fā)布。
歡迎訪問網(wǎng)易云社區(qū),了解更多網(wǎng)易技術(shù)產(chǎn)品運營經(jīng)驗。
調(diào)用pytest
調(diào)用命令:
python?-m?pytest?[...]上面的命令相當(dāng)于在命令行直接調(diào)用腳本 pytest [...](前提是python已經(jīng)加入環(huán)境變量)
一些幫助信息
pytest?--version???#?shows?where?pytest?was?imported?from?查看版本 pytest?--fixtures??#?show?available?builtin?function?arguments?查看內(nèi)置參數(shù) pytest?-h?|?--help?#?show?help?on?command?line?and?config?file?options?命令行和配置文件幫助失敗后停止
pytest?-x????????????#?stop?after?first?failure?首次失敗后停止運行 pytest?--maxfail=2????#?stop?after?two?failures?兩次失敗后停止執(zhí)行選擇測試用例
pytest?test_mod.py???#?run?tests?in?module?執(zhí)行模塊中的用例 pytest?somepath??????#?run?all?tests?below?somepath?執(zhí)行路徑中的用例 pytest?-k?stringexpr?#?only?run?tests?with?names?that?match?the??執(zhí)行字符串表達(dá)式中的用例#?"string?expression",?e.g.?"MyClass?and?not?method"?如:"MyClass?and?not?method"#?will?select?TestMyClass.test_something?將會選擇TestMyClass.test_something#?but?not?TestMyClass.test_method_simple?而不會選擇TestMyClass.test_method_simple pytest?test_mod.py::test_func??#?only?run?tests?that?match?the?"node?ID",?僅運行匹配"node?ID"的用例#?e.g.?"test_mod.py::test_func"?will?select?如選擇:"test_mod.py::test_func"時只運行test_mod.py文件內(nèi)的test_func?#?only?test_func?in?test_mod.py pytest?test_mod.py::TestClass::test_method??#?run?a?single?method?in?a?single?class?在單獨類中運行單獨方法 pytest?--pyargs?pkg?#?導(dǎo)入pkg,使用其文件系統(tǒng)位置來查找和執(zhí)行用例,執(zhí)行pkg目錄下的所有用例調(diào)試輸出
pytest?--showlocals?#?show?local?variables?in?tracebacks?在tracebacks中顯示本地變量 pytest?-l???????????#?show?local?variables?(shortcut)????在tracebacks中顯示本地變量(快捷方式)pytest?--tb=auto????#?(default)?'long'?tracebacks?for?the?first?and?last(默認(rèn))第一個和最后一個使用長tracebacks信息,其他使用短的#?entry,?but?'short'?style?for?the?other?entries pytest?--tb=long????#?exhaustive,?informative?traceback?formatting??完整的格式化的traceback信息 pytest?--tb=short???#?shorter?traceback?format??短的traceback信息 pytest?--tb=line????#?only?one?line?per?failure??每個錯誤信息顯示一行 pytest?--tb=native??#?Python?standard?library?formatting?標(biāo)準(zhǔn)格式化輸出 pytest?--tb=no??????#?no?traceback?at?all?無traceback信息Python帶有一個內(nèi)置的Python調(diào)試器稱為PDB。pytest可以在命令行選項指定調(diào)用:
pytest?--pdb這將每次失敗時調(diào)用Python調(diào)試器。通常,您可能只希望這樣做的第一個失敗的測試,以了解某些故障情況:
pytest?-x?--pdb???#?drop?to?PDB?on?first?failure,?then?end?test?session?下降到PDB上的第一次失敗,然后結(jié)束測試階段pytest?--pdb?--maxfail=3??#?drop?to?PDB?for?first?three?failures?下降到PDB前三失敗注意,任何失敗的異常信息都會存儲在sys.last_value,sys.last_type 以及 sys_last_traceback。在交互使用中,允許使用任意debug工具進(jìn)行調(diào)試,也可手動訪問異常信息,例如:
>>>?import?sys>>>?sys.last_traceback.tb_lineno42>>>?sys.last_valueAssertionError('assert?result?==?"ok"',)斷點設(shè)置
import?pytestdef?test_function():...pytest.set_trace()????#?invoke?PDB?debugger?and?tracing在pytest2.0.0版本之前,如果你沒有能夠通過 pytest -s 捕捉到trace信息,你只能查閱PDB traceing。在以后的版本中,當(dāng)你輸入PDB traceing,pytest自動禁用捕捉異常信息:
1.捕獲輸出不影響其他測試用例
2.任何prior測試輸出已經(jīng)被捕獲,并且被處理
3.同一個測試中,任何later測試輸出不會被捕獲,輸出內(nèi)容將會直接發(fā)送到sys.stdout。注意,這條也適用于交互式PDB會話已經(jīng)被退出,但測試輸出已經(jīng)開始了,也會繼續(xù)定期運行測試。
從pytest2.4.0版本開始,你可以不需要使用pytest.set_trace()包裝,也不需要禁用pytest -s命令,你可以使用本地Python命令:importpdb;pdb.set_trace()調(diào)用進(jìn)入PBD traceing。
測試執(zhí)行時間
獲取運行最慢的10個測試執(zhí)行時間:
pytest?--durations=10創(chuàng)建JUnitXML格式文件
為了創(chuàng)建一些可以讓Jenkins或者其他持續(xù)繼承服務(wù)器可識別的測試結(jié)果文件,可使用如下命令:
pytest?--junitxml=path記錄xml(2.8版本支持)
如果你想要為一個測試日志提供額外信息,可使用recode_xml_property 參數(shù):
def?test_function(record_xml_property):record_xml_property("example_key",?1)assert?0這段代碼將會為測試用例標(biāo)簽添加一個額外的屬性:example_key="1":
<testcase?classname="test_function"?file="test_function.py"?line="0"?name="test_function"?time="0.0009"><properties><property?name="example_key"?value="1"?/></properties></testcase>注:
record_xml_property
Add extra xml properties to the tag for the calling test.
The fixture is callable with ``(name, value)``, with value being automatically xml-encoded.
注意:這是一個測試中的參數(shù),這個接口在未來版本中可能會被更強大的接口取到,但他本身功能會保留
LogXML: add_global_property(3.0版本支持)
如果你希望在testsuite級別添加一個屬性節(jié)點(properties node),這個屬性會使得所有關(guān)聯(lián)的測試用例都生效。
import?pytest@pytest.fixture(scope="session")def?log_global_env_facts(f):if?pytest.config.pluginmanager.hasplugin('junitxml'):my_junit?=?getattr(pytest.config,?'_xml',?None)my_junit.add_global_property('ARCH',?'PPC')my_junit.add_global_property('STORAGE_TYPE',?'CEPH')@pytest.mark.usefixtures(log_global_env_facts)def?start_and_prepare_env():passclass?TestMe:def?test_foo(self):assert?True上面的代碼將在testsuite下添加一個屬性節(jié)點:
<testsuite?errors="0"?failures="0"?name="pytest"?skips="0"?tests="1"?time="0.006"><properties><property?name="ARCH"?value="PPC"/><property?name="STORAGE_TYPE"?value="CEPH"/></properties><testcase?classname="test_me.TestMe"?file="test_me.py"?line="16"?name="test_foo"?time="0.000243663787842"/></testsuite>注意:這是一個測試中的參數(shù),這個接口在未來版本中可能會被更強大的接口取到,但他本身功能會保留
測試結(jié)果格式化文件
這個功能在3.0版本已經(jīng)棄用,并于4.0版本移除
創(chuàng)建一個machine-readable的文本測試結(jié)果文件,可使用:
pytest?--resultlog=path這些文件會被如PyPy-test web頁面使用,來顯示測試結(jié)果的幾個修改。
在線發(fā)送測試報告服務(wù)
為每個測試失敗用例創(chuàng)建URL:
pytest?--pastebin=failed上面命令將提交測試運行信息到遠(yuǎn)程服務(wù),并且會為每個失敗用例提供一個URL。如果你只是想發(fā)送一個特別的失敗信息,你可以利用 -x 命令像往常一樣選擇測試用例集或者添加用例。
為整個會話日志創(chuàng)建URL:
pytest?--pastebin=all目前僅可以復(fù)制到 http://bpaste.net
禁用插件
使用 -p no 命令,在調(diào)用的時候禁用加載特定的插件,比如,禁用加載doctest插件,這個插件是從text文件列表中執(zhí)行doctest用例,調(diào)用:
pytest?-p?no:doctest使用Python調(diào)用pytest(2.0版本)
直接調(diào)用:
pytest.main()這句語句跟你從命令行執(zhí)行pytest命令是一樣的,他不會出現(xiàn)SystemExit,但會返回exitcode,可選參數(shù):
pytest.main(['-x',?'mytestdir'])給pytest.main指定附加插件:
#?content?of?myinvoke.pyimport?pytestclass?MyPlugin:def?pytest_sessionfinish(self):print("***?test?run?reporting?finishing")pytest.main(["-qq"],?plugins=[MyPlugin()])運行之后,將會顯示 MyPlugin被添加了,他的鉤子函數(shù)被調(diào)用:
$?python?myinvoke.py ***?test?run?reporting?finishing網(wǎng)易云免費體驗館,0成本體驗20+款云產(chǎn)品!?
更多網(wǎng)易技術(shù)、產(chǎn)品、運營經(jīng)驗分享請點擊。
相關(guān)文章:
【推薦】?年輕設(shè)計師如何做好商業(yè)設(shè)計
【推薦】?知物由學(xué)|見招拆招,Android應(yīng)用破解及防護(hù)秘籍
轉(zhuǎn)載于:https://www.cnblogs.com/163yun/p/9814557.html
總結(jié)
以上是生活随笔為你收集整理的[翻译]pytest测试框架(二):使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue小细节(1)
- 下一篇: dp_c_区间dp_g