mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-08 06:28:08 -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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -129,20 +132,31 @@ func TestParser(t *testing.T) {
|
|||||||
|
|
||||||
func TestJUnitFormatter(t *testing.T) {
|
func TestJUnitFormatter(t *testing.T) {
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
file, err := ioutil.ReadFile("tests/" + testCase.reportName)
|
report, err := loadTestReport(testCase.reportName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var junitReport bytes.Buffer
|
var junitReport bytes.Buffer
|
||||||
|
|
||||||
err = JUnitReportXML(testCase.report, &junitReport)
|
if err = JUnitReportXML(testCase.report, &junitReport); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(junitReport.Bytes()) != string(file) {
|
if string(junitReport.Bytes()) != report {
|
||||||
t.Fatalf("Report xml ==\n%s, want\n%s", string(junitReport.Bytes()), string(file))
|
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,6 +5,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ type JUnitTestSuite struct {
|
|||||||
Failures int `xml:"failures,attr"`
|
Failures int `xml:"failures,attr"`
|
||||||
Time string `xml:"time,attr"`
|
Time string `xml:"time,attr"`
|
||||||
Name string `xml:"name,attr"`
|
Name string `xml:"name,attr"`
|
||||||
|
Properties []JUnitProperty `xml:"properties>property,omitempty"`
|
||||||
TestCases []JUnitTestCase
|
TestCases []JUnitTestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +27,18 @@ type JUnitTestCase struct {
|
|||||||
Failure string `xml:"failure,omitempty"`
|
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 {
|
func JUnitReportXML(report *Report, w io.Writer) error {
|
||||||
suites := []JUnitTestSuite{}
|
suites := []JUnitTestSuite{}
|
||||||
|
|
||||||
@ -35,6 +49,7 @@ func JUnitReportXML(report *Report, w io.Writer) error {
|
|||||||
Failures: 0,
|
Failures: 0,
|
||||||
Time: formatTime(pkg.Time),
|
Time: formatTime(pkg.Time),
|
||||||
Name: pkg.Name,
|
Name: pkg.Name,
|
||||||
|
Properties: []JUnitProperty{},
|
||||||
TestCases: []JUnitTestCase{},
|
TestCases: []JUnitTestCase{},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +58,9 @@ func JUnitReportXML(report *Report, w io.Writer) error {
|
|||||||
classname = pkg.Name[idx+1:]
|
classname = pkg.Name[idx+1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// properties
|
||||||
|
ts.Properties = append(ts.Properties, NewJUnitProperty("go.version", runtime.Version()))
|
||||||
|
|
||||||
// individual test cases
|
// individual test cases
|
||||||
for _, test := range pkg.Tests {
|
for _, test := range pkg.Tests {
|
||||||
testCase := JUnitTestCase{
|
testCase := JUnitTestCase{
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<testsuite tests="2" failures="0" time="0.160" name="package/name">
|
<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="TestOne" time="0.060"></testcase>
|
||||||
<testcase classname="name" name="TestTwo" time="0.100"></testcase>
|
<testcase classname="name" name="TestTwo" time="0.100"></testcase>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<testsuite tests="2" failures="1" time="0.151" name="package/name">
|
<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="TestOne" time="0.020"></testcase>
|
||||||
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
|
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user