2 # subunit: extensions to python unittest to get test results from subprocesses.
3 # Copyright (C) 2005 Thomi Richards <thomi.richards@canonical.com>
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.
18 from io import BytesIO
20 from collections import namedtuple
21 from testtools import TestCase
22 from testtools.matchers import (
27 from testtools.testresult.doubles import StreamResult
29 from subunit.v2 import StreamResultToBytes, ByteStreamToStreamResult
30 from subunit._output import (
33 translate_command_name,
36 class OutputFilterArgumentTests(TestCase):
38 """Tests for the command line argument parser."""
40 def _test_command(self, command, test_id):
41 args = parse_arguments(args=[command, test_id])
43 self.assertThat(args.action, Equals(command))
44 self.assertThat(args.test_id, Equals(test_id))
46 def test_can_parse_start_test(self):
47 self._test_command('start', self.getUniqueString())
49 def test_can_parse_pass_test(self):
50 self._test_command('pass', self.getUniqueString())
52 def test_can_parse_fail_test(self):
53 self._test_command('fail', self.getUniqueString())
55 def test_can_parse_skip_test(self):
56 self._test_command('skip', self.getUniqueString())
58 def test_command_translation(self):
59 self.assertThat(translate_command_name('start'), Equals('inprogress'))
60 self.assertThat(translate_command_name('pass'), Equals('success'))
61 for command in ('fail', 'skip'):
62 self.assertThat(translate_command_name(command), Equals(command))
65 class ByteStreamCompatibilityTests(TestCase):
67 def _get_result_for(self, *commands):
68 """Get a result object from *commands.
70 Runs the 'generate_bytestream' function from subunit._output after
71 parsing *commands as if they were specified on the command line. The
72 resulting bytestream is then converted back into a result object and
78 for command_list in commands:
79 args = parse_arguments(command_list)
80 output_writer = StreamResultToBytes(output_stream=stream)
81 generate_bytestream(args, output_writer)
85 case = ByteStreamToStreamResult(source=stream)
86 result = StreamResult()
90 def test_start_generates_inprogress(self):
91 result = self._get_result_for(
97 MatchesCall(call='status', test_id='foo', test_status='inprogress')
100 def test_pass_generates_success(self):
101 result = self._get_result_for(
107 MatchesCall(call='status', test_id='foo', test_status='success')
110 def test_fail_generates_fail(self):
111 result = self._get_result_for(
117 MatchesCall(call='status', test_id='foo', test_status='fail')
120 def test_skip_generates_skip(self):
121 result = self._get_result_for(
127 MatchesCall(call='status', test_id='foo', test_status='skip')
131 class MatchesCall(Matcher):
147 def __init__(self, **kwargs):
148 unknown_kwargs = filter(
149 lambda k: k not in self._position_lookup,
153 raise ValueError("Unknown keywords: %s" % ','.join(unknown_kwargs))
154 self._filters = kwargs
156 def match(self, call_tuple):
157 for k,v in self._filters.items():
159 if call_tuple[self._position_lookup[k]] != v:
160 return Mismatch("Value for key is %r, not %r" % (self._position_lookup[k], v))
162 return Mismatch("Key %s is not present." % k)
165 return "<MatchesCall %r>" % self._filters