亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

通過代碼實例解析Pytest運行流程

 更新時間:2020年08月20日 09:30:53   作者:crystal1126  
這篇文章主要介紹了通過代碼實例解析Pytest運行流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

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)文章

最新評論