[elbe-devel] [PATCH 21/37] Use debian packaged python module

Eduard Krein eduard.krein at linutronix.de
Wed Feb 7 16:21:56 CET 2024



Am 07.02.2024 um 15:28 schrieb Benedikt Spranger:
> The python module "junit_xml" is not packaged in Debian, while the
> module "junitxml" is. Rework the test runner and use "junitxml".
> 
> Signed-off-by: Benedikt Spranger <b.spranger at linutronix.de>
Reviewed-by: Eduard Krein <eduard.krein at linutronix.de>
> ---
>  debian/control            |  1 +
>  elbepack/commands/test.py | 86 +++++++--------------------------------
>  2 files changed, 16 insertions(+), 71 deletions(-)
> 
> diff --git a/debian/control b/debian/control
> index 401bb381..a6c1d0f7 100644
> --- a/debian/control
> +++ b/debian/control
> @@ -21,6 +21,7 @@ Build-Depends: debhelper-compat (= 12),
>    python3-beaker,
>    python3-cherrypy3,
>    python3-debian,
> +  python3-junitxml,
>    python3-passlib,
>    python3-spyne,
>    python3-sqlalchemy
> diff --git a/elbepack/commands/test.py b/elbepack/commands/test.py
> index 2f3e76ef..72fabb5d 100644
> --- a/elbepack/commands/test.py
> +++ b/elbepack/commands/test.py
> @@ -5,13 +5,14 @@
>  # elbepack/commands/test.py - Elbe unit test wrapper
>  
>  import enum
> +import io
>  import optparse
>  import os
>  import re
>  import unittest
>  import warnings
>  
> -import junit_xml as junit
> +import junitxml as junit
>  
>  from elbepack.shellhelper import command_out
>  
> @@ -133,79 +134,23 @@ class ElbeTestResult(unittest.TestResult):
>  
>      def __init__(self):
>          super().__init__()
> -        self.cases = []
> -        self.current_case = None
> +        self.buffer = io.StringIO()
> +        self.result = junit.JUnitXmlResult(self.buffer)
> +        self.success = False
>  
> -    def startTest(self, test):
> -        self.current_case = junit.TestCase(name=str(test))
> -        self.cases.append(self.current_case)
> -        super().startTest(test)
> +    def run_testsuite(self, suite):
> +        self.result.startTestRun()
> +        unittest.TestSuite(suite).run(self.result)
> +        self.result.stopTestRun()
> +        self.success = self.result.wasSuccessful()
>  
> -    def addError(self, test, err):
> -        """Called when an error has occurred. 'err' is a tuple of values as
> -           returned by sys.exc_info().
> -        """
> -
> -        message = str(err[1])
> -        output = self._exc_info_to_string(err, test)
> -
> -        if err is not None:
> -            if issubclass(err[0], ElbeTestException):
> -                self.current_case.stdout = err[1].out
> -
> -        self.current_case.add_error_info(message, output)
> -        super().addError(test, err)
> -
> -    def addFailure(self, test, err):
> -        """Called when an error has occurred. 'err' is a tuple of values as
> -           returned by sys.exc_info()."""
> -
> -        message = str(err[1])
> -        output = self._exc_info_to_string(err, test)
> -
> -        if err is not None:
> -            if issubclass(err[0], ElbeTestException):
> -                self.current_case.stdout = err[1].out
> -
> -        self.current_case.add_failure_info(message, output)
> -        super().addFailure(test, err)
> -
> -    def addSubTest(self, test, subtest, err):
> -        """Called at the end of a subtest.
> -           'err' is None if the subtest ended successfully, otherwise it's a
> -           tuple of values as returned by sys.exc_info().
> -        """
> -
> -        self.current_case = junit.TestCase(name=str(subtest))
> -        self.cases.append(self.current_case)
> -
> -        if err is not None:
> -            message = str(err[1])
> -            output = self._exc_info_to_string(err, test)
> -
> -            if issubclass(err[0], ElbeTestException):
> -                self.current_case.stdout = err[1].out
> -
> -            if issubclass(err[0], test.failureException):
> -                self.current_case.add_failure_info(message, output)
> -            else:
> -                self.current_case.add_error_info(message, output)
> -
> -        super().addSubTest(test, subtest, err)
> -
> -    def addSkip(self, test, reason):
> -        """Called when a test is skipped."""
> -        self.current_case.add_skipped_info(message=reason)
> -        if test.stdout is not None:
> -            self.current_case.stdout = test.stdout
> -        super().addSkip(test, reason)
> +    def wasSuccessful(self):
> +        return self.success
>  
>      def get_xml(self):
> -        ts = junit.TestSuite(name="test", test_cases=self.cases)
> -
>          with warnings.catch_warnings():
>              warnings.simplefilter("ignore")
> -            results = junit.TestSuite.to_xml_string([ts], encoding="utf-8")
> +            results = self.buffer.getvalue()
>  
>          return results
>  
> @@ -271,9 +216,7 @@ def run_command(argv):
>  
>      result = ElbeTestResult()
>  
> -    for test in suite:
> -        print(test)
> -        test.run(result)
> +    result.run_testsuite(suite)
>  
>      if opt.output is None:
>          print(result.get_xml())
> @@ -282,4 +225,5 @@ def run_command(argv):
>              f.write(result.get_xml())
>  
>      if not result.wasSuccessful():
> +        print("Testsuite failed.")
>          os.sys.exit(77)

-- 
Eduard Krein
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 19; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy
can be found here): https://linutronix.de/legal/data-protection.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen |
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700
806 | Geschäftsführer (Managing Directors): Heinz Egger,
Thomas Gleixner, Sharon Heck, Yulia Beck, Tiffany Silva


More information about the elbe-devel mailing list