mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-08 06:28:08 -05:00
Write JUnit report xml
Add tests
This commit is contained in:
parent
64b850a234
commit
c9c0e1877c
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,13 +14,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write xml
|
// Write xml
|
||||||
err = report.XML(os.Stdout)
|
err = JUnitReportXML(report, os.Stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error writing XML: %s\n", err)
|
fmt.Printf("Error writing XML: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Report) XML(io.Writer) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
name string
|
name string
|
||||||
expectedReport Report
|
reportName string
|
||||||
|
report *Report
|
||||||
}
|
}
|
||||||
|
|
||||||
var testCases []TestCase = []TestCase{
|
var testCases []TestCase = []TestCase{
|
||||||
{
|
{
|
||||||
name: "01-pass.txt",
|
name: "01-pass.txt",
|
||||||
expectedReport: Report{
|
reportName: "01-report.xml",
|
||||||
|
report: &Report{
|
||||||
Packages: []Package{
|
Packages: []Package{
|
||||||
{
|
{
|
||||||
Name: "package/name",
|
Name: "package/name",
|
||||||
@ -38,7 +42,8 @@ var testCases []TestCase = []TestCase{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "02-fail.txt",
|
name: "02-fail.txt",
|
||||||
expectedReport: Report{
|
reportName: "02-report.xml",
|
||||||
|
report: &Report{
|
||||||
Packages: []Package{
|
Packages: []Package{
|
||||||
{
|
{
|
||||||
Name: "package/name",
|
Name: "package/name",
|
||||||
@ -79,7 +84,7 @@ func TestParser(t *testing.T) {
|
|||||||
t.Fatalf("Report == nil")
|
t.Fatalf("Report == nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := testCase.expectedReport
|
expected := testCase.report
|
||||||
if len(report.Packages) != len(expected.Packages) {
|
if len(report.Packages) != len(expected.Packages) {
|
||||||
t.Fatalf("Report packages == %d, want %d", len(report.Packages), len(expected.Packages))
|
t.Fatalf("Report packages == %d, want %d", len(report.Packages), len(expected.Packages))
|
||||||
}
|
}
|
||||||
@ -121,3 +126,23 @@ func TestParser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJUnitFormatter(t *testing.T) {
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
file, err := ioutil.ReadFile("tests/" + testCase.reportName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var junitReport bytes.Buffer
|
||||||
|
|
||||||
|
err = JUnitReportXML(testCase.report, &junitReport)
|
||||||
|
if 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
97
junit-formatter.go
Normal file
97
junit-formatter.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
|
||||||
|
type JUnitTestCase struct {
|
||||||
|
XMLName xml.Name `xml:"testcase"`
|
||||||
|
Classname string `xml:"classname,attr"`
|
||||||
|
Name string `xml:"name,attr"`
|
||||||
|
Time string `xml:"time,attr"`
|
||||||
|
Failure *string `xml:"failure"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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{},
|
||||||
|
}
|
||||||
|
|
||||||
|
classname := pkg.Name
|
||||||
|
if idx := strings.LastIndex(classname, "/"); idx > -1 && idx < len(pkg.Name) {
|
||||||
|
classname = pkg.Name[idx+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// individual test cases
|
||||||
|
for _, test := range pkg.Tests {
|
||||||
|
testCase := JUnitTestCase{
|
||||||
|
Classname: classname,
|
||||||
|
Name: test.Name,
|
||||||
|
Time: formatTime(test.Time),
|
||||||
|
Failure: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
if test.Result == FAIL {
|
||||||
|
ts.Failures += 1
|
||||||
|
|
||||||
|
// TODO: set error message
|
||||||
|
msg := ""
|
||||||
|
testCase.Failure = &msg
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.TestCases = append(ts.TestCases, testCase)
|
||||||
|
}
|
||||||
|
|
||||||
|
suites = append(suites, ts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// to xml
|
||||||
|
bytes, err := xml.MarshalIndent(suites, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writer := bufio.NewWriter(w)
|
||||||
|
|
||||||
|
// remove newline from xml.Header, because xml.MarshalIndent starts with a newline
|
||||||
|
writer.WriteString(xml.Header[:len(xml.Header)-1])
|
||||||
|
writer.Write(bytes)
|
||||||
|
writer.WriteByte('\n')
|
||||||
|
writer.Flush()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func countFailures(tests []Test) (result int) {
|
||||||
|
for _, test := range tests {
|
||||||
|
if test.Result == FAIL {
|
||||||
|
result += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatTime(time int) string {
|
||||||
|
return fmt.Sprintf("%.3f", float64(time)/1000.0)
|
||||||
|
}
|
5
tests/01-report.xml
Normal file
5
tests/01-report.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<testsuite tests="2" failures="0" time="0.160" name="package/name">
|
||||||
|
<testcase classname="name" name="TestOne" time="0.060"></testcase>
|
||||||
|
<testcase classname="name" name="TestTwo" time="0.100"></testcase>
|
||||||
|
</testsuite>
|
7
tests/02-report.xml
Normal file
7
tests/02-report.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<testsuite tests="2" failures="1" time="0.151" name="package/name">
|
||||||
|
<testcase classname="name" name="TestOne" time="0.020">
|
||||||
|
<failure></failure>
|
||||||
|
</testcase>
|
||||||
|
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
|
||||||
|
</testsuite>
|
Loading…
x
Reference in New Issue
Block a user