diff --git a/tests/doc.md b/tests/doc.md index 4c351497..5f9c887a 100644 --- a/tests/doc.md +++ b/tests/doc.md @@ -274,6 +274,45 @@ This section describes more advanced features that are probably not necessary 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 For tests that modify their input file it is useful to run these with a diff --git a/tests/system_tests.py b/tests/system_tests.py index 3e0b1205..f35e35e0 100644 --- a/tests/system_tests.py +++ b/tests/system_tests.py @@ -477,8 +477,26 @@ def test_run(self): got_stdout, got_stderr = proc.communicate() t.cancel() - processed_stdout = _process_output_post(got_stdout.decode('utf-8')) - processed_stderr = _process_output_post(got_stderr.decode('utf-8')) + processed_stdout = None + 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: print( @@ -506,6 +524,10 @@ class Case(unittest.TestCase): #: maxDiff set so that arbitrarily large diffs will be shown 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 def setUpClass(cls): """