【问题标题】:Expected results of Assertion Errors are not showing up when using Pytest and conftest.py file使用 Pytest 和 conftest.py 文件时未显示断言错误的预期结果
【发布时间】:2020-06-02 22:56:03
【问题描述】:

我正在使用 Pytest 测试我的网站内容。我的 conftest.py 中有一个修复程序,它创建了一个 webdriver 供其他测试参考。在测试运行期间发生错误时,仅显示 AssertionError,而不是在 assert 语句中测试的实际值和预期值。

这是我的 conftest.py 文件:

import os

import pytest
from selenium import webdriver


@pytest.fixture(scope="session")
def setup(request):
    driver_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'drivers')
    driver = webdriver.Chrome()
    session = request.node
    for item in session.items:
        cls = item.getparent(pytest.Class)
        setattr(cls.obj, "driver", driver)

    yield driver
    driver.close()

这是我的高级测试文件:

import pytest

from validations import *


@pytest.mark.usefixtures("setup")
class TestOurServices:
    def test_our_services_direct_navigation(self):
        go_to_our_services(self.driver)
        validate_our_services_content(self.driver)

那么这里是一个输出的例子:

【问题讨论】:

  • 碰巧,这对您有帮助吗? stackoverflow.com/questions/41522767/… 我不太能复制错误,我认为它可能在 go_to_our_services 或 'validate_our_services_content' 中
  • 是的,这帮助很大!谢谢@JackThomson。如果您在直接测试用例之外的辅助函数中使用断言,我没有意识到您必须使用“register_assert_rewrite”函数。我将在下面发布答案

标签: python python-3.x selenium-webdriver pytest


【解决方案1】:

这篇文章帮助解决了这个问题:

pytest assert introspection in helper function

我在辅助函数中有断言,例如上面的validate_our_services_content。他们在一个名为validations.py的文件中

参考上述问题,我创建了一个__init__.py 文件来声明这些验证:

__init__.py

import pytest

pytest.register_assert_rewrite('validations')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多