深入解析 Pytest 中的 conftest.py:测试配置与复用的利器

发布于:2024-12-18 ⋅ 阅读:(93) ⋅ 点赞:(0)

在 Pytest 测试框架中,conftest.py 是一个特殊的文件,用于定义测试会话的共享配置和通用功能。它是 Pytest 的核心功能之一,可以用于以下目的:

【主要功能】
1、定义共享的 Fixture
(1)conftest.py 文件可以存放常用的测试前置(fixture)代码,供同目录下或子目录中的测试用例直接使用,而无需显式导入。

参考前面的文章:全面解析 pytest fixture:使用方法、实战技巧与最佳实践-CSDN博客
(2)fixture 是 pytest 用来管理测试用例依赖的核心机制,可以用于设置测试前后需要的做的内容(通过yield实现),例如,启动appium server、启动app等。

 参考前面的文章:全面解析 pytest fixture:使用方法、实战技巧与最佳实践-CSDN博客
2、自定义钩子函数(Hooks)
(1)Pytest 提供了一系列的钩子(Hooks)函数,用于扩展测试框架行为。例如:
    a. 在测试会话开始或结束时执行特定代码,下面是iOS UI自动化每条用例执行结束后,如果failed会截一张图,并附加到allure报告中,并且把当次执行的结果passed、failed、skipped输出到日志中:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()

    if rep.when == 'call':
        if rep.failed:
            logger.error(f"Test {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_type=allure.attachment_type.PNG)

            if call.excinfo:
                error_message = str(call.excinfo.value)
                logger.error(f"Test {item.nodeid} failed with error: {error_message}\n")
        elif rep.skipped:
            outcome_status = 'SKIPPED'
            logger.info(f"Test {item.nodeid}: {outcome_status}\n")
        else:
            outcome_status = 'PASSED'
            logger.info(f"Test {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.py


from test_data_fixture import *
# test_data_fixture.py


# 设备数据
@pytest.fixture(scope="module")
def device_data(request):
    from network_request import get_dev_name_over_request

    dev_model = request.param
    dev_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.py


import pytest

# 定义一个共享的 Fixture
@pytest.fixture(scope="session")
def setup_environment():
    print("\nSetting up the environment")
    yield
    print("\nTearing down the environment")
# test_login.py


def test_login(setup_environment):
    # 测试用例会自动调用 `setup_environment` Fixture
    print("\nRunning login test")
    assert 1 == 1

# test_hahahah.py


def test_hahahah(setup_environment):
    # 测试用例会自动调用 `setup_environment` Fixture
    print("\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 文件中定义了同名的 fixture,pytest 会根据目录层级覆盖上层目录的定义。
3、调试时避免命名冲突
      如果测试用例或模块中定义了与 conftest.py 中同名的 fixture,pytest 优先使用本地的 fixture。

【总结】
conftest.py 是 Pytest 中一个用于集中管理共享配置的工具,可以极大地提高测试代码的可维护性和复用性。对于复杂的测试框架来说,合理使用 conftest.py 是非常关键的一部分。


网站公告

今日签到

点亮在社区的每一天
去签到