conftest在pytest框架里面的使用及实现失败截图的功能

在Python框架中,conftest.py文件通常用于共享测试配置和夹具(fixtures)。它位于测试文件或测试目录的顶级位置,并在运行测试时自动加载。

以下是一些在conftest.py中常见的代码示例:

  • 导入必要的库和模块:
import pytest
from selenium import webdriver


# 定义全局的夹具(fixtures):

@pytest.fixture(scope='session')
def browser():
    driver = webdriver.Chrome()  # 初始化浏览器
    yield driver  # 返回浏览器实例
    driver.quit()  # 关闭浏览器


# 定义其他自定义夹具(fixtures):

@pytest.fixture
def login(browser):
    # 登录操作
    # 返回登录后的页面对象或其他需要的数据
    pass

上述示例中的login夹具是一个自定义夹具,它可以在测试函数中作为参数使用,用于模拟登录操作并返回登录后的页面对象或其他需要的数据。

  • 添加钩子函数(hooks):
def pytest_runtest_setup(item):
    # 在每个测试用例运行之前执行的代码
    pass

def pytest_runtest_teardown(item):
    # 在每个测试用例运行之后执行的代码
    pass

上述示例中的pytest_runtest_setuppytest_runtest_teardown是钩子函数,它们可以在每个测试用例运行之前和之后执行自定义的代码。

需要注意的是,conftest.py中的代码可以根据测试需求进行扩展或修改。上述示例只是一种常见的用法,具体的实现取决于你的测试框架和需求。

  • conftest.py中实现失败截图的功能

可以使用pytest提供的钩子函数和Selenium的截图功能,使用钩子函数和Allure API将失败的截图添加到Allure报告中

import allure
import pytest
from selenium import webdriver


# 定义全局的夹具(fixtures)
@pytest.fixture(scope='session')
def browser(request):
    driver = webdriver.Chrome()  # 初始化浏览器
    yield driver  # 返回浏览器实例
    driver.quit()  # 关闭浏览器


# 添加钩子函数
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # 获取测试结果
    outcome = yield
    rep = outcome.get_result()

    if rep.when == 'call' and rep.failed:
        # 获取浏览器实例
        browser = item.funcargs['browser']

        # 截图
        screenshot = browser.get_screenshot_as_png()

        # 将截图添加到Allure报告中
        allure.attach(screenshot, name="Failure Screenshot", attachment_type=allure.attachment_type.PNG)


# 添加Allure报告的额外信息
@pytest.fixture(autouse=True)
def add_extra_info(request):
    # 添加测试用例的名称
    allure.dynamic.title(request.node.name)

    # 添加测试用例的描述
    allure.dynamic.description(request.node.nodeid)


# 添加Allure报告的环境信息
@pytest.fixture(scope='session', autouse=True)
def allure_environment(request):
    # 添加环境信息
    allure.environment(report='Environment', browser='Chrome', environment='Test')


福利
为方便大家自学软件测试,分享更多测试资料
主体内容包含:测试面试题,功能测试、性能测试、自动化测试等学习知识内容。
软件测试自学资料包领取

### pytest框架中的Mark用法 pytest提供了一种灵活的方式通过`mark`来标记测试函数,这使得可以对特定条件下的测试执行不同的操作。例如,在某些情况下可能只想运行带有特定标签的测试,或者跳过一些不适合当前环境配置的测试。 #### 基本语法 要给一个测试加上标志,可以在其定义前使用装饰器形式的应用程序接口(API)。最常用的内置marks包括`skip`, `skipif`, 和 `xfail`. 下面是一个简单的例子展示如何应用这些: ```python import pytest @pytest.mark.skip(reason="no way of currently testing this") def test_the_unknown(): pass ``` 对于更复杂的场景,比如基于条件决定是否应该跳过某个测试,则可利用`skipif`: ```python import sys import pytest @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") def test_functionality_only_on_python_37_and_above(): assert True ``` 当预期失败但仍希望记录下来以便后续审查时,`xfail`非常有用: ```python import pytest @pytest.mark.xfail(strict=True) def test_feature_that_is_broken(): # This will be marked as expected to fail. raise ValueError("This is a known issue.") ``` 除了上述预设外,还可以创建自定义的marks用于分类目的或其他用途: ```python import pytest @pytest.mark.webtest def test_example_website_response(): response = requests.get('http://example.com') assert response.status_code == 200 ``` 为了确保所有使用的非标准marks都被注册到pytest环境中,可以在项目的根目录下创建名为`conftest.py`文件,并在里面声明它们: ```python # content of conftest.py import pytest def pytest_configure(config): config.addinivalue_line( "markers", "webtest: mark tests that require web access." ) ``` 这样做的好处是可以让其他开发者清楚哪些是特殊的测试类别,并且有助于防止拼写错误造成的误解。 最后值得注意的是,可以通过命令行参数指定只运行具有某种特性的测试案例。例如,如果想要仅限于那些被标记为`webtest`的测试,那么就可以这样做: ```bash pytest -v -m webtest ``` 此命令会告诉pytest只挑选含有`@pytest.mark.webtest`修饰符的方法来进行验证[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值