diff --git a/xctestrunner/shared/ios_constants.py b/xctestrunner/shared/ios_constants.py index 2136b1c..54403eb 100644 --- a/xctestrunner/shared/ios_constants.py +++ b/xctestrunner/shared/ios_constants.py @@ -67,6 +67,9 @@ def enum(**enums): In xctest, the functionality is the same as "args". In xcuitest, the process of app under test is different with the process of test. + keep_xcresult_data: bool + Whether or not to keep the xcresult bundle produced by the test run + in the output_dir. tests_to_run : array The specific test classes or test methods to run. Each item should be string and its format is Test-Class-Name[/Test-Method-Name]. It is supported diff --git a/xctestrunner/test_runner/xctest_session.py b/xctestrunner/test_runner/xctest_session.py index a767a6f..05d6d4c 100644 --- a/xctestrunner/test_runner/xctest_session.py +++ b/xctestrunner/test_runner/xctest_session.py @@ -14,7 +14,6 @@ """The module to run XCTEST based tests.""" -import glob import logging import os import shutil @@ -60,6 +59,7 @@ def __init__(self, sdk, device_arch, work_dir=None, output_dir=None): self._destination_timeout_sec = None self._xctestrun_obj = None self._prepared = False + self._keep_xcresult_data = True # The following fields are only for Logic Test. self._logic_test_bundle = None self._logic_test_env_vars = None @@ -160,6 +160,7 @@ def SetLaunchOptions(self, launch_options): 'XctestSession.Prepare first.') if not launch_options: return + self._keep_xcresult_data = launch_options.get('keep_xcresult_data', True) self._startup_timeout_sec = launch_options.get('startup_timeout_sec') self._destination_timeout_sec = launch_options.get( 'destination_timeout_sec') @@ -205,18 +206,18 @@ def RunTest(self, device_id, os_version=None): 'XctestSession.Prepare first.') if self._xctestrun_obj: + result_bundle_path = os.path.join(self._output_dir, 'test.xcresult') exit_code = self._xctestrun_obj.Run(device_id, self._sdk, self._output_dir, self._startup_timeout_sec, self._destination_timeout_sec, - os_version=os_version) + os_version=os_version, + result_bundle_path=result_bundle_path) # The xcresult only contains raw data in Xcode 11 or later. if xcode_info_util.GetXcodeVersionNumber() >= 1100: - test_log_dir = '%s/Logs/Test' % self._output_dir - xcresults = glob.glob('%s/*.xcresult' % test_log_dir) - for xcresult in xcresults: - xcresult_util.ExposeDiagnosticsRef(xcresult, test_log_dir) - shutil.rmtree(xcresult) + xcresult_util.ExposeDiagnosticsRef(result_bundle_path, test_log_dir) + if not self._keep_xcresult_data: + shutil.rmtree(result_bundle_path) return exit_code elif self._logic_test_bundle: return logic_test_util.RunLogicTestOnSim( diff --git a/xctestrunner/test_runner/xctestrun.py b/xctestrunner/test_runner/xctestrun.py index 27183a1..6750144 100644 --- a/xctestrunner/test_runner/xctestrun.py +++ b/xctestrunner/test_runner/xctestrun.py @@ -142,7 +142,8 @@ def SetSkipTests(self, skip_tests): self.SetXctestrunField('SkipTestIdentifiers', skip_tests) def Run(self, device_id, sdk, derived_data_dir, startup_timeout_sec, - destination_timeout_sec=None, os_version=None): + destination_timeout_sec=None, os_version=None, + result_bundle_path=None): """Runs the test with generated xctestrun file in the specific device. Args: @@ -153,6 +154,7 @@ def Run(self, device_id, sdk, derived_data_dir, startup_timeout_sec, destination_timeout_sec: Wait for the given seconds while searching for the destination device. os_version: os version of the device. + result_bundle_path: path to output a xcresult bundle to Returns: A value of type runner_exit_codes.EXITCODE. @@ -161,7 +163,8 @@ def Run(self, device_id, sdk, derived_data_dir, startup_timeout_sec, # later, it is required to add swift5 fallback libraries to environment # variable. # See https://github.com/bazelbuild/rules_apple/issues/684 for context. - if (xcode_info_util.GetXcodeVersionNumber() >= 1100 and + xcode_version = xcode_info_util.GetXcodeVersionNumber() + if (xcode_version >= 1100 and sdk == ios_constants.SDK.IPHONESIMULATOR and os_version and version_util.GetVersionNumber(os_version) < 1220): new_env_var = { @@ -174,6 +177,11 @@ def Run(self, device_id, sdk, derived_data_dir, startup_timeout_sec, '-xctestrun', self._xctestrun_file_path, '-destination', 'id=%s' % device_id, '-derivedDataPath', derived_data_dir] + + if xcode_version >= 1100 and result_bundle_path: + shutil.rmtree(result_bundle_path, ignore_errors=True) + command.extend(['-resultBundlePath', result_bundle_path]) + if destination_timeout_sec: command.extend(['-destination-timeout', str(destination_timeout_sec)]) exit_code, _ = xcodebuild_test_executor.XcodebuildTestExecutor(