通過代碼實例解析Pytest運行流程
pytest的整個測試分成如下6個階段:
1、pytest_configure
插件和conftest.py文件配置初始化等,創(chuàng)建session。
2、pytest_sessionstart
創(chuàng)建session完以后,執(zhí)行collection之前的階段。會調(diào)用pytest_report_header向terminal打印一些環(huán)境信息,比如插件版本,python版本,操作平臺這些等。
3、pytest_collection
測試用例收集以及生成測試輸入的過程,這里還可能包括根據(jù)keywords和marker篩選測試用例的過程。這個過程會涉及多次generate item的調(diào)用,主要關(guān)注如下調(diào)用:
pytest_generate_tests(metafunc): 生成測試項;
pytest_make_parametrize_id(config, val, argname):根據(jù)@pytest.mark.parametrize生成對應(yīng)值;
pytest_collection_modifyitems(session, config, items):所有測試項收集完畢以后調(diào)用,一般用來進行重新排序和二次過濾。
pytest_deselected(items): 有測試項被關(guān)鍵字或者marker過濾掉的時候會被調(diào)用
注意: 通過::語法篩選測試用例的步驟是在之前生成測試用例階段完成的,并不是在deselected里面做的
4、pytest_runtestloop
執(zhí)行篩選過的測試用例, 在pytest_runtest_protocol里面完成包括setup, call, teardown和log打印的過程。主要關(guān)注如下調(diào)用:
pytest_runtest_logstart(nodeid, location):開始執(zhí)行一個新測試項的時候調(diào)用.
注:官方文檔的意思表述的有點模糊,并不是setup/call/teardown階段分別調(diào)用一次,就是函數(shù)命令一直的意思測試開始前打印一次
pytest_runtest_logfinish(nodeid, location): 結(jié)束執(zhí)行一個測試項的時候調(diào)用.
注:同上
pytest_runtest_setup(item): 在pytest_runtest_call執(zhí)行之前調(diào)用.
pytest_runtest_call(item): 執(zhí)行實際的測試過程。
pytest_runtest_teardow(item, nextitem): 在pytest_runtest_call執(zhí)行之后調(diào)用
pytest_fixture_setup(fixturedef, request):執(zhí)行fixture的setup過程(是否執(zhí)行取決于fixture是否需要創(chuàng)建).
pytest_fixture_post_finalizer(fixturedef, request): 執(zhí)行fixture的teardown過程(如果有)。
pytest_runtest_makereport(item, call): 返回給定item和call對應(yīng)的 _pytest.runner.TestReport 對象, 這里的call object我們一般不太接觸,_pytest/runner.py里面有具體的用法可以參考。
pytest_runtest_logreport(report): 在測試的setup/call/teardown階段report更新之后分別被調(diào)用到,可以用when屬性來區(qū)分不同階段。
pytest_report_teststatus(report, config): 返回各個測試階段的result, 可以用when屬性來區(qū)分不同階段。
5、pytest_sessionfinish
所有測試執(zhí)行完畢之后,返回exit status之前的階段。會調(diào)用pytest_terminal_summary向terminal打印一些summary信息,比如pass, fail, error數(shù)量之類的總結(jié)信息。
def pytest_terminal_summary(terminalreporter, exitstatus, config): '''收集測試結(jié)果''' print(terminalreporter.stats) print("total:", terminalreporter._numcollected) print('passed:', len([i for i in terminalreporter.stats.get('passed', []) if i.when != 'teardown'])) print('failed:', len([i for i in terminalreporter.stats.get('failed', []) if i.when != 'teardown'])) print('error:', len([i for i in terminalreporter.stats.get('error', []) if i.when != 'teardown'])) print('skipped:', len([i for i in terminalreporter.stats.get('skipped', []) if i.when != 'teardown'])) # terminalreporter._sessionstarttime 會話開始時間 duration = time.time() - terminalreporter._sessionstarttime print('total times:', duration, 'seconds')
6、pytest_unconfigure
session結(jié)束以后,整個process退出之前的階段。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在VSCode中添加Python解釋器并安裝Python庫的方法
這篇文章主要介紹了在VSCode中添加Python解釋器并安裝Python庫的方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2023-02-02Python實戰(zhàn)項目用PyQt5制作漫畫臉GUI界面
PyQt5 是用來創(chuàng)建Python GUI應(yīng)用程序的工具包。作為一個跨平臺的工具包,PyQt可以在所有主流操作系統(tǒng)上運行,本文主要介紹了如何用PyQt5制作漫畫臉的GUI界面2021-10-10Python實現(xiàn)檢測文件的MD5值來查找重復(fù)文件案例
這篇文章主要介紹了Python實現(xiàn)檢測文件的MD5值來查找重復(fù)文件案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03使用__init__.py將文件夾設(shè)置成Python模塊示例詳解
這篇文章主要為大家介紹了使用__init__.py將文件夾設(shè)置成Python模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Python3實現(xiàn)發(fā)送郵件和發(fā)送短信驗證碼功能
這篇文章主要介紹了Python3實現(xiàn)發(fā)送郵件和發(fā)送短信驗證碼功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01