mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-04 12:40:15 -05:00
junit: Add Testsuites.WriteXML method
This commit is contained in:
parent
b1271c39f9
commit
075629ad5f
@ -64,32 +64,21 @@ func (c Config) Run(input io.Reader, output io.Writer) (*gtr.Report, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if err = c.writeXML(output, report); err != nil {
|
||||
if err = c.writeJunitXML(output, report); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &report, nil
|
||||
}
|
||||
|
||||
func (c Config) writeXML(w io.Writer, report gtr.Report) error {
|
||||
func (c Config) writeJunitXML(w io.Writer, report gtr.Report) error {
|
||||
testsuites := junit.CreateFromReport(report, c.Hostname)
|
||||
|
||||
if !c.SkipXMLHeader {
|
||||
_, err := fmt.Fprintf(w, xml.Header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
enc := xml.NewEncoder(w)
|
||||
enc.Indent("", "\t")
|
||||
if err := enc.Encode(testsuites); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := enc.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := fmt.Fprintf(w, "\n")
|
||||
return err
|
||||
return testsuites.WriteXML(w)
|
||||
}
|
||||
|
||||
func (c Config) gotestOptions() []gotest.Option {
|
||||
|
@ -5,6 +5,7 @@ package junit
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -36,6 +37,20 @@ func (t *Testsuites) AddSuite(ts Testsuite) {
|
||||
t.Disabled += ts.Disabled
|
||||
}
|
||||
|
||||
// WriteXML writes the XML representation of Testsuites t to writer w.
|
||||
func (t *Testsuites) WriteXML(w io.Writer) error {
|
||||
enc := xml.NewEncoder(w)
|
||||
enc.Indent("", "\t")
|
||||
if err := enc.Encode(t); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := enc.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := fmt.Fprintf(w, "\n")
|
||||
return err
|
||||
}
|
||||
|
||||
// Testsuite is a single JUnit testsuite containing testcases.
|
||||
type Testsuite struct {
|
||||
// required attributes
|
||||
|
@ -1,6 +1,7 @@
|
||||
package junit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"testing"
|
||||
"time"
|
||||
@ -181,3 +182,28 @@ func TestMarshalUnmarshal(t *testing.T) {
|
||||
t.Errorf("Unmarshal result incorrect, diff (-want +got):\n%s\n", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteXML(t *testing.T) {
|
||||
want := `<testsuites tests="1">
|
||||
<testsuite name="Example" tests="1" failures="0" errors="0" id="0" time="">
|
||||
<testcase name="Test" classname=""></testcase>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
`
|
||||
|
||||
var suites Testsuites
|
||||
|
||||
ts := Testsuite{Name:"Example"}
|
||||
ts.AddTestcase(Testcase{Name: "Test", })
|
||||
suites.AddSuite(ts)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := suites.WriteXML(&buf); err != nil {
|
||||
t.Fatalf("WriteXML failed: %v\n", err)
|
||||
}
|
||||
|
||||
got := buf.String()
|
||||
if diff := cmp.Diff(want, got); diff != "" {
|
||||
t.Errorf("WriteXML mismatch, diff (-want +got):\n%s\n", diff)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user