如何在pytest

时间:2019-07-03 07:29:26

标签: python pytest

pytest 中,当测试用例失败时,报告中将包含以下类别:

  • 故障详细信息
  • 捕获的stdout呼叫
  • 捕获的stderr呼叫
  • 捕获的日志呼叫

pytest report

我想添加一些其他自定义部分(我有一个并行运行的服务器,并希望在专用部分中显示此服务器记录的信息)。

我怎么做(如果可能的话)?

谢谢


注意

我目前在源代码中找到了以下内容,但不知道这是否正确

nodes.py

class Item(Node):
    ...
    def add_report_section(self, when, key, content):
        """
        Adds a new report section, similar to what's done internally
        to add stdout and stderr captured output:: 
        ...
        """

reports.py

class BaseReport:
    ...

    @property
    def caplog(self):
        """Return captured log lines, if log capturing is enabled

        .. versionadded:: 3.5
        """
        return "\n".join(
            content for (prefix, content) in self.get_sections("Captured log")
        )

2 个答案:

答案 0 :(得分:1)

要将自定义节添加到终端输出,您需要追加到report.sections列表。这可以直接在pytest_report_teststatus hookimpl中完成,也可以在其他挂钩中间接完成(通过hookwrapper);实际的实现很大程度上取决于您的特定用例。示例:

# conftest.py

import os
import random
import pytest

def pytest_report_teststatus(report, config):
    messages = (
        'Egg and bacon',
        'Egg, sausage and bacon',
        'Egg and Spam',
        'Egg, bacon and Spam'
    )

    if report.when == 'teardown':
        line = f'{report.nodeid} says:\t"{random.choice(messages)}"'
        report.sections.append(('My custom section', line))


def pytest_terminal_summary(terminalreporter, exitstatus, config):
    reports = terminalreporter.getreports('')
    content = os.linesep.join(text for report in reports for secname, text in report.sections)
    if content:
        terminalreporter.ensure_newline()
        terminalreporter.section('My custom section', sep='-', blue=True, bold=True)
        terminalreporter.line(content)

示例测试:

def test_spam():
     assert True

def test_eggs():
     assert True


def test_bacon():
     assert False

运行测试时,您应该在底部的蓝色处看到My custom section标头,其中包含每个测试的消息:

collected 3 items

test_spam.py::test_spam PASSED
test_spam.py::test_eggs PASSED
test_spam.py::test_bacon FAILED

============================================= FAILURES =============================================
____________________________________________ test_bacon ____________________________________________

    def test_bacon():
>        assert False
E        assert False

test_spam.py:9: AssertionError
---------------------------------------- My custom section -----------------------------------------
test_spam.py::test_spam says:   "Egg, bacon and Spam"
test_spam.py::test_eggs says:   "Egg and Spam"
test_spam.py::test_bacon says:  "Egg, sausage and bacon"
================================ 1 failed, 2 passed in 0.07 seconds ================================

答案 1 :(得分:1)

另一个答案显示了如何将自定义部分添加到终端报告摘要,但这并不是为每个测试添加自定义部分的最佳方式。

为此,您可以(并且应该)使用 add_report_section 节点 (docs) 的更高级别 API Item。下面显示了一个极简示例,请对其进行修改以满足您的需求。如有必要,您可以通过项目节点从测试实例传递状态。

test_something.py 中,这里有一个通过测试,两个失败:

def test_good():
    assert 2 + 2 == 4

def test_bad():
    assert 2 + 2 == 5

def test_ugly():
    errorerror

conftest.py 中,设置一个钩子包装器:

import pytest

content = iter(["first", "second", "third"])

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
    outcome = yield
    item.add_report_section("call", "custom", next(content))

报告现在将显示自定义部分每个测试

$ pytest
============================== test session starts ===============================
platform linux -- Python 3.9.0, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /tmp/example
collected 3 items                                                                

test_something.py .FF                                                      [100%]

==================================== FAILURES ====================================
____________________________________ test_bad ____________________________________

    def test_bad():
>       assert 2 + 2 == 5
E       assert (2 + 2) == 5

test_something.py:5: AssertionError
------------------------------ Captured custom call ------------------------------
second
___________________________________ test_ugly ____________________________________

    def test_ugly():
>       errorerror
E       NameError: name 'errorerror' is not defined

test_something.py:8: NameError
------------------------------ Captured custom call ------------------------------
third
============================ short test summary info =============================
FAILED test_something.py::test_bad - assert (2 + 2) == 5
FAILED test_something.py::test_ugly - NameError: name 'errorerror' is not defined
========================== 2 failed, 1 passed in 0.02s ===========================