diff mbox series

[03/14] lava_runner: use TextTestResult

Message ID 20230313094431.507952-4-tzungbi@kernel.org (mailing list archive)
State Handled Elsewhere
Headers show
Series cros-ec-tests: fix some exceptions and clean-ups | expand

Commit Message

Tzung-Bi Shih March 13, 2023, 9:44 a.m. UTC
To simplify, make LavaTestResult is-a TextTestResult.

As a side-effect result, it prints errors and fails by the end of test
running.  See [1].

[1]: https://github.com/python/cpython/blob/main/Lib/unittest/runner.py#L139

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 cros/runners/lava_runner.py | 44 +++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 24 deletions(-)
diff mbox series

Patch

diff --git a/cros/runners/lava_runner.py b/cros/runners/lava_runner.py
index 7524f65dcd39..70f8aa61132e 100755
--- a/cros/runners/lava_runner.py
+++ b/cros/runners/lava_runner.py
@@ -13,38 +13,34 @@  from cros.tests.cros_ec_power import *
 from cros.tests.cros_ec_extcon import *
 
 
-class LavaTestResult(unittest.TestResult):
-    def __init__(self, stream, descriptions, verbosity):
-        unittest.TestResult.__init__(self)
-        self.stream = stream
+class LavaTestResult(unittest.TextTestResult):
+    def writeLavaSignal(self, test, result):
+        test_case_id = test.id().rsplit(".")[-1]
 
-    def addSuccess(self, test):
-        unittest.TestResult.addSuccess(self, test)
-        self.stream.write(
-            "<LAVA_SIGNAL_TESTCASE TEST_CASE_ID=%s RESULT=pass>\n"
-            % test.id().rsplit(".")[-1]
+        # LAVA signal must be start-of-line.  Print a newline if verbosity >= 1.
+        if self.showAll or self.dots:
+            self.stream.writeln()
+
+        self.stream.writeln(
+            f"<LAVA_SIGNAL_TESTCASE TEST_CASE_ID={test_case_id} RESULT={result}>"
         )
+        self.stream.flush()
+
+    def addSuccess(self, test):
+        super().addSuccess(test)
+        self.writeLavaSignal(test, "pass")
 
     def addError(self, test, err):
-        unittest.TestResult.addError(self, test, err)
-        self.stream.write(
-            "<LAVA_SIGNAL_TESTCASE TEST_CASE_ID=%s RESULT=unknown>\n"
-            % test.id().rsplit(".")[-1]
-        )
+        super().addError(test, err)
+        self.writeLavaSignal(test, "unknown")
 
     def addFailure(self, test, err):
-        unittest.TestResult.addFailure(self, test, err)
-        self.stream.write(
-            "<LAVA_SIGNAL_TESTCASE TEST_CASE_ID=%s RESULT=fail>\n"
-            % test.id().rsplit(".")[-1]
-        )
+        super().addFailure(test, err)
+        self.writeLavaSignal(test, "fail")
 
     def addSkip(self, test, reason):
-        unittest.TestResult.addSkip(self, test, reason)
-        self.stream.write(
-            "<LAVA_SIGNAL_TESTCASE TEST_CASE_ID=%s RESULT=skip>\n"
-            % test.id().rsplit(".")[-1]
-        )
+        super().addSkip(test, reason)
+        self.writeLavaSignal(test, "skip")
 
 
 if __name__ == "__main__":