输出XML测试报告

注解

0.2版新增功能

以JUnit XML格式输出测试报告。

此插件实现 startTest()testOutcome()stopTestRun() 以JUnit XML格式编译并输出测试报告。默认情况下,报告将写入名为 nose2-junit.xml 在当前工作目录中。

您可以通过设置来配置输出文件名 path 在一个 [junit-xml] 配置文件中的节。XML 1.0中无效的Unicode字符将替换为 U+FFFD 替换字符。如果您的软件抛出一个带有无效字节字符串的错误。

默认情况下,不鼓励使用的字符的范围也将被替换。这可以通过设置 keep_restricted 配置变量到 True .

默认情况下,不会打印参数化测试和生成测试的参数。例如,以下代码:

# a.py

from nose2 import tools

def test_gen():
    def check(a, b):
        assert a == b, '{}!={}'.format(a,b)

    yield check, 99, 99
    yield check, -1, -1

@tools.params('foo', 'bar')
def test_params(arg):
    assert arg in ['foo', 'bar', 'baz']

默认情况下生成此XML:

<testcase classname="a" name="test_gen:1" time="0.000171">
    <system-out />
</testcase>
<testcase classname="a" name="test_gen:2" time="0.000202">
    <system-out />
</testcase>
<testcase classname="a" name="test_params:1" time="0.000159">
    <system-out />
</testcase>
<testcase classname="a" name="test_params:2" time="0.000163">
    <system-out />
</testcase>

但是如果 test_fullnameTrue ,然后生成以下XML:

<testcase classname="a" name="test_gen:1 (99, 99)" time="0.000213">
    <system-out />
</testcase>
<testcase classname="a" name="test_gen:2 (-1, -1)" time="0.000194">
    <system-out />
</testcase>
<testcase classname="a" name="test_params:1 ('foo')" time="0.000178">
    <system-out />
</testcase>
<testcase classname="a" name="test_params:2 ('bar')" time="0.000187">
    <system-out />
</testcase>

启用此插件

此插件是内置的,但默认情况下不加载。

即使您指定 always-on = True 在配置中,除非您也启用它,否则它不会运行。您可以通过将以下内容放入 unittest.cfgnose2.cfg 文件

[unittest]
plugins = nose2.plugins.junitxml

这个 plugins 参数可以包含插件名称列表,包括 nose2.plugins.junitxml

配置 [junit-xml]

always-on
违约

类型

布尔

keep_restricted
违约

类型

布尔

path
违约

nose2-junit.xml文件

类型

STR

test_fullname
违约

类型

布尔

test_properties
违约

没有

类型

STR

示例配置

默认配置相当于在 unittest.cfg 文件。

[junit-xml]
always-on = False
keep_restricted = False
path = nose2-junit.xml
test_fullname = False

命令行选项

--junit-xml-path FILE

输出XML文件名

-X DEFAULT, --junit-xml DEFAULT

生成JUnit XML输出报告

插件类引用:JUnitXmlReporter

class nose2.plugins.junitxml.JUnitXmlReporter(*args, **kwargs)[源代码]

将JUnit XML测试报告输出到文件

handleArgs(event)[源代码]

从命令行读取选项,必要时重写配置文件中的值

startTest(event)[源代码]

计数测试,记录开始时间

stopTestRun(event)[源代码]

将XML树输出到文件

testOutcome(event)[源代码]

将测试结果添加到XML树

样品输出

在包中包含测试的nose2示例方案的XML测试报告如下所示:

<testsuite errors="1" failures="5" name="nose2-junit" skips="1" tests="25" time="0.004">
  <testcase classname="pkg1.test.test_things" name="test_gen:1" time="0.000141" />
  <testcase classname="pkg1.test.test_things" name="test_gen:2" time="0.000093" />
  <testcase classname="pkg1.test.test_things" name="test_gen:3" time="0.000086" />
  <testcase classname="pkg1.test.test_things" name="test_gen:4" time="0.000086" />
  <testcase classname="pkg1.test.test_things" name="test_gen:5" time="0.000087" />
  <testcase classname="pkg1.test.test_things" name="test_gen_nose_style:1" time="0.000085" />
  <testcase classname="pkg1.test.test_things" name="test_gen_nose_style:2" time="0.000090" />
  <testcase classname="pkg1.test.test_things" name="test_gen_nose_style:3" time="0.000085" />
  <testcase classname="pkg1.test.test_things" name="test_gen_nose_style:4" time="0.000087" />
  <testcase classname="pkg1.test.test_things" name="test_gen_nose_style:5" time="0.000086" />
  <testcase classname="pkg1.test.test_things" name="test_params_func:1" time="0.000093" />
  <testcase classname="pkg1.test.test_things" name="test_params_func:2" time="0.000098">
    <failure message="test failure">Traceback (most recent call last):
  File "nose2/plugins/loader/parameters.py", line 162, in func
    return obj(*argSet)
  File "nose2/tests/functional/support/scenario/tests_in_package/pkg1/test/test_things.py", line 64, in test_params_func
    assert a == 1
AssertionError
</failure>
  </testcase>
  <testcase classname="pkg1.test.test_things" name="test_params_func_multi_arg:1" time="0.000094" />
  <testcase classname="pkg1.test.test_things" name="test_params_func_multi_arg:2" time="0.000089">
    <failure message="test failure">Traceback (most recent call last):
  File "nose2/plugins/loader/parameters.py", line 162, in func
    return obj(*argSet)
  File "nose2/tests/functional/support/scenario/tests_in_package/pkg1/test/test_things.py", line 69, in test_params_func_multi_arg
    assert a == b
AssertionError
</failure>
  </testcase>
  <testcase classname="pkg1.test.test_things" name="test_params_func_multi_arg:3" time="0.000096" />
  <testcase classname="" name="test_fixt" time="0.000091" />
  <testcase classname="" name="test_func" time="0.000084" />
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_failed" time="0.000113">
    <failure message="test failure">Traceback (most recent call last):
  File "nose2/tests/functional/support/scenario/tests_in_package/pkg1/test/test_things.py", line 17, in test_failed
    assert False, "I failed"
AssertionError: I failed
</failure>
  </testcase>
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_ok" time="0.000093" />
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_params_method:1" time="0.000099" />
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_params_method:2" time="0.000101">
    <failure message="test failure">Traceback (most recent call last):
  File "nose2/plugins/loader/parameters.py", line 144, in _method
    return method(self, *argSet)
  File "nose2/tests/functional/support/scenario/tests_in_package/pkg1/test/test_things.py", line 29, in test_params_method
    self.assertEqual(a, 1)
AssertionError: 2 != 1
</failure>
  </testcase>
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_skippy" time="0.000104">
    <skipped />
  </testcase>
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_typeerr" time="0.000096">
    <error message="test failure">Traceback (most recent call last):
  File "nose2/tests/functional/support/scenario/tests_in_package/pkg1/test/test_things.py", line 13, in test_typeerr
    raise TypeError("oops")
TypeError: oops
</error>
  </testcase>
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_gen_method:1" time="0.000094" />
  <testcase classname="pkg1.test.test_things.SomeTests" name="test_gen_method:2" time="0.000090">
    <failure message="test failure">Traceback (most recent call last):
  File "nose2/plugins/loader/generators.py", line 145, in method
    return func(*args)
  File "nose2/tests/functional/support/scenario/tests_in_package/pkg1/test/test_things.py", line 24, in check
    assert x == 1
AssertionError
</failure>
  </testcase>
</testsuite>