使用junitxml python模块向junitxml添加失败的测试

2024-06-26 14:34:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些旧的C/Fortran代码,我正在将其集成到Bambol中。该代码附带了一系列测试,但最终结果是,如果测试“失败”,则会打印一些细节,否则将保持沉默。结果将编译成如下所示的文件:

TEST_NAME,OUT,ERR
tname0,,
tname1,,
tname2,,2c3ef0 

因此,在上面的示例中,tname0和tname1通过,但tname2失败。这对文件意味着第一列始终有数据,第二列从不有数据,第三列只有在测试失败时才有数据

我正在使用JUnitXMLPython模块将测试结果写入xml,以便竹的junit解析器能够读取它。然而,我不知道如何添加失败的测试用例,JUnitXML文档似乎没有任何示例:https://github.com/kyrus/python-junit-xml

这是我的解析器的要点:

from junit_xml import TestSuite, TestCase
import os

def main(fname, testname="DEFAULT"):

    test_cases = []
    testIdx = 0

    with open(fname, 'r') as fid:
        for line in fid.readlines():
            testIdx += 1
            line = line.split(",")

            test_cases.append((TestCase("Test" + str(testIdx), line[0],0,line[1], line[2]))

    ts = TestSuite(testname, test_cases)
    xmlstr = TestSuite.to_xml_string([ts])

    # Bamboo's default is to look for JUnit XML in directories named "**/test-reports/"
    ofile = os.path.join("test-reports", "test_" + testname + ".xml")

    with open(ofile, "w") as ofid:
        ofid.write(xmlstr)

不幸的是,它似乎从未检测到失败的测试。它肯定会将错误消息放入<system-err>标记中,但它似乎从未实际检测到任何错误,并且xml文件的顶部总是这样:

<?xml version="1.0" ?>
<testsuites disabled="0" errors="0" failures="0" tests="42" time="0.0">
    <testsuite disabled="0" errors="0" failures="0" name="DEFAULT" skipped="0" tests="42" time="0">

是否有我遗漏的内容和/或更全面的文档?即使我强制它,只计算错误数,并在xml字符串中执行find replace来写入errors="1"或其他内容,Bambor也不会将它们视为失败的测试,所以我觉得一定有更微妙的事情发生


Tags: 文件数据代码testlinexmljuniterrors
1条回答
网友
1楼 · 发布于 2024-06-26 14:34:19

我处理了同样的问题,发现TestCase拥有TestCase方法。add_error_info('your error info')) 如果您创建一个TestCase并执行这个方法,那么在TestCase和TestSuite中都会考虑这个错误

例如:

ts = TestCase ("name", "classname", 1.2345, "", "")
ts.add_error_info("did not succeed")

相关问题 更多 >