【pytest进阶】pytest之钩子函数
什么是 hook (钩子)函数
比如说你写了一个框架类的程序,你希望这个框架可以“被其他的代码注入”,即别人可以加入代码对你这个框架进行定制化,该如何做比较好?
一种很常见的方式就是约定一个规则,框架初始化时会收集满足这个规则的所有代码(文件),然后把这些代码加入到框架中来,在执行时一并执行。所有这一规则下可以被框架收集到的方法就是hook方法。
pytest 加载插件的方式
- 内置plugins:从代码内部的_pytest目录加载
- 外部插件(第三方插件):通过setuptools entry points机制发现的第三方插件模块
推荐的第三方的pytest的插件:https://docs.pytest.org/en/latest/plugins.html - conftest.py形式的本地插件:测试目录下的自动模块发现机制
查看当前pytest中所有的插件
pytest --trace-config
命令可以查看当前pytest中所有的plugin。
hook函数分类
第一部分:setuptools
引导挂钩要求足够早注册的插件(内部和setuptools插件),可以使用的钩子
- pytest_load_initial_conftests(early_config,parser,args): 在命令行选项解析之前实现初始conftest文件的加载。
- pytest_cmdline_preparse(config,args): (不建议使用)在选项解析之前修改命令行参数。
- pytest_cmdline_parse(pluginmanager,args): 返回一个初始化的配置对象,解析指定的args。
- pytest_cmdline_main(config): 要求执行主命令行动作。默认实现将调用configure hooks和runtest_mainloop。
第二部分: 初始化挂钩
初始化钩子需要插件和conftest.py文件
- pytest_addoption(parser): 注册argparse样式的选项和ini样式的配置值,这些值在测试运行开始时被调用一次。
- pytest_addhooks(pluginmanager): 在插件注册时调用,以允许通过调用来添加新的挂钩
- pytest_configure(config): 允许插件和conftest文件执行初始配置。
- pytest_unconfigure(config): 在退出测试过程之前调用。
- pytest_sessionstart(session): 在Session创建对象之后,执行收集并进入运行测试循环之前调用。
- pytest_sessionfinish(session,exitstatus): 在整个测试运行完成后调用,就在将退出状态返回系统之前。
- pytest_plugin_registered(plugin,manager):一个新的pytest插件已注册。
第三部分: collection 收集钩子
- pytest_collection(session): 执行给定会话的收集协议。
- pytest_collect_directory(path, parent): 在遍历目录以获取集合文件之前调用。
- pytest_collect_file(path, parent) 为给定的路径创建一个收集器,如果不相关,则创建“无”。
- pytest_pycollect_makemodule(path: py._path.local.LocalPath, parent) 返回给定路径的模块收集器或无。
- pytest_pycollect_makeitem(collector: PyCollector, name: str, obj: object) 返回模块中Python对象的自定义项目/收集器,或者返回None。在第一个非无结果处停止
- pytest_generate_tests(metafunc: Metafunc) 生成(多个)对测试函数的参数化调用。
- pytest_make_parametrize_id(config: Config, val: object, argname: str) 返回val 将由@ pytest.mark.parametrize调用使用的给定用户友好的字符串表示形式,如果挂钩不知道,则返回None val。
- pytest_collection_modifyitems(session: Session, config: Config, items: List[Item]) 在执行收集后调用。可能会就地过滤或重新排序项目。
- pytest_collection_finish(session: Session) 在执行并修改收集后调用。
第四部分:测试运行(runtest)钩子
- pytest_runtestloop(session: Session) 执行主运行测试循环(收集完成后)。
- pytest_runtest_protocol(item: Item, nextitem: Optional[Item]) 对单个测试项目执行运行测试协议。
- pytest_runtest_logstart(nodeid: str, location: Tuple[str, Optional[int], str]) 在运行单个项目的运行测试协议开始时调用。
- pytest_runtest_logfinish(nodeid: str, location: Tuple[str, Optional[int], str])在为单个项目运行测试协议结束时调用。
- pytest_runtest_setup(item: Item) 调用以执行测试项目的设置阶段。
- pytest_runtest_call(item: Item) 调用以运行测试项目的测试(调用阶段)。
- pytest_runtest_teardown(item: Item, nextitem: Optional[Item]) 调用以执行测试项目的拆卸阶段。
- pytest_runtest_makereport(item: Item, call: CallInfo[None]) 被称为为_pytest.reports.TestReport测试项目的每个设置,调用和拆卸运行测试阶段创建一个。
- pytest_pyfunc_call(pyfuncitem: Function) 调用基础测试功能。
第五部分:Reporting 报告钩子
- pytest_collectstart(collector: Collector) 收集器开始收集。
- pytest_make_collect_report(collector: Collector) 执行collector.collect()并返回一个CollectReport。
- pytest_itemcollected(item: Item) 我们刚刚收集了一个测试项目。
- pytest_collectreport(report: CollectReport) 收集器完成收集。
- pytest_deselected(items: Sequence[Item]) 要求取消选择的测试项目,例如按关键字。
- pytest_report_header(config: Config, startdir: py._path.local.LocalPath) 返回要显示为标题信息的字符串或字符串列表,以进行终端报告。
- pytest_report_collectionfinish(config: Config, startdir: py._path.local.LocalPath, items: Sequence[Item]) 返回成功完成收集后将显示的字符串或字符串列表。
- pytest_report_teststatus(report: Union[CollectReport, TestReport], config: Config) 返回结果类别,简写形式和详细词以进行状态报告。
- pytest_terminal_summary(terminalreporter: TerminalReporter, exitstatus: ExitCode, config: Config) 在终端摘要报告中添加一个部分。
- pytest_fixture_setup(fixturedef: FixtureDef[Any], request: SubRequest) 执行夹具设置执行。
- pytest_fixture_post_finalizer(fixturedef: FixtureDef[Any], request: SubRequest) 在夹具拆除之后但在清除缓存之前调用,因此夹具结果fixturedef.cached_result仍然可用(不是 None)
- pytest_warning_captured(warning_message: warnings.WarningMessage, when: Literal[‘config’, ‘collect’, ‘runtest’], item: Optional[Item], location: Optional[Tuple[str, int, str]]) (已弃用)处理内部pytest警告插件捕获的警告。
- pytest_warning_recorded(warning_message: warnings.WarningMessage, when: Literal[‘config’, ‘collect’, ‘runtest’], nodeid: str, location: Optional[Tuple[str, int, str]]) 处理内部pytest警告插件捕获的警告。
- pytest_runtest_logreport(report: TestReport) 处理项目的_pytest.reports.TestReport每个设置,调用和拆卸运行测试阶段产生的结果。
- pytest_assertrepr_compare(config: Config, op: str, left: object, right: object) 返回失败断言表达式中的比较的说明。
- pytest_assertion_pass(item: Item, lineno: int, orig: str, expl: str) (实验性的)在断言通过时调用。
第六部分:调试/相互作用钩
很少有可以用于特殊报告或与异常交互的挂钩:
- pytest_internalerror(excrepr: ExceptionRepr, excinfo: ExceptionInfo[BaseException]) 要求内部错误。返回True以禁止对将INTERNALERROR消息直接打印到sys.stderr的回退处理。
- pytest_keyboard_interrupt(excinfo: ExceptionInfo[Union[KeyboardInterrupt, Exit]]) 要求键盘中断。
- pytest_exception_interact(node: Union[Item, Collector], call: CallInfo[Any], report: Union[CollectReport, TestReport]) 在引发可能可以交互处理的异常时调用。
- pytest_enter_pdb(config: Config, pdb: pdb.Pdb) 调用了pdb.set_trace()。
pytest官方文档之hooks:https://docs.pytest.org/en/latest/reference/reference.html#hooks
编写自己的插件,参考官网文档:http://doc.pytest.org/en/latest/_modules/_pytest/hookspec.html
命令行添加自定义参数
parser.addoption()
参数说明:
- name:自定义命令行参数的名字,可以是:“foo”, “-foo” 或 “–foo”
- action:在命令行中遇到此参数时要采取的基本操作类型
- nargs:应该使用的命令行参数的数量
- const:某些操作和nargs选择所需的常量值
- default:如果参数不在命令行中,则生成的默认值。
- type:命令行参数应该转换为的类型
- choices:参数允许值的容器
- required:命令行选项是否可以省略(仅可选)
- help:对参数作用的简要说明
- metavar:用法消息中参数的名称
- dest:要添加到 parse_args() 返回的对象中的属性的名称
使用场景
- 使用 UI 自动化时,可以指定手机设备,或者不同的浏览器
- 接口自动化时,可以指定正式环境,测试环境分别读取不同的配置文件数据
示例
# conftest.py 文件---项目根目录
# 注册一个命令行参数--cmdopt
def pytest_addoption(parser):
parser.addoption(
"--cmdopt",
action="store",
default="devices_info",
help="What is needed to start the session udid and port,type is dict"
)
# 创建一个 fixture 获取命令行参数--cmdopt 的值
@pytest.fixture() # session 作用域的话,每次执行用例不会重启 app
def cmdopt(request):
return request.config.getoption("--cmdopt")
用例中查看自定义参数
# test_simple.py 文件
class TestDemo(object):
def test_004(self, cmdopt):
print("获取到的设备名称:{}".format(cmdopt))
assert 4 == 4
def test_005(self, cmdopt):
print("获取到的设备名称:{}".format(cmdopt))
assert 4 == 5
pytest.main(['test_simple.py::TestDemo',
'-vs', '--cmdopt=iphone'])
获取每个用例详细的执行结果
pytest_runtest_markreport()
可以获取到测试用例的详细执行结果,setup 是否执行成功,call 测试用例是否执行成功,teardown 是否执行成功
# conftest.py 文件
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print('*'*50)
# 获取钩子方法的调用结果
out = yield
print('用例执行结果', out)
# 从钩子方法的调用结果中获取测试报告
report = out.get_result()
print('测试报告:{}'.format(report))
print('步骤:{}'.format(report.when))
print('nodeid:{}'.format(report.nodeid))
print('description:{}'.format(str(item.function.__doc__)))
print(('运行结果: {}'.format(report.outcome)))
print('*'*50)
# 与上面一样,只是通过 when 进行了阶段的区分
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
print('*' * 50)
# 获取钩子方法的调用结果
out = yield
print('用例执行结果', out)
# 从钩子方法的调用结果中获取测试报告
report = out.get_result()
# 通过 when 来区分 setup,call,teardown 这三个阶段
if report.when == "call":
print('测试报告:{}'.format(report))
print('步骤:{}'.format(report.when))
print('nodeid:{}'.format(report.nodeid))
print('description:{}'.format(str(item.function.__doc__)))
print(('运行结果: {}'.format(report.outcome)))
print('*' * 50)
# test_simple.py 文件
class TestDemo(object):
def test_004(self, cmdopt):
assert 4 == 4
def test_005(self, cmdopt):
assert 4 == 5
pytest.main(['test_simple.py::TestDemo',
'-v'])
改变用例执行顺序
pytest_collection_modifyitems(session, items)
可以通过此 hook 函数改变收集到用例的执行顺序
# conftest.py 文件
def pytest_collection_modifyitems(session, items):
print("收集到的测试用例:%s" % items)
random.shuffle(items) # 将收集到的用例进行随机
print("随机排序后的用例:%s" % items)
# test_simple.py 文件
class TestDemo(object):
def test_004(self):
assert 4 == 4
def test_b_002(self):
assert 1 == 1
def test_a_002(self):
assert 1 == 1
def test_005(self):
assert 4 == 5
def test_b_001(self):
assert 1 == 1
def test_a_001(self):
assert 1 == 1
pytest.main(['test_simple.py::TestDemo',
'-v'])
统计测试结果
pytest_terminal_summary(terminalreporter, exitstatus, config)
通过此 hook 函数可以获取到用例执行的结果,通过数,失败数,跳过数,总数,执行时间等
# conftest.py
# 收集测试结果
def pytest_terminal_summary(terminalreporter, exitstatus, config):
print("收集测试结果开始:")
print("*"*30)
print(terminalreporter.stats)
print("total:", terminalreporter._numcollected)
print('passed:', len(terminalreporter.stats.get('passed', [])))
print('failed:', len(terminalreporter.stats.get('failed', [])))
print('error:', len(terminalreporter.stats.get('error', [])))
print('skipped:', len(terminalreporter.stats.get('skipped', [])))
# terminalreporter._sessionstarttime 会话开始时间
duration = time.time() - terminalreporter._sessionstarttime
print('total times:', duration, 's')
print("收集测试结果结束:")
print("*" * 30)
# test_simple.py 文件
class TestDemo(object):
def test_004(self):
assert 4 == 4
def test_b_002(self):
assert 1 == 1
def test_a_002(self):
assert 1 == 1
def test_005(self):
assert 4 == 5
def test_b_001(self):
assert 1 == 1
def test_a_001(self):
assert 1 == 1
pytest.main(['test_simple.py::TestDemo',
'-v'])
============================= 提升自己 ==========================
进群交流、获取更多干货, 请关注微信公众号:
> > > 咨询交流、进群,请加微信,备注来意:sanshu1318 (←点击获取二维码)
> > > 学习路线+测试实用干货精选汇总:
https://www.cnblogs.com/upstudy/p/15859768.html
> > > 【自动化测试实战】python+requests+Pytest+Excel+Allure,测试都在学的热门技术:
https://www.cnblogs.com/upstudy/p/15921045.html
> > > 【热门测试技术,建议收藏备用】项目实战、简历、笔试题、面试题、职业规划:
https://www.cnblogs.com/upstudy/p/15901367.html
> > > 声明:如有侵权,请联系删除。
============================= 升职加薪 ==========================
更多干货,正在挤时间不断更新中,敬请关注+期待。
进群交流、获取更多干货, 请关注微信公众号:
> > > 咨询交流、进群,请加微信,备注来意:sanshu1318 (←点击获取二维码)
> > > 学习路线+测试实用干货精选汇总:
https://www.cnblogs.com/upstudy/p/15859768.html
> > > 【自动化测试实战】python+requests+Pytest+Excel+Allure,测试都在学的热门技术:
https://www.cnblogs.com/upstudy/p/15921045.html
> > > 【热门测试技术,建议收藏备用】项目实战、简历、笔试题、面试题、职业规划:
https://www.cnblogs.com/upstudy/p/15901367.html
> > > 声明:如有侵权,请联系删除。
============================= 升职加薪 ==========================
更多干货,正在挤时间不断更新中,敬请关注+期待。