@@ -7,43 +7,128 @@ package junit
77import (
88 "fmt"
99 "testing"
10+ "time"
1011
12+ "github.com/stretchr/testify/assert"
1113 "github.com/stretchr/testify/require"
1214)
1315
14- func TestIngest (t * testing.T ) {
16+ func TestExamplesInTheWild (t * testing.T ) {
1517 tests := []struct {
1618 title string
17- input []byte
18- expected []Suite
19+ filename string
20+ origin string
21+ check func (* testing.T , []Suite )
1922 }{
2023 {
21- title : "xml input" ,
22- input : []byte (`<testsuite errors="0" failures="1" file="Foo.java">
23- <testcase name="unit tests" file="Foo.java">
24- <system-out><![CDATA[Hello, World]]></system-out>
25- <system-err><![CDATA[I'm an error!]]></system-err>
26- </testcase>
27- </testsuite>` ),
28- expected : []Suite {
29- {
30- Tests : []Test {
31- {
32- Name : "unit tests" ,
33- Status : "passed" ,
34- Properties : map [string ]string {
35- "file" : "Foo.java" ,
36- "name" : "unit tests" ,
37- },
38- SystemOut : "Hello, World" ,
39- SystemErr : "I'm an error!" ,
40- },
24+ title : "catchsoftware example" ,
25+ filename : "testdata/catchsoftware.xml" ,
26+ origin : "https://help.catchsoftware.com/display/ET/JUnit+Format" ,
27+ check : func (t * testing.T , suites []Suite ) {
28+ assert .Len (t , suites , 2 )
29+ assert .Len (t , suites [0 ].Tests , 0 )
30+ assert .Len (t , suites [1 ].Tests , 3 )
31+ assert .EqualError (t , suites [1 ].Tests [0 ].Error , "Assertion failed" )
32+ },
33+ },
34+ {
35+ title : "cubic example" ,
36+ filename : "testdata/cubic.xml" ,
37+ origin : "https://llg.cubic.org/docs/junit/" ,
38+ check : func (t * testing.T , suites []Suite ) {
39+ assert .Len (t , suites , 1 )
40+ assert .Len (t , suites [0 ].Tests , 1 )
41+ assert .Equal (t , "STDOUT text" , suites [0 ].SystemOut )
42+ assert .Equal (t , "STDERR text" , suites [0 ].SystemErr )
43+ assert .Equal (t , "STDOUT text" , suites [0 ].Tests [0 ].SystemOut )
44+ assert .Equal (t , "STDERR text" , suites [0 ].Tests [0 ].SystemErr )
45+ },
46+ },
47+ {
48+ title : "go-junit-report example" ,
49+ filename : "testdata/go-junit-report.xml" ,
50+ origin : "https://github.com/jstemmer/go-junit-report/blob/master/testdata/06-report.xml" ,
51+ check : func (t * testing.T , suites []Suite ) {
52+ assert .Len (t , suites , 2 )
53+ assert .Len (t , suites [0 ].Tests , 2 )
54+ assert .Len (t , suites [1 ].Tests , 2 )
55+ assert .Equal (t , "1.0" , suites [0 ].Properties ["go.version" ])
56+ assert .Equal (t , "1.0" , suites [1 ].Properties ["go.version" ])
57+ assert .EqualError (t , suites [1 ].Tests [0 ].Error , "file_test.go:11: Error message\n file_test.go:11: Longer\n \t error\n \t message." )
58+ },
59+ },
60+ {
61+ title : "ibm example" ,
62+ filename : "testdata/ibm.xml" ,
63+ origin : "https://www.ibm.com/support/knowledgecenter/en/SSQ2R2_14.2.0/com.ibm.rsar.analysis.codereview.cobol.doc/topics/cac_useresults_junit.html" ,
64+ check : func (t * testing.T , suites []Suite ) {
65+ assert .Len (t , suites , 1 )
66+ assert .Len (t , suites [0 ].Tests , 1 )
67+ assert .EqualError (t , suites [0 ].Tests [0 ].Error , "\n WARNING: Use a program name that matches the source file name\n Category: COBOL Code Review – Naming Conventions\n File: /project/PROGRAM.cbl\n Line: 2\n " )
68+ },
69+ },
70+ {
71+ title : "jenkinsci example" ,
72+ filename : "testdata/jenkinsci.xml" ,
73+ origin : "https://github.com/jenkinsci/junit-plugin/blob/master/src/test/resources/hudson/tasks/junit/junit-report-1463.xml" ,
74+ check : func (t * testing.T , suites []Suite ) {
75+ assert .Len (t , suites , 1 )
76+ assert .Len (t , suites [0 ].Tests , 6 )
77+ assert .Equal (t , "\n " , suites [0 ].Properties ["line.separator" ])
78+ assert .Equal (t , `\` , suites [0 ].Properties ["file.separator" ])
79+ },
80+ },
81+ {
82+ title : "nose2 example" ,
83+ filename : "testdata/nose2.xml" ,
84+ origin : "https://nose2.readthedocs.io/en/latest/plugins/junitxml.html" ,
85+ check : func (t * testing.T , suites []Suite ) {
86+ assert .Len (t , suites , 1 )
87+ assert .Len (t , suites [0 ].Tests , 25 )
88+ assert .EqualError (t , suites [0 ].Tests [22 ].Error , "Traceback (most recent call last):\n File \" nose2/tests/functional/support/scenario/tests_in_package/pkg1/test/test_things.py\" , line 13, in test_typeerr\n raise TypeError(\" oops\" )\n TypeError: oops\n " )
89+ },
90+ },
91+ {
92+ title : "python junit-xml example" ,
93+ filename : "testdata/python-junit-xml.xml" ,
94+ origin : "https://pypi.org/project/junit-xml/" ,
95+ check : func (t * testing.T , suites []Suite ) {
96+ assert .Len (t , suites , 1 )
97+ assert .Len (t , suites [0 ].Tests , 1 )
98+ assert .Equal (t , "\n I am stdout!\n " , suites [0 ].Tests [0 ].SystemOut )
99+ assert .Equal (t , "\n I am stderr!\n " , suites [0 ].Tests [0 ].SystemErr )
100+ },
101+ },
102+ {
103+ title : "surefire example" ,
104+ filename : "testdata/surefire.xml" ,
105+ origin : "https://gist.github.com/rwbergstrom/6f0193b1a12dca9d358e6043ee6abba4" ,
106+ check : func (t * testing.T , suites []Suite ) {
107+ assert .Len (t , suites , 1 )
108+ assert .Len (t , suites [0 ].Tests , 1 )
109+ assert .Equal (t , "\n " , suites [0 ].Properties ["line.separator" ])
110+ assert .Equal (t , "Hello, World\n " , suites [0 ].Tests [0 ].SystemOut )
111+ assert .Equal (t , "I'm an error!\n " , suites [0 ].Tests [0 ].SystemErr )
112+
113+ var testcase = Test {
114+ Name : "testStdoutStderr" ,
115+ Classname : "com.example.FooTest" ,
116+ Duration : 9 * time .Millisecond ,
117+ Status : StatusFailed ,
118+ Error : Error {
119+ Type : "java.lang.AssertionError" ,
120+ Body : "java.lang.AssertionError\n \t at com.example.FooTest.testStdoutStderr(FooTest.java:13)\n " ,
41121 },
42- Totals : Totals {
43- Tests : 1 ,
44- Passed : 1 ,
122+ Properties : map [string ]string {
123+ "classname" : "com.example.FooTest" ,
124+ "name" : "testStdoutStderr" ,
125+ "time" : "0.009" ,
45126 },
46- },
127+ SystemOut : "Hello, World\n " ,
128+ SystemErr : "I'm an error!\n " ,
129+ }
130+
131+ assert .Equal (t , testcase , suites [0 ].Tests [0 ])
47132 },
48133 },
49134 }
@@ -52,10 +137,9 @@ func TestIngest(t *testing.T) {
52137 name := fmt .Sprintf ("#%d - %s" , index + 1 , test .title )
53138
54139 t .Run (name , func (t * testing.T ) {
55- actual , err := Ingest (test .input )
56- require .Nil (t , err )
57- require .NotEmpty (t , actual )
58- require .Equal (t , test .expected , actual )
140+ suites , err := IngestFile (test .filename )
141+ require .NoError (t , err )
142+ test .check (t , suites )
59143 })
60144 }
61145}
0 commit comments