亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pytest-4.xx:如何報告 XFAILED 等 SKIPPED 測試?

pytest-4.xx:如何報告 XFAILED 等 SKIPPED 測試?

喵喵時光機 2022-01-18 21:31:45
當測試失敗時,打印的原因報告有關測試文件、測試類和測試用例,而跳過的測試用例僅報告測試文件和調用跳過的行。這是一個測試示例:#!/usr/bin/env pytestimport [email protected](reason="Reason of failure")def test_1():    pytest.fail("This will fail here")@pytest.mark.skip(reason="Reason of skipping")def test_2():    pytest.fail("This will fail here")這是實際結果:pytest test_file.py -rsx============================= test session starts =============================platform linux -- Python 3.5.2, pytest-4.4.1, py-1.7.0, pluggy-0.9.0rootdir: /home/ashot/questionscollected 2 items                                                             test_file.py xs                                                         [100%]=========================== short test summary info ===========================SKIPPED [1] test_file.py:9: Reason of skippingXFAIL test_file.py::test_1  Reason of failure==================== 1 skipped, 1 xfailed in 0.05 seconds =====================但我希望得到類似的東西:pytest test_file.py -rsx============================= test session starts =============================platform linux -- Python 3.5.2, pytest-4.4.1, py-1.7.0, pluggy-0.9.0rootdir: /home/ashot/questionscollected 2 items                                                             test_file.py xs                                                         [100%]=========================== short test summary info ===========================XFAIL test_file.py::test_1: Reason of failureSKIPPED test_file.py::test_2: Reason of skipping==================== 1 skipped, 1 xfailed in 0.05 seconds =====================
查看完整描述

2 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

您有兩種可能的方法來實現這一目標。快速而骯臟的方式:只需重新定義_pytest.skipping.show_xfailed您的test_file.py:


import _pytest


def custom_show_xfailed(terminalreporter, lines):

    xfailed = terminalreporter.stats.get("xfailed")

    if xfailed:

        for rep in xfailed:

            pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid)

            reason = rep.wasxfail

            s = "XFAIL %s" % (pos,)

            if reason:

                s += ": " + str(reason)

            lines.append(s)


# show_xfailed_bkp = _pytest.skipping.show_xfailed

_pytest.skipping.show_xfailed = custom_show_xfailed


... your tests

(不是那么)干凈的方法:conftest.py在與您的相同目錄中創建一個文件test_file.py,并添加一個鉤子:


import pytest

import _pytest


def custom_show_xfailed(terminalreporter, lines):

    xfailed = terminalreporter.stats.get("xfailed")

    if xfailed:

        for rep in xfailed:

            pos = terminalreporter.config.cwd_relative_nodeid(rep.nodeid)

            reason = rep.wasxfail

            s = "XFAIL %s" % (pos,)

            if reason:

                s += ": " + str(reason)

            lines.append(s)


@pytest.hookimpl(tryfirst=True)

def pytest_terminal_summary(terminalreporter):

    tr = terminalreporter

    if not tr.reportchars:

        return


    lines = []

    for char in tr.reportchars:

        if char == "x":

            custom_show_xfailed(terminalreporter, lines)

        elif char == "X":

            _pytest.skipping.show_xpassed(terminalreporter, lines)

        elif char in "fF":

            _pytest.skipping.show_simple(terminalreporter, lines, 'failed', "FAIL %s")

        elif char in "sS":

            _pytest.skipping.show_skipped(terminalreporter, lines)

        elif char == "E":

            _pytest.skipping.show_simple(terminalreporter, lines, 'error', "ERROR %s")

        elif char == 'p':

            _pytest.skipping.show_simple(terminalreporter, lines, 'passed', "PASSED %s")


    if lines:

        tr._tw.sep("=", "short test summary info")

        for line in lines:

            tr._tw.line(line)


    tr.reportchars = [] # to avoid further output

第二種方法是大材小用,因為你必須重新定義整個pytest_terminal_summary.


查看完整回答
反對 回復 2022-01-18
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

感謝這個答案,我發現以下解決方案非常適合我。我已經在conftest.py我的測試套件的根目錄中創建了具有以下內容的文件:


import _pytest.skipping as s


def show_xfailed(tr, lines):

    for rep in tr.stats.get("xfailed", []):

        pos = tr.config.cwd_relative_nodeid(rep.nodeid)

        reason = rep.wasxfail

        s = "XFAIL\t%s" % pos

        if reason:

            s += ": " + str(reason)

        lines.append(s)


s.REPORTCHAR_ACTIONS["x"] = show_xfailed


def show_skipped(tr, lines):

    for rep in tr.stats.get("skipped", []):

        pos = tr.config.cwd_relative_nodeid(rep.nodeid)

        reason = rep.longrepr[-1]

        if reason.startswith("Skipped: "):

            reason = reason[9:]

        verbose_word = s._get_report_str(tr.config, report=rep)

        lines.append("%s\t%s: %s" % (verbose_word, pos, reason))


s.REPORTCHAR_ACTIONS["s"] = show_skipped

s.REPORTCHAR_ACTIONS["S"] = show_skipped

現在我得到以下輸出:


./test_file.py -rsx

============================= test session starts =============================

platform linux -- Python 3.5.2, pytest-4.4.1, py-1.7.0, pluggy-0.9.0

rootdir: /home/ashot/questions

collected 2 items                                                             


test_file.py xs                                                         [100%]

=========================== short test summary info ===========================

SKIPPED test_file.py::test_2: Reason of skipping

XFAIL   test_file.py::test_1: Reason of failure


==================== 1 skipped, 1 xfailed in 0.05 seconds =====================


查看完整回答
反對 回復 2022-01-18
  • 2 回答
  • 0 關注
  • 403 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號