File: test_testsuite.py

package info (click to toggle)
python-testtools 2.7.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,400 kB
  • sloc: python: 14,643; makefile: 126; sh: 3
file content (442 lines) | stat: -rw-r--r-- 13,484 bytes parent folder | download
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# Copyright (c) 2009-2015 testtools developers. See LICENSE for details.

"""Test ConcurrentTestSuite and related things."""

import doctest
from pprint import pformat
import unittest

from testtools import (
    ConcurrentTestSuite,
    ConcurrentStreamTestSuite,
    iterate_tests,
    PlaceHolder,
    TestByTestResult,
    TestCase,
)
from testtools.helpers import try_import
from testtools.matchers import DocTestMatches, Equals
from testtools.testresult.doubles import StreamResult as LoggingStream
from testtools.testsuite import FixtureSuite, sorted_tests
from testtools.tests.helpers import LoggingResult

FunctionFixture = try_import("fixtures.FunctionFixture")


class Sample(TestCase):
    def __hash__(self):
        return id(self)

    def test_method1(self):
        pass

    def test_method2(self):
        pass


class TestConcurrentTestSuiteRun(TestCase):
    def test_broken_test(self):
        log = []

        def on_test(test, status, start_time, stop_time, tags, details):
            log.append((test.id(), status, set(details.keys())))

        class BrokenTest:
            # Simple break - no result parameter to run()
            def __call__(self):
                pass

            run = __call__

        original_suite = unittest.TestSuite([BrokenTest()])
        suite = ConcurrentTestSuite(original_suite, self.split_suite)
        suite.run(TestByTestResult(on_test))
        self.assertEqual([("broken-runner", "error", {"traceback"})], log)

    def test_trivial(self):
        log = []
        result = LoggingResult(log)
        test1 = Sample("test_method1")
        test2 = Sample("test_method2")
        original_suite = unittest.TestSuite([test1, test2])
        suite = ConcurrentTestSuite(original_suite, self.split_suite)
        suite.run(result)
        # log[0] is the timestamp for the first test starting.
        test1 = log[1][1]
        test2 = log[-1][1]
        self.assertIsInstance(test1, Sample)
        self.assertIsInstance(test2, Sample)
        self.assertNotEqual(test1.id(), test2.id())

    def test_wrap_result(self):
        # ConcurrentTestSuite has a hook for wrapping the per-thread result.
        wrap_log = []

        def wrap_result(thread_safe_result, thread_number):
            wrap_log.append((thread_safe_result.result.decorated, thread_number))
            return thread_safe_result

        result_log = []
        result = LoggingResult(result_log)
        test1 = Sample("test_method1")
        test2 = Sample("test_method2")
        original_suite = unittest.TestSuite([test1, test2])
        suite = ConcurrentTestSuite(
            original_suite, self.split_suite, wrap_result=wrap_result
        )
        suite.run(result)
        self.assertEqual(
            [
                (result, 0),
                (result, 1),
            ],
            wrap_log,
        )
        # Smoke test to make sure everything ran OK.
        self.assertNotEqual([], result_log)

    def split_suite(self, suite):
        return list(iterate_tests(suite))


class TestConcurrentStreamTestSuiteRun(TestCase):
    def test_trivial(self):
        result = LoggingStream()
        test1 = Sample("test_method1")
        test2 = Sample("test_method2")

        def cases():
            return [(test1, "0"), (test2, "1")]

        suite = ConcurrentStreamTestSuite(cases)
        suite.run(result)

        def freeze(set_or_none):
            if set_or_none is None:
                return set_or_none
            return frozenset(set_or_none)

        # Ignore event order: we're testing the code is all glued together,
        # which just means we can pump events through and they get route codes
        # added appropriately.
        self.assertEqual(
            {
                (
                    "status",
                    "testtools.tests.test_testsuite.Sample.test_method1",
                    "inprogress",
                    None,
                    True,
                    None,
                    None,
                    False,
                    None,
                    "0",
                    None,
                ),
                (
                    "status",
                    "testtools.tests.test_testsuite.Sample.test_method1",
                    "success",
                    frozenset(),
                    True,
                    None,
                    None,
                    False,
                    None,
                    "0",
                    None,
                ),
                (
                    "status",
                    "testtools.tests.test_testsuite.Sample.test_method2",
                    "inprogress",
                    None,
                    True,
                    None,
                    None,
                    False,
                    None,
                    "1",
                    None,
                ),
                (
                    "status",
                    "testtools.tests.test_testsuite.Sample.test_method2",
                    "success",
                    frozenset(),
                    True,
                    None,
                    None,
                    False,
                    None,
                    "1",
                    None,
                ),
            },
            {
                event[0:3] + (freeze(event[3]),) + event[4:10] + (None,)
                for event in result._events
            },
        )

    def test_broken_runner(self):
        # If the object called breaks, the stream is informed about it
        # regardless.
        class BrokenTest:
            # broken - no result parameter!
            def __call__(self):
                pass

            def run(self):
                pass

        result = LoggingStream()

        def cases():
            return [(BrokenTest(), "0")]

        suite = ConcurrentStreamTestSuite(cases)
        suite.run(result)
        events = result._events
        # Check the traceback loosely.
        self.assertEqual(
            events[1][6].decode("utf8"), "Traceback (most recent call last):\n"
        )
        self.assertThat(
            events[2][6].decode("utf8"),
            DocTestMatches(
                """\
  File "...testtools/testsuite.py", line ..., in _run_test
    test.run(process_result)...
""",
                doctest.ELLIPSIS,
            ),
        )
        self.assertThat(
            events[3][6].decode("utf8"),
            DocTestMatches(
                """\
TypeError: ...run() takes ...1 ...argument...2...given...
""",
                doctest.ELLIPSIS,
            ),
        )
        events = [event[0:10] + (None,) for event in events]
        events[1] = events[1][:6] + (None,) + events[1][7:]
        events[2] = events[2][:6] + (None,) + events[2][7:]
        events[3] = events[3][:6] + (None,) + events[3][7:]
        self.assertEqual(
            [
                (
                    "status",
                    "broken-runner-'0'",
                    "inprogress",
                    None,
                    True,
                    None,
                    None,
                    False,
                    None,
                    "0",
                    None,
                ),
                (
                    "status",
                    "broken-runner-'0'",
                    None,
                    None,
                    True,
                    "traceback",
                    None,
                    False,
                    'text/x-traceback; charset="utf8"; language="python"',
                    "0",
                    None,
                ),
                (
                    "status",
                    "broken-runner-'0'",
                    None,
                    None,
                    True,
                    "traceback",
                    None,
                    False,
                    'text/x-traceback; charset="utf8"; language="python"',
                    "0",
                    None,
                ),
                (
                    "status",
                    "broken-runner-'0'",
                    None,
                    None,
                    True,
                    "traceback",
                    None,
                    True,
                    'text/x-traceback; charset="utf8"; language="python"',
                    "0",
                    None,
                ),
                (
                    "status",
                    "broken-runner-'0'",
                    "fail",
                    set(),
                    True,
                    None,
                    None,
                    False,
                    None,
                    "0",
                    None,
                ),
            ],
            events,
        )

    def split_suite(self, suite):
        tests = list(enumerate(iterate_tests(suite)))
        return [(test, str(pos)) for pos, test in tests]

    def test_setupclass_skip(self):
        # We should support setupclass skipping using cls.skipException.
        # Because folk have used that.
        class Skips(TestCase):
            @classmethod
            def setUpClass(cls):
                raise cls.skipException("foo")

            def test_notrun(self):
                pass

        # Test discovery uses the default suite from unittest (unless users
        # deliberately change things, in which case they keep both pieces).
        suite = unittest.TestSuite([Skips("test_notrun")])
        log = []
        result = LoggingResult(log)
        suite.run(result)
        self.assertEqual(["addSkip"], [item[0] for item in log])

    def test_setupclass_upcall(self):
        # Note that this is kindof-a-case-test, kindof-suite, because
        # setUpClass is linked between them.
        class Simples(TestCase):
            @classmethod
            def setUpClass(cls):
                super().setUpClass()

            def test_simple(self):
                pass

        # Test discovery uses the default suite from unittest (unless users
        # deliberately change things, in which case they keep both pieces).
        suite = unittest.TestSuite([Simples("test_simple")])
        log = []
        result = LoggingResult(log)
        suite.run(result)
        self.assertEqual(
            ["startTest", "addSuccess", "stopTest"], [item[0] for item in log]
        )


class TestFixtureSuite(TestCase):
    def setUp(self):
        super().setUp()
        if FunctionFixture is None:
            self.skipTest("Need fixtures")

    def test_fixture_suite(self):
        log = []

        class Sample(TestCase):
            def test_one(self):
                log.append(1)

            def test_two(self):
                log.append(2)

        fixture = FunctionFixture(
            lambda: log.append("setUp"), lambda fixture: log.append("tearDown")
        )
        suite = FixtureSuite(fixture, [Sample("test_one"), Sample("test_two")])
        suite.run(LoggingResult([]))
        self.assertEqual(["setUp", 1, 2, "tearDown"], log)

    def test_fixture_suite_sort(self):
        log = []

        class Sample(TestCase):
            def test_one(self):
                log.append(1)

            def test_two(self):
                log.append(2)

        fixture = FunctionFixture(
            lambda: log.append("setUp"), lambda fixture: log.append("tearDown")
        )
        suite = FixtureSuite(fixture, [Sample("test_one"), Sample("test_one")])
        self.assertRaises(ValueError, suite.sort_tests)


class TestSortedTests(TestCase):
    def test_sorts_custom_suites(self):
        a = PlaceHolder("a")
        b = PlaceHolder("b")

        class Subclass(unittest.TestSuite):
            def sort_tests(self):
                self._tests = sorted_tests(self, True)

        input_suite = Subclass([b, a])
        suite = sorted_tests(input_suite)
        self.assertEqual([a, b], list(iterate_tests(suite)))
        self.assertEqual([input_suite], list(iter(suite)))

    def test_custom_suite_without_sort_tests_works(self):
        a = PlaceHolder("a")
        b = PlaceHolder("b")

        class Subclass(unittest.TestSuite):
            pass

        input_suite = Subclass([b, a])
        suite = sorted_tests(input_suite)
        self.assertEqual([b, a], list(iterate_tests(suite)))
        self.assertEqual([input_suite], list(iter(suite)))

    def test_sorts_simple_suites(self):
        a = PlaceHolder("a")
        b = PlaceHolder("b")
        suite = sorted_tests(unittest.TestSuite([b, a]))
        self.assertEqual([a, b], list(iterate_tests(suite)))

    def test_duplicate_simple_suites(self):
        a = PlaceHolder("a")
        b = PlaceHolder("b")
        c = PlaceHolder("a")
        self.assertRaises(ValueError, sorted_tests, unittest.TestSuite([a, b, c]))

    def test_multiple_duplicates(self):
        # If there are multiple duplicates on a test suite, we report on them
        # all.
        a = PlaceHolder("a")
        b = PlaceHolder("b")
        c = PlaceHolder("a")
        d = PlaceHolder("b")
        error = self.assertRaises(
            ValueError, sorted_tests, unittest.TestSuite([a, b, c, d])
        )
        self.assertThat(
            str(error),
            Equals("Duplicate test ids detected: {}".format(pformat({"a": 2, "b": 2}))),
        )


def test_suite():
    from unittest import TestLoader

    return TestLoader().loadTestsFromName(__name__)