[elbe-devel] [PATCH 03/11] commands: test: Add elbe-ci coverage

Olivier Dion dion at linutronix.de
Mon Aug 17 18:20:12 CEST 2020


Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/commands/test.py | 92 ++++++++++++++++++++-------------------
 1 file changed, 48 insertions(+), 44 deletions(-)

diff --git a/elbepack/commands/test.py b/elbepack/commands/test.py
index 2c84e2b3..e2dcd763 100644
--- a/elbepack/commands/test.py
+++ b/elbepack/commands/test.py
@@ -14,6 +14,8 @@ import warnings
 
 import junit_xml as junit
 
+from elbepack.cov import ElbeCoverage
+
 class ElbeTestLevel(enum.IntEnum):
     BASE   = enum.auto()
     EXTEND = enum.auto()
@@ -128,7 +130,7 @@ def run_command(argv):
 
     oparser.add_option("-d", "--dry-run", dest="dry_run",
                        action="store_true", default=False,
-                       help="List tests that would have been executed and exit")
+                       help="List tests that would have been executed and exit (implies -c)")
 
     oparser.add_option("-p", "--parallel", dest="parallel",
                        type="string", default="0,1",
@@ -146,65 +148,67 @@ def run_command(argv):
               (opt.level, ", ".join(key for key in ElbeTestLevel.__members__)))
         os.sys.exit(20)
 
-    ElbeTestCase.level = ElbeTestLevel[opt.level]
+    with ElbeCoverage(en_coverage=True, report="coverage-report.txt"):
+
+        ElbeTestCase.level = ElbeTestLevel[opt.level]
 
-    # Find all tests
-    loader            = unittest.defaultTestLoader
-    loader.suiteClass = ElbeTestSuite
-    suite             = loader.discover(top_dir)
+        # Find all tests
+        loader            = unittest.defaultTestLoader
+        loader.suiteClass = ElbeTestSuite
+        suite             = loader.discover(top_dir)
 
-    # then filter them
-    suite.filter_test(opt.parallel, opt.filter, opt.invert_re)
+        # then filter them
+        suite.filter_test(opt.parallel, opt.filter, opt.invert_re)
 
-    # Dry run? Just exit gently
-    if opt.dry_run:
-        suite.ls()
-        print("======================================================================\n"
-              "This was a dry run. No tests were executed")
-        os.sys.exit(0)
+        # Dry run? Just exit gently
+        if opt.dry_run:
+            suite.ls()
+            print("======================================================================\n"
+                  "This was a dry run. No tests were executed")
+            os.sys.exit(0)
 
-    cases = []
+        cases = []
 
-    err_cnt  = 0
-    fail_cnt = 0
+        err_cnt  = 0
+        fail_cnt = 0
 
-    for test in suite:
+        for test in suite:
 
-        print(test)
+            print(test)
 
-        result = unittest.TestResult()
+            result = unittest.TestResult()
 
-        test.run(result)
+            test.run(result)
 
-        case = junit.TestCase(name=str(test))
+            case = junit.TestCase(name=str(test))
 
-        for error in result.errors:
-            case.add_error_info(message=error[1])
-            err_cnt += 1
+            for error in result.errors:
+                case.add_error_info(message=error[1])
+                err_cnt += 1
 
-        for failure in result.failures:
-            case.add_failure_info(message=failure[1])
-            fail_cnt += 1
+            for failure in result.failures:
+                case.add_failure_info(message=failure[1])
+                fail_cnt += 1
 
-        for us in result.unexpectedSuccesses:
-            case.add_failure_info(message=us)
-            err_cnt += 1
+            for us in result.unexpectedSuccesses:
+                case.add_failure_info(message=us)
+                err_cnt += 1
 
-        for skip in result.skipped:
-            case.add_skipped_info(message=skip[1])
+            for skip in result.skipped:
+                case.add_skipped_info(message=skip[1])
 
-        cases.append(case)
+            cases.append(case)
 
-    ts = junit.TestSuite(name="test", test_cases=cases)
+        ts = junit.TestSuite(name="test", test_cases=cases)
 
-    with warnings.catch_warnings():
-        warnings.simplefilter("ignore")
-        results = junit.TestSuite.to_xml_string([ts], encoding="utf-8")
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            results = junit.TestSuite.to_xml_string([ts], encoding="utf-8")
 
-    if opt.output is None:
-        print(results)
-    else:
-        with open(opt.output, "w") as f:
-            f.write(results)
+        if opt.output is None:
+            print(results)
+        else:
+            with open(opt.output, "w") as f:
+                f.write(results)
 
-    os.sys.exit(err_cnt | fail_cnt)
+        os.sys.exit(err_cnt | fail_cnt)
-- 
2.28.0



More information about the elbe-devel mailing list