“自定义”简短测试摘要信息 pytest

问题描述 投票:0回答:1

我开始学习自动化测试。 当我运行测试时,我想在 selenium NoSuchElementException 错误时获取特定文本(“无添加到购物车按钮”)

我正在尝试:

class TestCart:
    def test_check_btn(self, browser):
        browser.get(link)
        browser.implicitly_wait(5)
        assert browser.find_element(by=By.CSS_SELECTOR, value='.no-such-btn') is not None, 'No Add to Cart button'

这是真实的日志

=============================================================================== short test summary info =============================================================================== 
FAILED test_cart/test_items.py::TestCart::test_check_btn - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css sele...

如何将错误消息替换为我想要的内容?

python selenium pytest assert
1个回答
0
投票

里面

conftest.py

import typing

if typing.TYPE_CHECKING:
    import pytest
    import _pytest._code.code

def pytest_runtest_logreport(report: 'pytest.TestReport'):
    if report.failed:
        try:
            chain: '_pytest._code.code.ExceptionChainRepr' = report.longrepr.chain
            repr_file_location: '_pytest._code.code.ReprFileLocation' = chain[-1][1]
            repr_file_location.message = 'My Message: ' + repr_file_location.message[:32].title()
        except:
            import traceback
            import sys
            traceback.print_exc(file=sys.stderr)
© www.soinside.com 2019 - 2024. All rights reserved.