将对象追加到列表末尾

Append object to end of list

我正在使用 python 和 junit_xml 来解析日志文件以生成 xml 输出。 我的日志文件如下所示:

/path/to/app1,app1,success,my@email.com,app1_log.log
/path/to/app2,app2,fail,my@email.com,app1_log.log

我可以使用以下代码将多个 TestCase 对象附加到 test_cases:

test_cases = [TestCase('app1), TestCase('app2')]

我需要的是逐行查看日志文件并将 testresult[0] 添加到测试用例对象。

from junit_xml import TestSuite, TestCase

test_cases=[]

lines = open('testresults.log').readlines()

for line in lines:
    testresult = string.split(string.strip(line), ',')
    test_cases.append(TestCase(testresult[0])
ts = TestSuite("my test suite", test_cases)

线性解析部分工作正常,但我似乎无法将多个 TestCase 对象添加到 test_case 列表中。

将您的代码更改为以下内容似乎有效:

from junit_xml import TestSuite, TestCase

test_cases=[]

lines = open('testresults.log').readlines()

for line in lines:
    testresult = line.split(",")
    test_cases.append(testresult[0])

print test_cases

$ python script.py ['/path/to/app1', '/path/to/app2']