簡體   English   中英

使用 Pytest 和 conftest.py 文件時未顯示斷言錯誤的預期結果

[英]Expected results of Assertion Errors are not showing up when using Pytest and conftest.py file

我正在使用 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)

然后這是一個輸出示例:

在此處輸入圖片說明

這篇文章幫助解決了這個問題:

pytest 在輔助函數中斷言自省

我在輔助函數中進行了斷言,例如上面的validate_our_services_content 他們在一個名為validations.py的文件中

參考上述問題,我創建了一個__init__.py文件來聲明這些驗證:

__init__.py

import pytest

pytest.register_assert_rewrite('validations')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM