[testsuite] Added option to use different encodings for the output

v0.27.3
Dan Čermák 7 years ago
parent dd3bcaf41f
commit b6d93558ee

@ -274,6 +274,45 @@ This section describes more advanced features that are probably not necessary
the "standard" usage of the test suite. the "standard" usage of the test suite.
### Using a different output encoding
The test suite will try to interpret the program's output as utf-8 encoded
strings and if that fails it will try the `iso-8859-1` encoding (also know as
`latin-1`).
If the tested program outputs characters in another encoding then it can be
supplied as the `encodings` parameter in each test case:
``` python
# -*- coding: utf-8 -*-
import system_tests
class AnInformativeName(metaclass=system_tests.CaseMeta):
encodings = ['ascii']
filename = "invalid_input_file"
commands = [
"$binary -c $import_file -i $filename"
]
retval = ["$abort_exit_value"]
stdout = ["Reading $filename"]
stderr = [
"""$abort_error
error in $filename
"""
]
```
The test suite will try to decode the program's output with the provided
encodings in the order that they appear in the list. It will select the first
encoding that can decode the output successfully. If no encoding is able to
decode the program's output, then an error is raised. The list of all supported
encodings can be found
[here](https://docs.python.org/3/library/codecs.html#standard-encodings).
### Creating file copies ### Creating file copies
For tests that modify their input file it is useful to run these with a For tests that modify their input file it is useful to run these with a

@ -477,8 +477,26 @@ def test_run(self):
got_stdout, got_stderr = proc.communicate() got_stdout, got_stderr = proc.communicate()
t.cancel() t.cancel()
processed_stdout = _process_output_post(got_stdout.decode('utf-8')) processed_stdout = None
processed_stderr = _process_output_post(got_stderr.decode('utf-8')) processed_stderr = None
for encoding in self.encodings:
try:
processed_stdout = _process_output_post(
got_stdout.decode(encoding)
)
processed_stderr = _process_output_post(
got_stderr.decode(encoding)
)
except UnicodeError:
pass
else:
break
if processed_stdout is None or processed_stderr is None:
raise ValueError(
"Could not decode the output of the command '{!s}' with the "
"following encodings: {!s}"
.format(command, ','.join(self.encodings))
)
if _debug_mode: if _debug_mode:
print( print(
@ -506,6 +524,10 @@ class Case(unittest.TestCase):
#: maxDiff set so that arbitrarily large diffs will be shown #: maxDiff set so that arbitrarily large diffs will be shown
maxDiff = None maxDiff = None
#: list of encodings that are used to decode the test program's output
#: the first encoding that does not raise a UnicodeError is used
encodings = ['utf-8', 'iso-8859-1']
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
""" """

Loading…
Cancel
Save