美文网首页
Pytest custom report

Pytest custom report

作者: 勇者与王者 | 来源:发表于2020-03-06 11:48 被阅读0次

    custom命令行结果:
    1.case的test_方法参数 加 record_property
    def test_failed3(record_property):
    record_property("50","200")
    assert False

    2.conftest.py里加hook函数:
    import pytest
    from _pytest.nodes import Item
    from _pytest.runner import CallInfo

    @pytest.hookimpl(hookwrapper=True)
    def pytest_runtest_makereport(item:Item, call:CallInfo):

    outcome = yield
    result = outcome.get_result()
    
    # if result.when == "call" and result.failed:
        # result.longrepr.addsection("CPU %", item.user_properties[0][0])
        # result.longrepr.addsection("MEM %", item.user_properties[0][1])
    

    3.命令行运行 pytest 该case 即可得到输出:
    ============================= test session starts ==============================
    plugins: html-2.0.1, allure-pytest-2.8.11, metadata-1.8.0
    collected 2 items

    test_s.py FF [100%]

    =================================== FAILURES ===================================
    _________________________________ test_failed __________________________________

    record_property = <function record_property.<locals>.append_property at 0x102474040>

    def test_failed(record_property):
        record_property("100","60")
    
      assert False
    

    E assert False

    test_s.py:6: AssertionError
    ------------------------------------ CPU % -------------------------------------
    100
    ------------------------------------ MEM % -------------------------------------
    60
    _________________________________ test_failed2 _________________________________

    record_property = <function record_property.<locals>.append_property at 0x10243bf70>

    def test_failed2(record_property):
        record_property("50", "200")
    
      assert False
    

    E assert False

    test_s.py:10: AssertionError
    ------------------------------------ CPU % -------------------------------------
    50
    ------------------------------------ MEM % -------------------------------------
    200
    ============================== 2 failed in 0.06s ===============================

    4.自定义xml格式报告:
    更简单,只用case的test_方法参数添加record_xml_attribute即可:
    def test_failed(record_xml_attribute):
    record_xml_attribute("auto_case", "False")
    record_xml_attribute("CPU","50%")
    record_xml_attribute("MEM", "450MB")
    assert True

    def test_failed2(record_xml_attribute):
    record_xml_attribute("auto_case","True")
    record_xml_attribute("CPU", "60%")
    record_xml_attribute("MEM", "200MB")
    assert False

    5.运行命令如下:pytest test_s.py -v --junitxml="resul.xml"
    输出xml:
    <?xml version="1.0" encoding="utf-8"?><testsuites><testsuite errors="0" failures="1" hostname="" name="pytest" skipped="0" tests="2" time="0.060" timestamp="2020-03-05T20:20:04.317114">
    <testcase
    CPU="50%"
    MEM="450MB"
    auto_case="False"
    classname="test_s"
    file="test_s.py"
    line="3"
    name="test_failed"
    time="0.001">
    </testcase>

    <testcase
            CPU="60%"
            MEM="200MB"
            auto_case="True"
            classname="test_s"
            file="test_s.py"
            line="9"
            name="test_failed2"
            time="0.001">
        <failure message="assert False">record_xml_attribute = &lt;bound method _NodeReporter.add_attribute of &lt;_pytest.junitxml._NodeReporter object at 0x10b36e640&gt;&gt;
    
    def test_failed2(record_xml_attribute):
        record_xml_attribute(&quot;auto_case&quot;,&quot;True&quot;)
        record_xml_attribute(&quot;CPU&quot;, &quot;60%&quot;)
        record_xml_attribute(&quot;MEM&quot;, &quot;200MB&quot;)
    

    > assert False
    E assert False

    test_s.py:14: AssertionError
    </failure>
    </testcase>
    /testsuite>
    </testsuites>

    相关文章

      网友评论

          本文标题:Pytest custom report

          本文链接:https://www.haomeiwen.com/subject/rlxbrhtx.html