Merge pull request #93 from johnSchnake/unmarshalSuites

Adjust xml annotations to allow unmarshal of test suites
This commit is contained in:
Joël Stemmer 2019-10-03 22:11:10 +01:00 committed by GitHub
commit 080dff894f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 3 deletions

View File

@ -14,8 +14,8 @@ import (
// JUnitTestSuites is a collection of JUnit test suites.
type JUnitTestSuites struct {
XMLName xml.Name `xml:"testsuites"`
Suites []JUnitTestSuite
XMLName xml.Name `xml:"testsuites"`
Suites []JUnitTestSuite `xml:"testsuite"`
}
// JUnitTestSuite is a single JUnit test suite which may contain many
@ -27,7 +27,7 @@ type JUnitTestSuite struct {
Time string `xml:"time,attr"`
Name string `xml:"name,attr"`
Properties []JUnitProperty `xml:"properties>property,omitempty"`
TestCases []JUnitTestCase
TestCases []JUnitTestCase `xml:"testcase"`
}
// JUnitTestCase is a single test case with its result.

View File

@ -0,0 +1,64 @@
package formatter
import (
"bytes"
"encoding/xml"
"testing"
)
func TestSuites_Unmarshal(t *testing.T) {
tests := []struct {
desc string
suites JUnitTestSuites
noXMLHeader bool
goVersion string
}{
{
desc: "Suites should marshal back and forth",
suites: JUnitTestSuites{
Suites: []JUnitTestSuite{
{
Name: "suite1",
TestCases: []JUnitTestCase{
{Name: "test1-1"},
{Name: "test1-2"},
},
},
{
Name: "suite2",
TestCases: []JUnitTestCase{
{Name: "test2-1"},
{Name: "test2-2"},
},
},
},
},
},
}
for _, test := range tests {
t.Logf("Test case: %v", test.desc)
initialBytes, err := xml.Marshal(test.suites)
if err != nil {
t.Fatalf("Expected no failure when generating xml; got %v", err)
}
var suites JUnitTestSuites
err = xml.Unmarshal(initialBytes, &suites)
if err != nil {
t.Fatalf("Expected no failure when unmarshaling; got %v", err)
}
newBytes, err := xml.Marshal(suites)
if err != nil {
t.Fatalf("Expected no failure when generating xml again; got %v", err)
}
if !bytes.Equal(newBytes, initialBytes) {
t.Errorf("Expected the same result when marshal/unmarshal/marshal. Expected\n%v\n\t but got\n%v",
string(initialBytes),
string(newBytes),
)
}
}
}