智能化网站建设,什么样建网站,舟山建设网站公司,建设网站专家在 Pytest 测试框架中#xff0c;conftest.py 是一个特殊的文件#xff0c;用于定义测试会话的共享配置和通用功能。它是 Pytest 的核心功能之一#xff0c;可以用于以下目的#xff1a;
【主要功能】 1、定义共享的 Fixture #xff08;1#xff09;conftest.py 文件可…在 Pytest 测试框架中conftest.py 是一个特殊的文件用于定义测试会话的共享配置和通用功能。它是 Pytest 的核心功能之一可以用于以下目的
【主要功能】 1、定义共享的 Fixture 1conftest.py 文件可以存放常用的测试前置fixture代码供同目录下或子目录中的测试用例直接使用而无需显式导入。
参考前面的文章全面解析 pytest fixture使用方法、实战技巧与最佳实践-CSDN博客 2fixture 是 pytest 用来管理测试用例依赖的核心机制可以用于设置测试前后需要的做的内容通过yield实现例如启动appium server、启动app等。 参考前面的文章全面解析 pytest fixture使用方法、实战技巧与最佳实践-CSDN博客 2、自定义钩子函数Hooks 1Pytest 提供了一系列的钩子Hooks函数用于扩展测试框架行为。例如 a. 在测试会话开始或结束时执行特定代码下面是iOS UI自动化每条用例执行结束后如果failed会截一张图并附加到allure报告中并且把当次执行的结果passed、failed、skipped输出到日志中
pytest.hookimpl(tryfirstTrue, hookwrapperTrue)
def pytest_runtest_makereport(item, call):outcome yieldrep outcome.get_result()if rep.when call:if rep.failed:logger.error(fTest {item.nodeid}: FAILED\n)driver item.funcargs[ios_driver]screenshot_path take_screenshot(driver)with open(screenshot_path, rb) as image_file:allure.attach(image_file.read(), name测试未通过截图, attachment_typeallure.attachment_type.PNG)if call.excinfo:error_message str(call.excinfo.value)logger.error(fTest {item.nodeid} failed with error: {error_message}\n)elif rep.skipped:outcome_status SKIPPEDlogger.info(fTest {item.nodeid}: {outcome_status}\n)else:outcome_status PASSEDlogger.info(fTest {item.nodeid}: {outcome_status}\n) b. 修改收集测试用例的行为。 2这些钩子函数通常也会放在 conftest.py 中。 3、参数化和共享配置 可以在 conftest.py 中为多个测试用例设置公共的参数化数据或共享变量或者通过conftest.py 集中管理其他fixture例如在本次的iOS UI自动化项目中把用到的设备数据单独放在了test_data_fixture.py中数据格式如下在conftest.py中引用test_data_fixture.py通过 conftest.py 来集中管理所有 fixture避免了在每个测试文件中都需要导入多个 fixture 文件
# conftest.pyfrom test_data_fixture import *
# test_data_fixture.py# 设备数据
pytest.fixture(scopemodule)
def device_data(request):from network_request import get_dev_name_over_requestdev_model request.paramdev_name get_dev_name_over_request(dev_model)if dev_model CCC:result clear_sandbox_log(iPhoneX)return {iphone_model: iPhoneX,sn: CCC2DA110012345,dev_model: CCC,dev_name: dev_name,sleep_time: 25}elif dev_model LLL:result clear_sandbox_log(iPhoneX)return {iphone_model: iPhoneX,sn: CCC2DA110012345,dev_model: CCC,dev_name: dev_name,sleep_time: 35}else:raise ValueError(Unknown device model: {}.format(dev_model))4、灵活的作用域控制 通过设置 fixture 的作用域function、class、module 或 session可以让它们在不同层次的测试中共享减少冗余。 参考前面的文章全面解析 pytest fixture使用方法、实战技巧与最佳实践-CSDN博客 5、层级作用域 每个目录都可以有自己的 conftest.py这些文件会在测试运行时自动被发现且只会作用于其所在目录及子目录中的测试用例。
【conftest.py 的加载规则】 1、不需要显式导入pytest 会自动发现 conftest.py 文件。 2、每个目录下的 conftest.py 仅影响该目录及其子目录的测试用例。 3、同名的 fixture 或配置会覆盖上层目录的内容。
示例 1共享 Fixture 目录结构
project/
│
├── conftest.py
├── tests/
│ ├── test_login.py
│ ├── test_dashboard.py# conftest.pyimport pytest# 定义一个共享的 Fixture
pytest.fixture(scopesession)
def setup_environment():print(\nSetting up the environment)yieldprint(\nTearing down the environment)# test_login.pydef test_login(setup_environment):# 测试用例会自动调用 setup_environment Fixtureprint(\nRunning login test)assert 1 1# test_hahahah.pydef test_hahahah(setup_environment):# 测试用例会自动调用 setup_environment Fixtureprint(\nRunning hahahah test)assert 2 2$ pytest -s
Setting up the environment
Running login test
.
Running hahahah test
.
Tearing down the environment【注意事项】 1、避免在 conftest.py 中包含业务逻辑 conftest.py 应该仅用于配置和工具代码不建议放置实际的测试逻辑。 2、fixture 名称避免冲突 如果在多个 conftest.py 文件中定义了同名的 fixturepytest 会根据目录层级覆盖上层目录的定义。 3、调试时避免命名冲突 如果测试用例或模块中定义了与 conftest.py 中同名的 fixturepytest 优先使用本地的 fixture。
【总结】 conftest.py 是 Pytest 中一个用于集中管理共享配置的工具可以极大地提高测试代码的可维护性和复用性。对于复杂的测试框架来说合理使用 conftest.py 是非常关键的一部分。