junit_spec.rb 7.26 KB
Newer Older
1
2
# frozen_string_literal: true

3
require 'fast_spec_helper'
4

5
describe Gitlab::Ci::Parsers::Test::Junit do
6
  describe '#parse!' do
7
    subject { described_class.new.parse!(junit, test_suite) }
8
9
10
11

    let(:test_suite) { Gitlab::Ci::Reports::TestSuite.new('rspec') }
    let(:test_cases) { flattened_test_cases(test_suite) }

12
    context 'when data is JUnit style XML' do
13
      context 'when there are no <testcases> in <testsuite>' do
14
15
16
17
18
19
        let(:junit) do
          <<-EOF.strip_heredoc
            <testsuite></testsuite>
          EOF
        end

20
21
        it 'ignores the case' do
          expect { subject }.not_to raise_error
22
23
24
25
26

          expect(test_cases.count).to eq(0)
        end
      end

27
28
29
30
31
32
33
34
35
36
37
38
39
40
      context 'when there are no <testcases> in <testsuites>' do
        let(:junit) do
          <<-EOF.strip_heredoc
            <testsuites><testsuite /></testsuites>
          EOF
        end

        it 'ignores the case' do
          expect { subject }.not_to raise_error

          expect(test_cases.count).to eq(0)
        end
      end

41
      context 'when there is only one <testsuite> in <testsuites>' do
42
43
        let(:junit) do
          <<-EOF.strip_heredoc
44
45
46
47
48
            <testsuites>
              <testsuite>
                <testcase classname='Calculator' name='sumTest1' time='0.01'></testcase>
              </testsuite>
            </testsuites>
49
50
51
52
53
54
55
56
57
58
59
60
          EOF
        end

        it 'parses XML and adds a test case to a suite' do
          expect { subject }.not_to raise_error

          expect(test_cases[0].classname).to eq('Calculator')
          expect(test_cases[0].name).to eq('sumTest1')
          expect(test_cases[0].execution_time).to eq(0.01)
        end
      end

61
      context 'when there is <testcase>' do
62
63
64
        let(:junit) do
          <<-EOF.strip_heredoc
              <testsuite>
65
66
67
                <testcase classname='Calculator' name='sumTest1' time='0.01'>
                  #{testcase_content}
                </testcase>
68
69
70
71
              </testsuite>
          EOF
        end

72
73
74
        let(:test_case) { test_cases[0] }

        before do
75
          expect { subject }.not_to raise_error
76
        end
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
        shared_examples_for '<testcase> XML parser' do |status, output|
          it 'parses XML and adds a test case to the suite' do
            aggregate_failures do
              expect(test_case.classname).to eq('Calculator')
              expect(test_case.name).to eq('sumTest1')
              expect(test_case.execution_time).to eq(0.01)
              expect(test_case.status).to eq(status)
              expect(test_case.system_output).to eq(output)
            end
          end
        end

        context 'and has failure' do
          let(:testcase_content) { '<failure>Some failure</failure>' }

          it_behaves_like '<testcase> XML parser',
            ::Gitlab::Ci::Reports::TestCase::STATUS_FAILED,
            'Some failure'
        end

        context 'and has error' do
          let(:testcase_content) { '<error>Some error</error>' }

          it_behaves_like '<testcase> XML parser',
            ::Gitlab::Ci::Reports::TestCase::STATUS_FAILED,
            'Some error'
        end

        context 'and has an unknown type' do
          let(:testcase_content) { '<foo>Some foo</foo>' }

          it_behaves_like '<testcase> XML parser',
            ::Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS,
            nil
        end

        context 'and has no content' do
          let(:testcase_content) { '' }

          it_behaves_like '<testcase> XML parser',
            ::Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS,
            nil
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        end
      end

      context 'PHPUnit' do
        let(:junit) do
          <<-EOF.strip_heredoc
          <testsuites>
            <testsuite name="Project Test Suite" tests="1" assertions="1" failures="0" errors="0" time="1.376748">
              <testsuite name="XXX\\FrontEnd\\WebBundle\\Tests\\Controller\\LogControllerTest" file="/Users/mcfedr/projects/xxx/server/tests/XXX/FrontEnd/WebBundle/Tests/Controller/LogControllerTest.php" tests="1" assertions="1" failures="0" errors="0" time="1.376748">
                <testcase name="testIndexAction" class="XXX\\FrontEnd\\WebBundle\\Tests\\Controller\\LogControllerTest" file="/Users/mcfedr/projects/xxx/server/tests/XXX/FrontEnd/WebBundle/Tests/Controller/LogControllerTest.php" line="9" assertions="1" time="1.376748"/>
              </testsuite>
            </testsuite>
          </testsuites>
          EOF
        end

        it 'parses XML and adds a test case to a suite' do
          expect { subject }.not_to raise_error

          expect(test_cases.count).to eq(1)
        end
      end

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
      context 'when there are two test cases' do
        let(:junit) do
          <<-EOF.strip_heredoc
            <testsuite>
              <testcase classname='Calculator' name='sumTest1' time='0.01'></testcase>
              <testcase classname='Calculator' name='sumTest2' time='0.02'></testcase>
            </testsuite>
          EOF
        end

        it 'parses XML and adds test cases to a suite' do
          expect { subject }.not_to raise_error

          expect(test_cases[0].classname).to eq('Calculator')
          expect(test_cases[0].name).to eq('sumTest1')
          expect(test_cases[0].execution_time).to eq(0.01)
          expect(test_cases[1].classname).to eq('Calculator')
          expect(test_cases[1].name).to eq('sumTest2')
          expect(test_cases[1].execution_time).to eq(0.02)
        end
      end

      context 'when there are two test suites' do
        let(:junit) do
          <<-EOF.strip_heredoc
            <testsuites>
              <testsuite>
                <testcase classname='Calculator' name='sumTest1' time='0.01'></testcase>
                <testcase classname='Calculator' name='sumTest2' time='0.02'></testcase>
              </testsuite>
              <testsuite>
                <testcase classname='Statemachine' name='happy path' time='100'></testcase>
                <testcase classname='Statemachine' name='unhappy path' time='200'></testcase>
              </testsuite>
            </testsuites>
          EOF
        end

        it 'parses XML and adds test cases to a suite' do
          expect { subject }.not_to raise_error

          expect(test_cases[0].classname).to eq('Calculator')
          expect(test_cases[0].name).to eq('sumTest1')
          expect(test_cases[0].execution_time).to eq(0.01)
          expect(test_cases[1].classname).to eq('Calculator')
          expect(test_cases[1].name).to eq('sumTest2')
          expect(test_cases[1].execution_time).to eq(0.02)
          expect(test_cases[2].classname).to eq('Statemachine')
          expect(test_cases[2].name).to eq('happy path')
          expect(test_cases[2].execution_time).to eq(100)
          expect(test_cases[3].classname).to eq('Statemachine')
          expect(test_cases[3].name).to eq('unhappy path')
          expect(test_cases[3].execution_time).to eq(200)
        end
      end
    end

200
201
202
203
204
205
206
207
208
209
    context 'when data is not JUnit style XML' do
      let(:junit) { { testsuite: 'abc' }.to_json }

      it 'raises an error' do
        expect { subject }.to raise_error(described_class::JunitParserError)
      end
    end

    private

210
211
212
213
214
215
216
217
218
    def flattened_test_cases(test_suite)
      test_suite.test_cases.map do |status, value|
        value.map do |key, test_case|
          test_case
        end
      end.flatten
    end
  end
end