aboutsummaryrefslogtreecommitdiff
path: root/tests/core/go_test/xmlreport_test.go
blob: 091983566f5e4b2175ffd453f5b2da0d179de999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test_filter_test

import (
	"encoding/xml"
	"io/ioutil"
	"path/filepath"
	"reflect"
	"strings"
	"testing"

	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

func TestMain(m *testing.M) {
	bazel_testing.TestMain(m, bazel_testing.Args{
		Main: `
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
    name = "xml_test",
    importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/xml_test",
    srcs = ["xml_test.go"],
)

-- xml_test.go --
package test

import (
    "math/rand"
    "testing"
    "time"
)

func TestPass(t *testing.T) {
    t.Parallel()
}

func TestPassLog(t *testing.T) {
    t.Parallel()
    t.Log("pass")
}

func TestFail(t *testing.T) {
    t.Error("Not working")
}

func TestSubtests(t *testing.T) {
    for i, subtest := range []string{"subtest a", "testB", "another subtest"} {
        t.Run(subtest, func(t *testing.T) {
            t.Logf("from subtest %s", subtest)
            time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
            t.Logf("from subtest %s", subtest)
            if i%3 == 0 {
                t.Skip("skipping this test")
            }
            if i%2 == 0 {
                t.Fail()
            }
        })
    }
}
`,
	})
}

// test execution time attributes will vary per testrun, so we must parse the
// xml to inspect a subset of testresults
type xmlTestSuite struct {
	XMLName  xml.Name `xml:"testsuite"`
	Errors   int      `xml:"errors,attr"`
	Failures int      `xml:"failures,attr"`
	Skipped  int      `xml:"skipped,attr"`
	Tests    int      `xml:"tests,attr"`
	Name     string   `xml:"name,attr"`
}
type xmlTestSuites struct {
	XMLName xml.Name       `xml:"testsuites"`
	Suites  []xmlTestSuite `xml:"testsuite"`
}

func Test(t *testing.T) {
	tests := []struct {
		name     string
		args     []string
		expected xmlTestSuites
	}{
		{
			name: "default",
			args: []string{"test", "//:xml_test"},
			expected: xmlTestSuites{
				XMLName: xml.Name{Local: "testsuites"},
				Suites: []xmlTestSuite{{
					XMLName:  xml.Name{Local: "testsuite"},
					Name:     "github.com/bazelbuild/rules_go/tests/core/go_test/xml_test",
					Errors:   0,
					Failures: 3,
					Tests:    3,
				}},
			},
		},
		{
			name: "verbose",
			args: []string{"test", "--test_env=GO_TEST_WRAP_TESTV=1", "//:xml_test"},
			expected: xmlTestSuites{
				XMLName: xml.Name{Local: "testsuites"},
				Suites: []xmlTestSuite{{
					XMLName:  xml.Name{Local: "testsuite"},
					Name:     "github.com/bazelbuild/rules_go/tests/core/go_test/xml_test",
					Errors:   0,
					Failures: 3,
					Skipped:  1,
					Tests:    7,
				}},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := bazel_testing.RunBazel(tt.args...)
			if err == nil {
				t.Fatal("expected bazel test to have failed")
			}
			if xerr, ok := err.(*bazel_testing.StderrExitError); !ok || xerr.Err.ExitCode() != 3 {
				t.Fatalf("expected bazel tests to fail with exit code 3 (TESTS_FAILED), got: %s", err)
			}

			p, err := bazel_testing.BazelOutput("info", "bazel-testlogs")
			if err != nil {
				t.Fatal("could not find testlog root: %s", err)
			}
			path := filepath.Join(strings.TrimSpace(string(p)), "xml_test/test.xml")
			b, err := ioutil.ReadFile(path)
			if err != nil {
				t.Fatalf("could not read generated xml file: %s", err)
			}

			var suites xmlTestSuites
			if err := xml.Unmarshal(b, &suites); err != nil {
				t.Fatalf("could not unmarshall generated xml: %s", err)
			}

			if !reflect.DeepEqual(suites, tt.expected) {
				t.Fatalf("expected %#v, got: %#v", tt.expected, suites)
			}
		})
	}
}