Bug 1473313 - Part 2: Support running emulator junit tests with code coverage support. r=gbrown draft
authorTudor-Gabriel Vîjială <tvijiala@mozilla.com>
Tue, 24 Jul 2018 11:49:13 +0100
changeset 825869 b2e4f3c1c4d2313c3108ecdedb86bfbad1f02fb7
parent 825868 9edc37913a3929ad045270c601c77791d122e363
child 825870 4d6d0b55435905933e452e03f649bb3382efee62
child 825921 929e1e3ab1a0a8f30c45ddc48a5b2e1642a42f6a
push id118194
push userbmo:tvijiala@mozilla.com
push dateThu, 02 Aug 2018 15:08:25 +0000
reviewersgbrown
bugs1473313
milestone63.0a1
Bug 1473313 - Part 2: Support running emulator junit tests with code coverage support. r=gbrown MozReview-Commit-ID: MIZm9SEdlI
testing/mochitest/runjunit.py
--- a/testing/mochitest/runjunit.py
+++ b/testing/mochitest/runjunit.py
@@ -46,16 +46,21 @@ class JUnitTestRunner(MochitestDesktop):
         self.device = ADBAndroid(adb=options.adbPath or 'adb',
                                  device=options.deviceSerial,
                                  test_root=options.remoteTestRoot,
                                  verbose=verbose)
         self.options = options
         self.log.debug("options=%s" % vars(options))
         update_mozinfo()
         self.remote_profile = posixpath.join(self.device.test_root, 'junit-profile')
+
+        if self.options.coverage and not self.options.coverage_output_path:
+            raise Exception("--coverage-output-path is required when using --enable-coverage")
+        self.remote_coverage_output_path = posixpath.join(self.device.test_root,
+                                                          'junit-coverage.ec')
         self.server_init()
 
         self.cleanup()
         self.device.clear_logcat()
         self.build_profile()
         self.startServers(
             self.options,
             debuggerInfo=None,
@@ -140,16 +145,20 @@ class JUnitTestRunner(MochitestDesktop):
         shard = self.options.thisChunk
         if shards is not None and shard is not None:
             shard -= 1  # shard index is 0 based
             cmd = cmd + " -e numShards %d -e shardIndex %d" % (shards, shard)
         # test filters: limit run to specific test(s)
         for f in test_filters:
             # filter can be class-name or 'class-name#method-name' (single test)
             cmd = cmd + " -e class %s" % f
+        # enable code coverage reports
+        if self.options.coverage:
+            cmd = cmd + " -e coverage true"
+            cmd = cmd + " -e coverageFile %s" % self.remote_coverage_output_path
         # environment
         env = {}
         env["MOZ_CRASHREPORTER"] = "1"
         env["MOZ_CRASHREPORTER_NO_REPORT"] = "1"
         env["MOZ_CRASHREPORTER_SHUTDOWN"] = "1"
         env["XPCOM_DEBUG_BREAK"] = "stack"
         env["DISABLE_UNSAFE_CPOW_WARNINGS"] = "1"
         env["MOZ_DISABLE_NONLOCAL_CONNECTIONS"] = "1"
@@ -257,16 +266,19 @@ class JUnitTestRunner(MochitestDesktop):
             self.log.info("Failed: %d" % self.fail_count)
             self.log.info("Todo: %d" % self.todo_count)
         finally:
             self.log.suite_end()
 
         if self.check_for_crashes():
             self.fail_count = 1
 
+        if self.options.coverage:
+            self.device.pull(self.remote_coverage_output_path, self.options.coverage_output_path)
+
         return 1 if self.fail_count else 0
 
     def check_for_crashes(self):
         logcat = self.device.get_logcat()
         if logcat:
             if mozcrash.check_for_java_exception(logcat, self.current_full_name):
                 return True
         symbols_path = self.options.symbolsPath
@@ -359,16 +371,27 @@ class JunitArgumentParser(argparse.Argum
                           default=None,
                           help="Total number of chunks to split tests into.")
         self.add_argument("--this-chunk",
                           action="store",
                           type=int,
                           dest="thisChunk",
                           default=None,
                           help="If running tests by chunks, the chunk number to run.")
+        self.add_argument("--enable-coverage",
+                          action="store_true",
+                          dest="coverage",
+                          default=False,
+                          help="Enable code coverage collection.")
+        self.add_argument("--coverage-output-path",
+                          action="store",
+                          type=str,
+                          dest="coverage_output_path",
+                          default=None,
+                          help="If collecting code coverage, save the report file to this path.")
         # Additional options for server.
         self.add_argument("--certificate-path",
                           action="store",
                           type=str,
                           dest="certPath",
                           default=None,
                           help="Path to directory containing certificate store."),
         self.add_argument("--http-port",