summaryrefslogtreecommitdiff
path: root/unittests/framework/backends/test_junit.py
blob: 8f09dac16e142ed07d0beb617b20e1d0d64ece63 (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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Copyright (c) 2014, 2016 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Tests for the Junit backend package."""

from __future__ import (
    absolute_import, division, print_function, unicode_literals
)
import os
import textwrap
try:
    from lxml import etree
except ImportError:
    import xml.etree.cElementTree as etree
try:
    import mock
except ImportError:
    from unittest import mock

import pytest
import six

from framework import backends
from framework import grouptools
from framework import results
from framework import status

from . import shared

# pylint: disable=no-self-use

JUNIT_SCHEMA = os.path.join(os.path.dirname(__file__), 'schema', 'junit-7.xsd')

_XML = """\
<?xml version='1.0' encoding='utf-8'?>
  <testsuites>
    <testsuite name="piglit" tests="1">
      <testcase classname="piglit.foo.bar" name="a-test" status="pass" time="1.12345">
        <system-out>this/is/a/command\nThis is stdout</system-out>
        <system-err>this is stderr

pid: 1934
time start: 1.0
time end: 4.5
        </system-err>
      </testcase>
    </testsuite>
  </testsuites>
"""

@pytest.yield_fixture(autouse=True, scope="module")
def mock_compression():
    with mock.patch('framework.backends.compression.get_mode',
                    mock.Mock(return_value='none')):
        yield


class TestProtectedLoad(object):
    """Tests for the _load method."""

    def test_default_name(self, tmpdir):
        """backends.junit._load: uses 'junit result' for name as fallback."""
        tmpdir.chdir()
        with open('results.xml', 'w') as f:
            f.write(_XML)
        test = backends.junit.REGISTRY.load('results.xml', 'none')

        assert test.name == 'junit result'

    def test_file_name(self, tmpdir):
        """backends.junit._load: uses the filename for name if filename !=
        'results'
        """
        p = tmpdir.join('foobar.xml')
        p.write(_XML)
        test = backends.junit.REGISTRY.load(six.text_type(p), 'none')
        assert test.name == 'foobar'

    def test_folder_name(self, tmpdir):
        """backends.junit._load: uses the folder name if the result is
        'results.'
        """
        tmpdir.mkdir('foo')
        p = tmpdir.join('foo', 'results.xml')
        p.write(_XML)
        test = backends.junit.REGISTRY.load(six.text_type(p), 'none')

        assert test.name == 'foo'

    class TestReturned(object):
        """Test that the returned object is as expected."""

        testname = grouptools.join('foo', 'bar', 'a-test')

        @pytest.fixture
        def result(self, tmpdir):
            p = tmpdir.join('test.xml')
            p.write(_XML)
            return backends.junit._load(six.text_type(p))

        def test_testrunresult(self, result):
            """backends.junit._load: returns a TestrunResult instance."""
            assert isinstance(result, results.TestrunResult)

        def test_replace_sep(self, result):
            """backends.junit._load: replaces '.' with grouptools.SEPARATOR."""
            assert self.testname in result.tests

        def test_testresult_instance(self, result):
            """backends.junit._load: replaces result with TestResult instance.
            """
            assert isinstance(result.tests[self.testname], results.TestResult)

        def test_status_instance(self, result):
            """backends.junit._load: a status is found and loaded."""
            assert isinstance(result.tests[self.testname].result,
                              status.Status)

        def test_time(self, result):
            time = result.tests[self.testname].time
            assert isinstance(time, results.TimeAttribute)
            assert time.start == 1.0
            assert time.end == 4.5

        def test_command(self, result):
            """backends.junit._load: command is loaded correctly."""
            assert result.tests[self.testname].command == 'this/is/a/command'

        def test_out(self, result):
            """backends.junit._load: stdout is loaded correctly."""
            assert result.tests[self.testname].out == 'This is stdout'

        def test_err(self, result):
            """backends.junit._load: stderr is loaded correctly."""
            expected = textwrap.dedent("""\
                this is stderr

                pid: 1934
                time start: 1.0
                time end: 4.5""")
            assert result.tests[self.testname].err.strip() == expected

        def test_totals(self, result):
            """backends.junit._load: Totals are calculated."""
            assert bool(result)

        def test_pid(self, result):
            """backends.junit._load: pid is loaded correctly."""
            assert result.tests[self.testname].pid == 1934


class TestJUnitBackend(object):
    """Tests for the JUnitBackend class."""

    class TestFinalize(object):
        """Tests for the finalize method."""

        def test_skips_illformed_tests(self, tmpdir):
            """backends.junit.JUnitBackend: skips illformed tests"""
            result = results.TestResult()
            result.time.end = 1.2345
            result.result = 'pass'
            result.out = 'this is stdout'
            result.err = 'this is stderr'
            result.command = 'foo'

            test = backends.junit.JUnitBackend(six.text_type(tmpdir))
            test.initialize(shared.INITIAL_METADATA)
            with test.write_test(grouptools.join('a', 'group', 'test1')) as t:
                t(result)
            tmpdir.join('tests', '1.xml').write('bad data')

            test.finalize()


class TestJUnitWriter(object):
    """Tests for the JUnitWriter class."""

    def test_junit_replace(self, tmpdir):
        """backends.junit.JUnitBackend.write_test: grouptools.SEPARATOR is
        replaced with '.'.
        """
        result = results.TestResult()
        result.time.end = 1.2345
        result.result = 'pass'
        result.out = 'this is stdout'
        result.err = 'this is stderr'
        result.command = 'foo'

        test = backends.junit.JUnitBackend(six.text_type(tmpdir))
        test.initialize(shared.INITIAL_METADATA)
        with test.write_test(grouptools.join('a', 'group', 'test1')) as t:
            t(result)
        test.finalize()

        test_value = etree.parse(six.text_type(tmpdir.join('results.xml')))
        test_value = test_value.getroot()

        assert test_value.find('.//testcase').attrib['classname'] == \
            'piglit.a.group'

    class TestValid(object):
        @pytest.fixture
        def test_file(self, tmpdir):
            tmpdir.mkdir('foo')
            p = tmpdir.join('foo')

            result = results.TestResult()
            result.time.end = 1.2345
            result.result = 'pass'
            result.out = 'this is stdout'
            result.err = 'this is stderr'
            result.command = 'foo'
            result.pid = 1034

            test = backends.junit.JUnitBackend(six.text_type(p))
            test.initialize(shared.INITIAL_METADATA)
            with test.write_test(grouptools.join('a', 'group', 'test1')) as t:
                t(result)

            result.result = 'fail'
            with test.write_test(grouptools.join('a', 'test', 'test1')) as t:
                t(result)
            test.finalize()

            return six.text_type(p.join('results.xml'))

        def test_xml_well_formed(self, test_file):
            """backends.junit.JUnitBackend.write_test: produces well formed xml."""
            etree.parse(test_file)

        @pytest.mark.skipif(etree.__name__ != 'lxml.etree',
                            reason="This test requires lxml")
        def test_xml_valid(self, test_file):
            """backends.junit.JUnitBackend.write_test: produces valid JUnit xml."""
            # This XMLSchema class is unique to lxml
            schema = etree.XMLSchema(file=JUNIT_SCHEMA)  # pylint: disable=no-member
            with open(test_file, 'r') as f:
                assert schema.validate(etree.parse(f))