通過代碼實(shí)例解析Pytest運(yùn)行流程
pytest的整個(gè)測試分成如下6個(gè)階段:
1、pytest_configure
插件和conftest.py文件配置初始化等,創(chuàng)建session。
2、pytest_sessionstart
創(chuàng)建session完以后,執(zhí)行collection之前的階段。會(huì)調(diào)用pytest_report_header向terminal打印一些環(huán)境信息,比如插件版本,python版本,操作平臺(tái)這些等。
3、pytest_collection
測試用例收集以及生成測試輸入的過程,這里還可能包括根據(jù)keywords和marker篩選測試用例的過程。這個(gè)過程會(huì)涉及多次generate item的調(diào)用,主要關(guān)注如下調(diào)用:
pytest_generate_tests(metafunc): 生成測試項(xiàng);
pytest_make_parametrize_id(config, val, argname):根據(jù)@pytest.mark.parametrize生成對(duì)應(yīng)值;
pytest_collection_modifyitems(session, config, items):所有測試項(xiàng)收集完畢以后調(diào)用,一般用來進(jìn)行重新排序和二次過濾。
pytest_deselected(items): 有測試項(xiàng)被關(guān)鍵字或者marker過濾掉的時(shí)候會(huì)被調(diào)用
注意: 通過::語法篩選測試用例的步驟是在之前生成測試用例階段完成的,并不是在deselected里面做的
4、pytest_runtestloop
執(zhí)行篩選過的測試用例, 在pytest_runtest_protocol里面完成包括setup, call, teardown和log打印的過程。主要關(guān)注如下調(diào)用:
pytest_runtest_logstart(nodeid, location):開始執(zhí)行一個(gè)新測試項(xiàng)的時(shí)候調(diào)用.
注:官方文檔的意思表述的有點(diǎn)模糊,并不是setup/call/teardown階段分別調(diào)用一次,就是函數(shù)命令一直的意思測試開始前打印一次
pytest_runtest_logfinish(nodeid, location): 結(jié)束執(zhí)行一個(gè)測試項(xiàng)的時(shí)候調(diào)用.
注:同上
pytest_runtest_setup(item): 在pytest_runtest_call執(zhí)行之前調(diào)用.
pytest_runtest_call(item): 執(zhí)行實(shí)際的測試過程。
pytest_runtest_teardow(item, nextitem): 在pytest_runtest_call執(zhí)行之后調(diào)用
pytest_fixture_setup(fixturedef, request):執(zhí)行fixture的setup過程(是否執(zhí)行取決于fixture是否需要?jiǎng)?chuàng)建).
pytest_fixture_post_finalizer(fixturedef, request): 執(zhí)行fixture的teardown過程(如果有)。
pytest_runtest_makereport(item, call): 返回給定item和call對(duì)應(yīng)的 _pytest.runner.TestReport 對(duì)象, 這里的call object我們一般不太接觸,_pytest/runner.py里面有具體的用法可以參考。
pytest_runtest_logreport(report): 在測試的setup/call/teardown階段report更新之后分別被調(diào)用到,可以用when屬性來區(qū)分不同階段。
pytest_report_teststatus(report, config): 返回各個(gè)測試階段的result, 可以用when屬性來區(qū)分不同階段。
5、pytest_sessionfinish
所有測試執(zhí)行完畢之后,返回exit status之前的階段。會(huì)調(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 會(huì)話開始時(shí)間
duration = time.time() - terminalreporter._sessionstarttime
print('total times:', duration, 'seconds')
6、pytest_unconfigure
session結(jié)束以后,整個(gè)process退出之前的階段。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用Selenium執(zhí)行JavaScript代碼的步驟詳解
Selenium是一個(gè)用于自動(dòng)化瀏覽器操作的工具,可以模擬人工操作,執(zhí)行各種瀏覽器操作,而JavaScript是一種常用的腳本語言,本文將介紹如何在Python中使用Selenium執(zhí)行JavaScript代碼,并給出一些常見的應(yīng)用示例2023-11-11
利用Python產(chǎn)生加密表和解密表的實(shí)現(xiàn)方法
這篇文章主要介紹了利用Python產(chǎn)生加密表和解密表的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
python自動(dòng)化調(diào)用百度api解決驗(yàn)證碼
這篇文章主要介紹了python自動(dòng)化調(diào)用百度api解決驗(yàn)證碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
實(shí)例講解Python編程中@property裝飾器的用法
裝飾器中蘊(yùn)含著很多Python的進(jìn)階技巧,@property也不例外,比如文后會(huì)講到的快速進(jìn)行代碼重構(gòu)的一個(gè)例子,這里我們就來以實(shí)例講解Python編程中@property裝飾器的用法:2016-06-06
Python匿名函數(shù)/排序函數(shù)/過濾函數(shù)/映射函數(shù)/遞歸/二分法
這篇文章主要介紹了Python匿名函數(shù)/排序函數(shù)/過濾函數(shù)/映射函數(shù)/遞歸/二分法 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06

