def RunLogicTestOnSim(sim_id, test_bundle_path, env_vars=None, args=None, tests_to_run=None, os_version=None): """Runs logic tests on the simulator. The output prints on system stdout. Args: sim_id: string, the id of the simulator. test_bundle_path: string, the path of the logic test bundle. env_vars: dict, the additionl environment variables passing to test's process. args: array, the additional arguments passing to test's process. tests_to_run: array, the format of each item is TestClass[/TestMethod]. If it is empty, then runs with All methods. os_version: string, the OS version of the simulator. Returns: exit_code: A value of type runner_exit_codes.EXITCODE. Raises: ios_errors.SimError: The command to launch logic test has error. """ simctl_env_vars = {} if env_vars: for key in env_vars: simctl_env_vars[_SIMCTL_ENV_VAR_PREFIX + key] = env_vars[key] simctl_env_vars['NSUnbufferedIO'] = 'YES' # When running tests on iOS 12.1 or earlier simulator under Xcode 11 or 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 os_version and version_util.GetVersionNumber(os_version) < 1220): key = _SIMCTL_ENV_VAR_PREFIX + 'DYLD_FALLBACK_LIBRARY_PATH' simctl_env_vars[key] = xcode_info_util.GetSwift5FallbackLibsDir() # We need to set the DEVELOPER_DIR to ensure xcrun works correctly developer_dir = os.environ.get('DEVELOPER_DIR') if developer_dir: simctl_env_vars['DEVELOPER_DIR'] = developer_dir command = [ 'xcrun', 'simctl', 'spawn', '-s', sim_id, xcode_info_util.GetXctestToolPath(ios_constants.SDK.IPHONESIMULATOR) ] if args: command += args if not tests_to_run: tests_to_run_str = 'All' else: tests_to_run_str = ','.join(tests_to_run) return_code = subprocess.Popen( command + ['-XCTest', tests_to_run_str, test_bundle_path], env=simctl_env_vars, stdout=sys.stdout, stderr=subprocess.STDOUT).wait() if return_code != 0: return runner_exit_codes.EXITCODE.FAILED return runner_exit_codes.EXITCODE.SUCCEEDED
def Run(self, device_id, sdk, derived_data_dir, startup_timeout_sec, destination_timeout_sec=None, os_version=None): """Runs the test with generated xctestrun file in the specific device. Args: device_id: ID of the device. sdk: shared.ios_constants.SDK, sdk of the device. derived_data_dir: path of derived data directory of this test session. startup_timeout_sec: seconds until the xcodebuild command is deemed stuck. destination_timeout_sec: Wait for the given seconds while searching for the destination device. os_version: os version of the device. Returns: A value of type runner_exit_codes.EXITCODE. """ # When running tests on iOS 12.1 or earlier simulator under Xcode 11 or # 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 sdk == ios_constants.SDK.IPHONESIMULATOR and os_version and version_util.GetVersionNumber(os_version) < 1220): new_env_var = { 'DYLD_FALLBACK_LIBRARY_PATH': xcode_info_util.GetSwift5FallbackLibsDir() } self.SetTestEnvVars(new_env_var) logging.info('Running test-without-building with device %s', device_id) command = [ 'xcodebuild', 'test-without-building', '-xctestrun', self._xctestrun_file_path, '-destination', 'id=%s' % device_id, '-derivedDataPath', derived_data_dir ] if destination_timeout_sec: command.extend( ['-destination-timeout', str(destination_timeout_sec)]) exit_code, _ = xcodebuild_test_executor.XcodebuildTestExecutor( command, succeeded_signal=_SIGNAL_TEST_WITHOUT_BUILDING_SUCCEEDED, failed_signal=_SIGNAL_TEST_WITHOUT_BUILDING_FAILED, sdk=sdk, test_type=self.test_type, device_id=device_id, app_bundle_id=self._aut_bundle_id, startup_timeout_sec=startup_timeout_sec).Execute( return_output=False) return exit_code
def GetXcodeVersionNumber(): """Gets the Xcode version number. E.g. if xcode version is 8.2.1, the xcode version number is 821. Returns: integer, xcode version number. """ global _xcode_version_number if _xcode_version_number is not None: return _xcode_version_number # Example output: # Xcode 8.2.1 # Build version 8C1002 output = subprocess.check_output(('xcodebuild', '-version')) xcode_version = output.split('\n')[0].split(' ')[1] # Add cache xcode_version_number to avoid calling subprocess multiple times. # It is expected that no one changes xcode during the test runner working. _xcode_version_number = version_util.GetVersionNumber(xcode_version) return _xcode_version_number