1 # SPDX-License-Identifier: BSD-2-Clause
3 # Copyright (c) 2016, Matt Layman
6 from unittest
import TextTestResult
, TextTestRunner
7 from unittest
.runner
import _WritelnDecorator
10 from tap
import formatter
11 from tap
.i18n
import _
12 from tap
.tracker
import Tracker
15 class TAPTestResult(TextTestResult
):
19 def __init__(self
, stream
, descriptions
, verbosity
):
20 super(TAPTestResult
, self
).__init
__(stream
, descriptions
, verbosity
)
22 def stopTestRun(self
):
23 """Once the test run is complete, generate each of the TAP files."""
24 super(TAPTestResult
, self
).stopTestRun()
25 self
.tracker
.generate_tap_reports()
27 def addError(self
, test
, err
):
28 super(TAPTestResult
, self
).addError(test
, err
)
29 diagnostics
= formatter
.format_exception(err
)
30 self
.tracker
.add_not_ok(
31 self
._cls
_name
(test
), self
._description
(test
), diagnostics
=diagnostics
34 def addFailure(self
, test
, err
):
35 super(TAPTestResult
, self
).addFailure(test
, err
)
36 diagnostics
= formatter
.format_exception(err
)
37 self
.tracker
.add_not_ok(
38 self
._cls
_name
(test
), self
._description
(test
), diagnostics
=diagnostics
41 def addSuccess(self
, test
):
42 super(TAPTestResult
, self
).addSuccess(test
)
43 self
.tracker
.add_ok(self
._cls
_name
(test
), self
._description
(test
))
45 def addSkip(self
, test
, reason
):
46 super(TAPTestResult
, self
).addSkip(test
, reason
)
47 self
.tracker
.add_skip(self
._cls
_name
(test
), self
._description
(test
), reason
)
49 def addExpectedFailure(self
, test
, err
):
50 super(TAPTestResult
, self
).addExpectedFailure(test
, err
)
51 diagnostics
= formatter
.format_exception(err
)
52 self
.tracker
.add_not_ok(
54 self
._description
(test
),
55 _('(expected failure)'),
56 diagnostics
=diagnostics
,
59 def addUnexpectedSuccess(self
, test
):
60 super(TAPTestResult
, self
).addUnexpectedSuccess(test
)
62 self
._cls
_name
(test
), self
._description
(test
), _('(unexpected success)')
65 def _cls_name(self
, test
):
66 return test
.__class
__.__name
__
68 def _description(self
, test
):
71 return self
.FORMAT
.format(
72 method_name
=str(test
),
73 short_description
=test
.shortDescription() or '',
78 'Bad format string: {format}\n'
79 'Replacement options are: {{short_description}} and '
81 ).format(format
=self
.FORMAT
)
84 return test
.shortDescription() or str(test
)
87 # TODO: 2016-7-30 mblayman - Since the 2.6 signature is no longer relevant,
88 # check the possibility of removing the module level scope.
90 # Module level state stinks, but this is the only way to keep compatibility
91 # with Python 2.6. The best place for the tracker is as an instance variable
92 # on the runner, but __init__ is so different that it is not easy to create
93 # a runner that satisfies every supported Python version.
97 class TAPTestRunner(TextTestRunner
):
98 """A test runner that will behave exactly like TextTestRunner and will
99 additionally generate TAP files for each test case"""
101 resultclass
= TAPTestResult
103 def set_stream(self
, streaming
):
104 """Set the streaming boolean option to stream TAP directly to stdout.
106 The test runner default output will be suppressed in favor of TAP.
108 self
.stream
= _WritelnDecorator(open(os
.devnull
, 'w'))
109 _tracker
.streaming
= streaming
110 _tracker
.stream
= sys
.stdout
112 def _makeResult(self
):
113 result
= self
.resultclass(self
.stream
, self
.descriptions
, self
.verbosity
)
114 result
.tracker
= _tracker
118 def set_outdir(cls
, outdir
):
119 """Set the output directory so that TAP files are written to the
120 specified outdir location.
122 # Blame the lack of unittest extensibility for this hacky method.
123 _tracker
.outdir
= outdir
126 def set_combined(cls
, combined
):
127 """Set the tracker to use a single output file."""
128 _tracker
.combined
= combined
131 def set_header(cls
, header
):
132 """Set the header display flag."""
133 _tracker
.header
= header
136 def set_format(cls
, fmt
):
137 """Set the format of each test line.
139 The format string can use:
140 * {method_name}: The test method name
141 * {short_description}: The test's docstring short description
143 TAPTestResult
.FORMAT
= fmt
This page took 0.036718 seconds and 4 git commands to generate.