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, 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) { func TestParser(t *testing.T) {
for _, testCase := range testCases { for _, testCase := range testCases {
t.Logf("Running: %s", testCase.name)
file, err := os.Open("tests/" + testCase.name) file, err := os.Open("tests/" + testCase.name)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -233,7 +285,7 @@ func TestJUnitFormatter(t *testing.T) {
} }
if string(junitReport.Bytes()) != report { 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 // 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 return report, nil
} }

View File

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

View File

@ -1,8 +1,10 @@
<?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"> <testsuites>
<properties> <testsuite tests="2" failures="0" time="0.160" name="package/name">
<property name="go.version" value="1.0"></property> <properties>
</properties> <property name="go.version" value="1.0"></property>
<testcase classname="name" name="TestOne" time="0.060"></testcase> </properties>
<testcase classname="name" name="TestTwo" time="0.100"></testcase> <testcase classname="name" name="TestOne" time="0.060"></testcase>
</testsuite> <testcase classname="name" name="TestTwo" time="0.100"></testcase>
</testsuite>
</testsuites>

View File

@ -1,10 +1,12 @@
<?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"> <testsuites>
<properties> <testsuite tests="2" failures="1" time="0.151" name="package/name">
<property name="go.version" value="1.0"></property> <properties>
</properties> <property name="go.version" value="1.0"></property>
<testcase classname="name" name="TestOne" time="0.020"> </properties>
<failure message="Failed" type="">file_test.go:11: Error message&#xA;file_test.go:11: Longer&#xA;&#x9;error&#xA;&#x9;message.</failure> <testcase classname="name" name="TestOne" time="0.020">
</testcase> <failure message="Failed" type="">file_test.go:11: Error message&#xA;file_test.go:11: Longer&#xA;&#x9;error&#xA;&#x9;message.</failure>
<testcase classname="name" name="TestTwo" time="0.130"></testcase> </testcase>
</testsuite> <testcase classname="name" name="TestTwo" time="0.130"></testcase>
</testsuite>
</testsuites>

View File

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

View File

@ -1,8 +1,10 @@
<?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"> <testsuites>
<properties> <testsuite tests="2" failures="0" time="0.160" name="package/name">
<property name="go.version" value="1.0"></property> <properties>
</properties> <property name="go.version" value="1.0"></property>
<testcase classname="name" name="TestOne" time="0.060"></testcase> </properties>
<testcase classname="name" name="TestTwo" time="0.100"></testcase> <testcase classname="name" name="TestOne" time="0.060"></testcase>
</testsuite> <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"> <testsuites>
<properties> <testsuite tests="2" failures="0" time="0.160" name="package/name">
<property name="go.version" value="1.0"></property> <properties>
</properties> <property name="go.version" value="1.0"></property>
<testcase classname="name" name="TestOne" time="0.060"></testcase> </properties>
<testcase classname="name" name="TestTwo" time="0.100"></testcase> <testcase classname="name" name="TestOne" time="0.060"></testcase>
</testsuite> <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>