...

Source file src/github.com/joshdk/go-junit/ingest_test.go

Documentation: github.com/joshdk/go-junit

     1  // Copyright Josh Komoroske. All rights reserved.
     2  // Use of this source code is governed by the MIT license,
     3  // a copy of which can be found in the LICENSE.txt file.
     4  
     5  package junit
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestExamplesInTheWild(t *testing.T) {
    17  	tests := []struct {
    18  		title    string
    19  		filename string
    20  		origin   string
    21  		check    func(*testing.T, []Suite)
    22  	}{
    23  		{
    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\nfile_test.go:11: Longer\n\terror\n\tmessage.")
    58  			},
    59  		},
    60  		{
    61  			title:    "go-junit-report skipped example",
    62  			filename: "testdata/go-junit-report-skipped.xml",
    63  			origin:   "https://github.com/jstemmer/go-junit-report/blob/master/testdata/03-report.xml",
    64  			check: func(t *testing.T, suites []Suite) {
    65  				assert.Len(t, suites, 1)
    66  				assert.Len(t, suites[0].Tests, 2)
    67  				assert.Equal(t, "package/name", suites[0].Name)
    68  				assert.Equal(t, "TestOne", suites[0].Tests[0].Name)
    69  				assert.Equal(t, "file_test.go:11: Skip message", suites[0].Tests[0].Message)
    70  			},
    71  		},
    72  		{
    73  			title:    "ibm example",
    74  			filename: "testdata/ibm.xml",
    75  			origin:   "https://www.ibm.com/support/knowledgecenter/en/SSQ2R2_14.2.0/com.ibm.rsar.analysis.codereview.cobol.doc/topics/cac_useresults_junit.html",
    76  			check: func(t *testing.T, suites []Suite) {
    77  				assert.Len(t, suites, 1)
    78  				assert.Len(t, suites[0].Tests, 1)
    79  				assert.EqualError(t, suites[0].Tests[0].Error, "\nWARNING: Use a program name that matches the source file name\nCategory: COBOL Code Review – Naming Conventions\nFile: /project/PROGRAM.cbl\nLine: 2\n      ")
    80  			},
    81  		},
    82  		{
    83  			title:    "jenkinsci example",
    84  			filename: "testdata/jenkinsci.xml",
    85  			origin:   "https://github.com/jenkinsci/junit-plugin/blob/master/src/test/resources/hudson/tasks/junit/junit-report-1463.xml",
    86  			check: func(t *testing.T, suites []Suite) {
    87  				assert.Len(t, suites, 1)
    88  				assert.Len(t, suites[0].Tests, 6)
    89  				assert.Equal(t, "\n", suites[0].Properties["line.separator"])
    90  				assert.Equal(t, `\`, suites[0].Properties["file.separator"])
    91  			},
    92  		},
    93  		{
    94  			title:    "nose2 example",
    95  			filename: "testdata/nose2.xml",
    96  			origin:   "https://nose2.readthedocs.io/en/latest/plugins/junitxml.html",
    97  			check: func(t *testing.T, suites []Suite) {
    98  				assert.Len(t, suites, 1)
    99  				assert.Len(t, suites[0].Tests, 25)
   100  				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\")\nTypeError: oops\n")
   101  			},
   102  		},
   103  		{
   104  			title:    "python junit-xml example",
   105  			filename: "testdata/python-junit-xml.xml",
   106  			origin:   "https://pypi.org/project/junit-xml/",
   107  			check: func(t *testing.T, suites []Suite) {
   108  				assert.Len(t, suites, 1)
   109  				assert.Len(t, suites[0].Tests, 1)
   110  				assert.Equal(t, "\n                I am stdout!\n            ", suites[0].Tests[0].SystemOut)
   111  				assert.Equal(t, "\n                I am stderr!\n            ", suites[0].Tests[0].SystemErr)
   112  			},
   113  		},
   114  		{
   115  			title:    "surefire example",
   116  			filename: "testdata/surefire.xml",
   117  			origin:   "https://gist.github.com/rwbergstrom/6f0193b1a12dca9d358e6043ee6abba4",
   118  			check: func(t *testing.T, suites []Suite) {
   119  				assert.Len(t, suites, 1)
   120  				assert.Len(t, suites[0].Tests, 1)
   121  				assert.Equal(t, "\n", suites[0].Properties["line.separator"])
   122  				assert.Equal(t, "Hello, World\n", suites[0].Tests[0].SystemOut)
   123  				assert.Equal(t, "I'm an error!\n", suites[0].Tests[0].SystemErr)
   124  
   125  				var testcase = Test{
   126  					Name:      "testStdoutStderr",
   127  					Classname: "com.example.FooTest",
   128  					Duration:  1234560 * time.Millisecond,
   129  					Status:    StatusFailed,
   130  					Error: Error{
   131  						Type: "java.lang.AssertionError",
   132  						Body: "java.lang.AssertionError\n\tat com.example.FooTest.testStdoutStderr(FooTest.java:13)\n",
   133  					},
   134  					Properties: map[string]string{
   135  						"classname": "com.example.FooTest",
   136  						"name":      "testStdoutStderr",
   137  						"time":      "1,234.56",
   138  					},
   139  					SystemOut: "Hello, World\n",
   140  					SystemErr: "I'm an error!\n",
   141  				}
   142  
   143  				assert.Equal(t, testcase, suites[0].Tests[0])
   144  			},
   145  		},
   146  		{
   147  			title:    "fastlane example",
   148  			filename: "testdata/fastlane-trainer.xml",
   149  			check: func(t *testing.T, suites []Suite) {
   150  				assert.Len(t, suites, 1)
   151  				assert.Len(t, suites[0].Tests, 4)
   152  
   153  				var testcase = Test{
   154  					Name:      "testSomething()",
   155  					Classname: "TestClassSample",
   156  					Duration:  342 * time.Millisecond,
   157  					Status:    StatusFailed,
   158  					Message:   "XCTAssertTrue failed",
   159  					Error: Error{
   160  						Message: "XCTAssertTrue failed",
   161  						Body:    "\n            ",
   162  					},
   163  					Properties: map[string]string{
   164  						"classname": "TestClassSample",
   165  						"name":      "testSomething()",
   166  						"time":      "0.342",
   167  					},
   168  				}
   169  
   170  				assert.Equal(t, testcase, suites[0].Tests[2])
   171  				assert.EqualError(t, suites[0].Tests[2].Error, "XCTAssertTrue failed")
   172  				assert.EqualError(t, suites[0].Tests[3].Error, "NullPointerException")
   173  			},
   174  		},
   175  		{
   176  			title:    "phpunit example",
   177  			filename: "testdata/phpunit.xml",
   178  			check: func(t *testing.T, suites []Suite) {
   179  				assert.Len(t, suites, 1)
   180  				assert.Len(t, suites[0].Tests, 0)
   181  				assert.Len(t, suites[0].Suites, 1)
   182  
   183  				suite := suites[0].Suites[0]
   184  				assert.Len(t, suite.Tests, 1)
   185  				assert.Len(t, suite.Suites, 2)
   186  
   187  				assert.Equal(t, "SampleTest", suite.Name)
   188  				assert.Equal(t, "/untitled/tests/SampleTest.php", suite.Properties["file"])
   189  
   190  				var testcase = Test{
   191  					Name:      "testA",
   192  					Classname: "SampleTest",
   193  					Duration:  5917 * time.Microsecond,
   194  					Status:    StatusPassed,
   195  					Properties: map[string]string{
   196  						"assertions": "1",
   197  						"class":      "SampleTest",
   198  						"classname":  "SampleTest",
   199  						"file":       "/untitled/tests/SampleTest.php",
   200  						"line":       "7",
   201  						"name":       "testA",
   202  						"time":       "0.005917",
   203  					},
   204  				}
   205  
   206  				assert.Equal(t, testcase, suite.Tests[0])
   207  
   208  				assert.Len(t, suite.Suites[1].Suites, 0)
   209  				assert.Len(t, suite.Suites[1].Tests, 3)
   210  				assert.Equal(t, "testC with data set #0", suite.Suites[1].Tests[0].Name)
   211  
   212  				// checking recursive aggregation
   213  				suites[0].Aggregate()
   214  				actualTotals := suites[0].Totals
   215  				expectedTotals := Totals{
   216  					Tests:    7,
   217  					Passed:   4,
   218  					Skipped:  0,
   219  					Failed:   3,
   220  					Error:    0,
   221  					Duration: 8489 * time.Microsecond,
   222  				}
   223  				assert.Equal(t, expectedTotals, actualTotals)
   224  			},
   225  		},
   226  	}
   227  
   228  	for index, test := range tests {
   229  		name := fmt.Sprintf("#%d - %s", index+1, test.title)
   230  
   231  		t.Run(name, func(t *testing.T) {
   232  			suites, err := IngestFile(test.filename)
   233  			require.NoError(t, err)
   234  			test.check(t, suites)
   235  		})
   236  	}
   237  }
   238  

View as plain text