def set_bp(self, port=None): self.bp = port or self.bp if not self.platform: raise ArgsError('platform not set') if not self.bp: raise ArgsError('bp must be not None') if self.platform == 'ios': self.device['wdaLocalPort'] = '{}'.format(self.bp) self.args.append('--webdriveragent-port') else: # self.device['systemPort'] = '{}'.format(self.bp) # self.device['systemPort'] = '{}'.format(system_port or get_free_port(8200)[0][0]) self.args.append('--bootstrap-port') self.args.append('{}'.format(self.bp)) return self
def set_port(self, port=None): self.port = port or self.port if not self.port: raise ArgsError('port must be not None') self.args.append('-p') self.args.append('{}'.format(self.port)) return self
def run(method, args, amount=1): pool = None try: if len(args) <= 1: method(*args[0]) else: if platform.system().lower() != "windows": mp.set_start_method('spawn', force=True) cores = mp.cpu_count() perc = psutil.cpu_percent() used = int(perc * cores / 100) + 1 left = cores - used - 1 if left <= 0: raise ResourceWarning( f"Not enough core for test: {used} used, {cores} in total") amount = amount if amount < left else left pool = mp.Pool(amount) for a in args: # print(f"a: {a}") if not isinstance(a, (list, tuple)): raise ArgsError("invalid args, expected list or tuple") pool.apply_async(method, a) # 固定并发限制amount,异步进入, 须len(args) >= amount time.sleep(0.5) # pool.map(method, args) # 固定并发,需要结合partial pool.close() pool.join() except KeyboardInterrupt: if pool: pool.close() pool.join() else: os._exit(1)
def get_tests(self): if appOptions.tests_dir in self.sys_argv and \ self.option_args.tests_dir != self.tests_dir: self.tests_dir = self.option_args.tests_dir self.default_device = self.get_default_device() device = self.option_args.device test = self.option_args.test if device or test: device = self.get_device_name(device) if not test: if device in self.tests: self.tests = {device: self.tests[device]} else: test = self.get_default_test(device) self.tests = {device: {'test module': [test]}} else: self.tests = {device, {'test module': [test]}} for d in self.tests: if not self.tests[d].get('test module'): test = self.get_default_test(d) self.tests[d]['test module'] = [test] if not self.tests: print('No tests set, using default configuration') if not self.default_device: raise ArgsError('No default device set') test = self.get_default_test(self.default_device) self.tests = {self.default_device, {'test module': [test]}} self.devices = self.tests.keys() return self
def add_udid(self, udid=None): self.udid = udid or self.udid if not self.udid: raise ArgsError('udid must be set') self.device['udid'] = self.udid self.args.append('-U') self.args.append('{}'.format(self.udid)) return self
def get_device_name(self, d): if str(d).lower() not in \ ['ios', 'android', 'browser', 'windows', 'mac']: d = self.device_config[d.lower().capitalize()] else: d = d or self.default_device if d: if d.lower() in \ ['ios', 'android', 'browser', 'windows', 'mac']: d = self.device_config[d.lower().capitalize()] if not d: raise ArgsError('No device set to test on! ' 'set Default device in `device.yml` at least') return d
def set_result(self): if appOptions.separate in self.sys_argv and \ self.option_args.separate != self.separate: self.separate = self.option_args.separate if appOptions.output_dir in self.sys_argv and \ self.option_args.output_dir != self.report_dir: self.report_dir = self.option_args.output_dir self.junitxml = any( str(x).startswith('--junit-xml') for x in self.pytest_args) if (self.junitxml or '--alluredir' in self.pytest_args) and len(self.devices) > 1: raise ArgsError( 'Should not set `--alluredir` or `--junit-xml` when more than one devices' ) for d in self.tests: if self.separate: report_dir = Path(self.report_dir) / Path(d) / Path( self.test_time) else: report_dir = Path(self.report_dir) / Path(d) self.tests[d]['output_dir'] = str(report_dir) self.tests[d]['log_file'] = str( report_dir / Path("{}_{}.log".format(d, self.test_time))) device_info = self.device_config[d] version = device_info['caps'].get( 'platformVersion') or device_info['caps'].get('version') self.tests[d]['allure_report_dir'] = str(report_dir / Path('allure-report')) if '--alluredir' not in self.pytest_args: self.tests[d]['allure_data_dir'] = str(report_dir / Path('allure-data')) if not self.junitxml: xml_dir = report_dir / Path('junit-xml') self.tests[d]['xml_dir'] = str(xml_dir) self.tests[d]['xml'] = str( xml_dir / Path("{}_{}_{}.xml".format(d, version, self.test_time))) return self
def set_platform(self, device_platform=None): self.platform = device_platform or self.platform if not self.platform: raise ArgsError('platform must be set') return self
def set_timestamp(self, ts=None): self.log_time = ts or self.log_time if not self.log_time: raise ArgsError('timestamp must be set') return self