Merge pull request #9 from nickpalmer/testsuites

Testsuites
This commit is contained in:
Joël Stemmer 2015-01-15 21:37:55 +01:00
commit 18e031d2c6
9 changed files with 146 additions and 43 deletions

View File

@ -156,10 +156,62 @@ var testCases []TestCase = []TestCase{
},
noXmlHeader: true,
},
{
name: "06-mixed.txt",
reportName: "06-report.xml",
report: &Report{
Packages: []Package{
{
Name: "package/name1",
Time: 160,
Tests: []Test{
{
Name: "TestOne",
Time: 60,
Result: PASS,
Output: []string{},
},
{
Name: "TestTwo",
Time: 100,
Result: PASS,
Output: []string{},
},
},
},
{
Name: "package/name2",
Time: 151,
Tests: []Test{
{
Name: "TestOne",
Time: 20,
Result: FAIL,
Output: []string{
"file_test.go:11: Error message",
"file_test.go:11: Longer",
"\terror",
"\tmessage.",
},
},
{
Name: "TestTwo",
Time: 130,
Result: PASS,
Output: []string{},
},
},
},
},
},
noXmlHeader: true,
},
}
func TestParser(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Running: %s", testCase.name)
file, err := os.Open("tests/" + testCase.name)
if err != nil {
t.Fatal(err)
@ -233,7 +285,7 @@ func TestJUnitFormatter(t *testing.T) {
}
if string(junitReport.Bytes()) != report {
t.Fatalf("Report xml ==\n%s, want\n%s", string(junitReport.Bytes()), report)
t.Fatalf("Fail: %s Report xml ==\n%s, want\n%s", testCase.name, string(junitReport.Bytes()), report)
}
}
}
@ -245,7 +297,7 @@ func loadTestReport(name string) (string, error) {
}
// replace value="1.0" With actual version
report := strings.Replace(string(contents), `value="1.0"`, fmt.Sprintf(`value="%s"`, runtime.Version()), 1)
report := strings.Replace(string(contents), `value="1.0"`, fmt.Sprintf(`value="%s"`, runtime.Version()), -1)
return report, nil
}

View File

@ -9,6 +9,11 @@ import (
"strings"
)
type JUnitTestSuites struct {
XMLName xml.Name `xml:"testsuites"`
Suites []JUnitTestSuite
}
type JUnitTestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Tests int `xml:"tests,attr"`
@ -53,7 +58,7 @@ func NewJUnitProperty(name, value string) JUnitProperty {
// JUnitReportXML writes a junit xml representation of the given report to w
// in the format described at http://windyroad.org/dl/Open%20Source/JUnit.xsd
func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error {
suites := []JUnitTestSuite{}
suites := JUnitTestSuites{}
// convert Report to JUnit test suites
for _, pkg := range report.Packages {
@ -100,7 +105,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error {
ts.TestCases = append(ts.TestCases, testCase)
}
suites = append(suites, ts)
suites.Suites = append(suites.Suites, ts)
}
// to xml
@ -114,6 +119,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error {
if !noXmlHeader {
writer.WriteString(xml.Header)
}
writer.Write(bytes)
writer.WriteByte('\n')
writer.Flush()

View File

@ -1,8 +1,10 @@
<?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>
<testsuites>
<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>
</testsuites>

View File

@ -1,10 +1,12 @@
<?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">
<failure message="Failed" type="">file_test.go:11: Error message&#xA;file_test.go:11: Longer&#xA;&#x9;error&#xA;&#x9;message.</failure>
</testcase>
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
</testsuite>
<testsuites>
<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">
<failure message="Failed" type="">file_test.go:11: Error message&#xA;file_test.go:11: Longer&#xA;&#x9;error&#xA;&#x9;message.</failure>
</testcase>
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
</testsuite>
</testsuites>

View File

@ -1,10 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="2" failures="0" time="0.150" name="package/name">
<properties>
<property name="go.version" value="1.0"></property>
</properties>
<testcase classname="name" name="TestOne" time="0.020">
<skipped message="file_test.go:11: Skip message"></skipped>
</testcase>
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
</testsuite>
<testsuites>
<testsuite tests="2" failures="0" time="0.150" name="package/name">
<properties>
<property name="go.version" value="1.0"></property>
</properties>
<testcase classname="name" name="TestOne" time="0.020">
<skipped message="file_test.go:11: Skip message"></skipped>
</testcase>
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
</testsuite>
</testsuites>

View File

@ -1,8 +1,10 @@
<?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>
<testsuites>
<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>
</testsuites>

View File

@ -1,7 +1,9 @@
<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>
<testsuites>
<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>
</testsuites>

17
tests/06-mixed.txt Normal file
View File

@ -0,0 +1,17 @@
=== RUN TestOne
--- PASS: TestOne (0.06 seconds)
=== RUN TestTwo
--- PASS: TestTwo (0.10 seconds)
PASS
ok package/name1 0.160s
=== RUN TestOne
--- FAIL: TestOne (0.02 seconds)
file_test.go:11: Error message
file_test.go:11: Longer
error
message.
=== RUN TestTwo
--- PASS: TestTwo (0.13 seconds)
FAIL
exit status 1
FAIL package/name2 0.151s

18
tests/06-report.xml Normal file
View File

@ -0,0 +1,18 @@
<testsuites>
<testsuite tests="2" failures="0" time="0.160" name="package/name1">
<properties>
<property name="go.version" value="1.0"></property>
</properties>
<testcase classname="name1" name="TestOne" time="0.060"></testcase>
<testcase classname="name1" name="TestTwo" time="0.100"></testcase>
</testsuite>
<testsuite tests="2" failures="1" time="0.151" name="package/name2">
<properties>
<property name="go.version" value="1.0"></property>
</properties>
<testcase classname="name2" name="TestOne" time="0.020">
<failure message="Failed" type="">file_test.go:11: Error message&#xA;file_test.go:11: Longer&#xA;&#x9;error&#xA;&#x9;message.</failure>
</testcase>
<testcase classname="name2" name="TestTwo" time="0.130"></testcase>
</testsuite>
</testsuites>