mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-05 13:08:07 -05:00
Add properties to xml report
Include the go runtime version as a property.
This commit is contained in:
parent
8cf088e24f
commit
d72fb56d06
@ -2,8 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -129,20 +132,31 @@ func TestParser(t *testing.T) {
|
||||
|
||||
func TestJUnitFormatter(t *testing.T) {
|
||||
for _, testCase := range testCases {
|
||||
file, err := ioutil.ReadFile("tests/" + testCase.reportName)
|
||||
report, err := loadTestReport(testCase.reportName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var junitReport bytes.Buffer
|
||||
|
||||
err = JUnitReportXML(testCase.report, &junitReport)
|
||||
if err != nil {
|
||||
if err = JUnitReportXML(testCase.report, &junitReport); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if string(junitReport.Bytes()) != string(file) {
|
||||
t.Fatalf("Report xml ==\n%s, want\n%s", string(junitReport.Bytes()), string(file))
|
||||
if string(junitReport.Bytes()) != report {
|
||||
t.Fatalf("Report xml ==\n%s, want\n%s", string(junitReport.Bytes()), report)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadTestReport(name string) (string, error) {
|
||||
contents, err := ioutil.ReadFile("tests/" + name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// replace value="1.0" With actual version
|
||||
report := strings.Replace(string(contents), `value="1.0"`, fmt.Sprintf(`value="%s"`, runtime.Version()), 1)
|
||||
|
||||
return report, nil
|
||||
}
|
||||
|
@ -5,16 +5,18 @@ import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type JUnitTestSuite struct {
|
||||
XMLName xml.Name `xml:"testsuite"`
|
||||
Tests int `xml:"tests,attr"`
|
||||
Failures int `xml:"failures,attr"`
|
||||
Time string `xml:"time,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
TestCases []JUnitTestCase
|
||||
XMLName xml.Name `xml:"testsuite"`
|
||||
Tests int `xml:"tests,attr"`
|
||||
Failures int `xml:"failures,attr"`
|
||||
Time string `xml:"time,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
Properties []JUnitProperty `xml:"properties>property,omitempty"`
|
||||
TestCases []JUnitTestCase
|
||||
}
|
||||
|
||||
type JUnitTestCase struct {
|
||||
@ -25,17 +27,30 @@ type JUnitTestCase struct {
|
||||
Failure string `xml:"failure,omitempty"`
|
||||
}
|
||||
|
||||
type JUnitProperty struct {
|
||||
Name string `xml:"name,attr"`
|
||||
Value string `xml:"value,attr"`
|
||||
}
|
||||
|
||||
func NewJUnitProperty(name, value string) JUnitProperty {
|
||||
return JUnitProperty{
|
||||
Name: name,
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func JUnitReportXML(report *Report, w io.Writer) error {
|
||||
suites := []JUnitTestSuite{}
|
||||
|
||||
// convert Report to JUnit test suites
|
||||
for _, pkg := range report.Packages {
|
||||
ts := JUnitTestSuite{
|
||||
Tests: len(pkg.Tests),
|
||||
Failures: 0,
|
||||
Time: formatTime(pkg.Time),
|
||||
Name: pkg.Name,
|
||||
TestCases: []JUnitTestCase{},
|
||||
Tests: len(pkg.Tests),
|
||||
Failures: 0,
|
||||
Time: formatTime(pkg.Time),
|
||||
Name: pkg.Name,
|
||||
Properties: []JUnitProperty{},
|
||||
TestCases: []JUnitTestCase{},
|
||||
}
|
||||
|
||||
classname := pkg.Name
|
||||
@ -43,6 +58,9 @@ func JUnitReportXML(report *Report, w io.Writer) error {
|
||||
classname = pkg.Name[idx+1:]
|
||||
}
|
||||
|
||||
// properties
|
||||
ts.Properties = append(ts.Properties, NewJUnitProperty("go.version", runtime.Version()))
|
||||
|
||||
// individual test cases
|
||||
for _, test := range pkg.Tests {
|
||||
testCase := JUnitTestCase{
|
||||
|
@ -1,5 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<testsuite tests="2" failures="0" time="0.160" name="package/name">
|
||||
<properties>
|
||||
<property name="go.version" value="1.0"></property>
|
||||
</properties>
|
||||
<testcase classname="name" name="TestOne" time="0.060"></testcase>
|
||||
<testcase classname="name" name="TestTwo" time="0.100"></testcase>
|
||||
</testsuite>
|
||||
|
@ -1,5 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<testsuite tests="2" failures="1" time="0.151" name="package/name">
|
||||
<properties>
|
||||
<property name="go.version" value="1.0"></property>
|
||||
</properties>
|
||||
<testcase classname="name" name="TestOne" time="0.020"></testcase>
|
||||
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
|
||||
</testsuite>
|
||||
|
Loading…
x
Reference in New Issue
Block a user