2 # subunit: extensions to python unittest to get test results from subprocesses.
3 # Copyright (C) 2008 Robert Collins <robertc@robertcollins.net>
5 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
6 # license at the users choice. A copy of both licenses are available in the
7 # project source as Apache-2.0 and BSD. You may not use this file except in
8 # compliance with one of these two licences.
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # license you chose for the specific language governing permissions and
14 # limitations under that license.
17 """List tests in a subunit stream."""
19 from optparse
import OptionParser
23 from subunit
import DiscardStream
, ProtocolTestCase
25 class TestIdPrintingResult(unittest
.TestResult
):
27 def __init__(self
, stream
, show_times
=False):
28 """Create a FilterResult object outputting to stream."""
29 unittest
.TestResult
.__init
__(self
)
33 self
.show_times
= show_times
35 self
._test
_duration
= 0
37 def addError(self
, test
, err
):
38 self
.failed_tests
+= 1
41 def addFailure(self
, test
, err
):
42 self
.failed_tests
+= 1
45 def addSuccess(self
, test
):
48 def reportTest(self
, test
, duration
):
50 seconds
= duration
.seconds
51 seconds
+= duration
.days
* 3600 * 24
52 seconds
+= duration
.microseconds
/ 1000000.0
53 self
._stream
.write(test
.id() + ' %0.3f\n' % seconds
)
55 self
._stream
.write(test
.id() + '\n')
57 def startTest(self
, test
):
58 self
._start
_time
= self
._time
()
60 def stopTest(self
, test
):
61 test_duration
= self
._time
() - self
._start
_time
62 self
.reportTest(self
._test
, test_duration
)
70 def wasSuccessful(self
):
71 "Tells whether or not this result was a success"
72 return self
.failed_tests
== 0
75 parser
= OptionParser(description
=__doc__
)
76 parser
.add_option("--times", action
="store_true",
77 help="list the time each test took (requires a timestamped stream)",
79 parser
.add_option("--no-passthrough", action
="store_true",
80 help="Hide all non subunit input.", default
=False, dest
="no_passthrough")
81 (options
, args
) = parser
.parse_args()
82 result
= TestIdPrintingResult(sys
.stdout
, options
.times
)
83 if options
.no_passthrough
:
84 passthrough_stream
= DiscardStream()
86 passthrough_stream
= None
87 test
= ProtocolTestCase(sys
.stdin
, passthrough
=passthrough_stream
)
89 if result
.wasSuccessful():