将测试结果转换为XML格式

时间:2018-09-26 16:15:58

标签: regex xml bash

这是测试结果

TEST: Test description
PASS: test case description
PASS: test case description
PASS: test case description
FAIL: failure description

我正在尝试将此测试结果转换为XML格式

<testsuite tests="Test description">
    <testcase result="PASS" description="test case description"/>
    <testcase result="PASS" description="test case description"/>
    <testcase result="PASS" description="test case description"/>
    <testcase result="FAIL">
        <failure> details about failure </failure>
    </testcase>
</testsuite>

我将如何在Bash中这样做?

4 个答案:

答案 0 :(得分:2)

尽管您可以一次性直接使用进行所有操作,但是我建议使用某种形式的XML转换器,例如。原因是XML具有很多微妙之处,而不仅仅是具有有趣格式的ASCII文件。

我在这里采用的方法是从您的ASCII文件创建一个简单的Pyx format,然后使用来实现魔术:

创建Pyx文件:PYX格式非常易于描述和理解。每行的第一个字符 标识行的内容类型。内容虽然连续,但并不直接跨越线 行可能包含相同的内容类型。对于标签属性,属性名称和 值仅用空格分隔,不使用多余的引号。前缀字符为:

( start-tag
) end-tag
A attribute
- character data (content)
? processing instruction

因此,有了这些知识,我们可以编写以下简单的(存储在a2pyx.awk中)

BEGIN{FS=": *"}
($1=="TEST") && (NR>1) { print ")testsuite" } # close testsuite node
($1=="TEST") { print "(testsuite"             # open testsuite node
               print "Atests", $2  }          # attribute tests
($1=="PASS") { print "(testcase"              # open testcase-pass node
               print "Aresult PASS"           # attribute result
               print "Adescription",$2        # attribute description
               print ")testcase" }            # close testcase node
($1=="FAIL") { print "(testcase"              # open testcase-fail node
               print "Aresult FAIL"           # attribute result
               print "(failure"               # open failure node
               print "-"$2                    # add content
               print ")failure"               # close failure node
               print ")testcase" }            # close testcase node
END          { print ")testsuite" }           # close last testsuite node

哪个输出为awk -f a2pyx.awk /path/to/file

(testsuite
Atests Test description
(testcase
Aresult PASS
Adescription test case description
)testcase
(testcase
Aresult PASS
Adescription test case description
)testcase
(testcase
Aresult PASS
Adescription test case description
)testcase
(testcase
Aresult FAIL
(failure
-failure description
)failure
)testcase
)testsuite

将PYX转换为XML :现在,您可以创建PYX文件。因此,现在您可以使用通过以下操作将其转换为XML:

awk -f a2pyx.awk /path/to/file | xmlstarlet p2x | xmlstarlet fo -R -

第一个xmlstarlet将其转换为单行XML,第二个版本将其重新格式化为有效XML。输出为:

<?xml version="1.0"?>
<testsuite tests="Test description">
  <testcase result="PASS" description="test case description"/>
  <testcase result="PASS" description="test case description"/>
  <testcase result="PASS" description="test case description"/>
  <testcase result="FAIL">
    <failure>failure description</failure>
  </testcase>
</testsuite>

答案 1 :(得分:0)

awk中,这是一个粗略的刺探。

awk -F": " -v dq='"' '$1=="TEST"{printf "%s\n", "<testsuite tests="dq$2dq">"}$1=="PASS"{printf "\t%s\n", "<testcase result="dq$1dq" description="dq$2dq" />"}$1=="FAIL"{printf "\t%s\n\t\t%s\n\t%s\n","<testcase result="dq$1dq">", "<failure>"$2"<failure>", "</testcase>"}END{print "</testsuite>"}' inputfile

示例:

~$ cat input
TEST: Test description
PASS: test case description
PASS: test case description
PASS: test case description
FAIL: failure description
~$ awk -F": " -v dq='"' '$1=="TEST"{printf "%s\n", "<testsuite tests="dq$2dq">"}$1=="PASS"{printf "\t%s\n", "<testcase result="dq$1dq" description="dq$2dq" />"}$1=="FAIL"{printf "\t%s\n\t\t%s\n\t%s\n","<testcase result="dq$1dq">", "<failure>"$2"<failure>", "</testcase>"}END{print "</testsuite>"}' input
<testsuite tests="Test description">
        <testcase result="PASS" description="test case description" />
        <testcase result="PASS" description="test case description" />
        <testcase result="PASS" description="test case description" />
        <testcase result="FAIL">
                <failure>failure description<failure>
        </testcase>
</testsuite>

答案 2 :(得分:0)

需要担心的一个极端情况是说明中是否包含引号:

$ cat results
TEST: Test description
PASS: test case description
PASS: "test case" description
PASS: test case description
FAIL: failure description "with quotes"

一些打击

close_suite=false
while IFS=" :" read -r item value; do
    case $item in 
        TEST) printf '<testsuite tests="%s">\n' "${value//\"/&quot;}"; close_suite=true ;; 
        PASS) printf '<testcase result="PASS" description="%s"/>\n' "${value//\"/&quot;}" ;; 
        FAIL) printf '<testcase result="FAIL"><failure>%s</failure></testcase>\n' "$value" ;; 
    esac
done < results
$close_suite && echo '</testsuite>'
<testsuite tests="Test description">
<testcase result="PASS" description="test case description"/>
<testcase result="PASS" description="&quot;test case&quot; description"/>
<testcase result="PASS" description="test case description"/>
<testcase result="FAIL"><failure>failure description "with quotes"</failure></testcase>
</testsuite>

答案 3 :(得分:0)

SGML可以将您的测试结果日志直接解析为标记,然后将其格式化为规范的XML格式。为此,您要告诉SGML用字符<test-case><result>替换每行的开头(RS = SGML术语为“ record-start”),并将冒号:替换为{{ 1}}使用简短引用声明,并允许省略</result>的end-element标签:

test-case

然后,您可以从bash调用<!DOCTYPE test-suite [ <!ELEMENT test-suite - - (test-case+)> <!ELEMENT test-case - O (result,description)> <!ELEMENT (result|description) 0 0 (#PCDATA)> <!ENTITY start-result '<test-case><result>'> <!ENTITY end-result '</result>'> <!SHORTREF s '&RS' start-result> <!SHORTREF t ':' end-result> <!USEMAP s test-suite> <!USEMAP t result> <!ENTITY yourfile SYSTEM 'name-of-input-file> ]> <test-suite> &yourfile; </test-suite> 程序(OpenSP / OpenJade程序包的一部分)以从输入中生成XML。但是请注意,结果会将osxresult编码为元素,而不是属性。