def main(): # pylint: disable=R0912,R0915 # R0912 Too many branches (17/12) # R0915 Too many statements (57/50) config = Configuration() if config.version: print "behave " + __version__ sys.exit(0) if config.tags_help: print TAG_HELP sys.exit(0) if config.lang_list: iso_codes = languages.keys() iso_codes.sort() print "Languages available:" for iso_code in iso_codes: native = languages[iso_code]["native"][0] name = languages[iso_code]["name"][0] print u"%s: %s / %s" % (iso_code, native, name) sys.exit(0) if config.lang_help: if config.lang_help not in languages: sys.exit("%s is not a recognised language: try --lang-list" % config.lang_help) trans = languages[config.lang_help] print u"Translations for %s / %s" % (trans["name"][0], trans["native"][0]) for kw in trans: if kw in "name native".split(): continue print u"%16s: %s" % (kw.title().replace("_", " "), u", ".join(w for w in trans[kw] if w != "*")) sys.exit(0) if not config.format: format0 = config.defaults["format0"] config.format = [format0] elif "help" in config.format: print "Available formatters:" formatters.list_formatters(sys.stdout) sys.exit(0) # -- SANITY: Use at most one formatter, more cause various problems. # PROBLEM DESCRIPTION: # 1. APPEND MODE: configfile.format + --format # 2. Daisy chaining of formatters does not work # => behave.formatter.formatters.get_formatter() # => Stream methods, stream.write(), stream.flush are missing # in Formatter interface config.format = config.format[-1:] stream = config.output runner = Runner(config) try: failed = runner.run() except ParserError, e: sys.exit(str(e))
def main(): config = Configuration() if config.version: print "behave " + __version__ sys.exit(0) if config.tags_help: print TAG_HELP sys.exit(0) if config.lang_list: iso_codes = languages.keys() iso_codes.sort() print "Languages available:" for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print u'%s: %s / %s' % (iso_code, native, name) sys.exit(0) if config.lang_help: if config.lang_help not in languages: sys.exit('%s is not a recognised language: try --lang-list' % config.lang_help) trans = languages[config.lang_help] print u"Translations for %s / %s" % (trans['name'][0], trans['native'][0]) for kw in trans: if kw in 'name native'.split(): continue print u'%16s: %s' % (kw.title().replace('_', ' '), u', '.join(w for w in trans[kw] if w != '*')) sys.exit(0) if not config.format: format0 = config.defaults["format0"] config.format = [ format0 ] elif 'help' in config.format: print "Available formatters:" formatters.list_formatters(sys.stdout) sys.exit(0) # -- SANITY: Use at most one formatter, more cause various problems. # PROBLEM DESCRIPTION: # 1. APPEND MODE: configfile.format + --format # 2. Daisy chaining of formatter does not work # => behave.formatter.formatters.get_formatter() # => Stream methods, stream.write(), stream.flush are missing # in Formatter interface if DISABLE_MULTI_FORMATTERS: config.format = config.format[-1:] runner = Runner(config) try: failed = runner.run() except ParserError, e: sys.exit(str(e))
def main(): config = Configuration() if config.version: print "behave " + __version__ sys.exit(0) if config.tags_help: print TAG_HELP sys.exit(0) if config.lang_list: iso_codes = languages.keys() iso_codes.sort() print "Languages available:" for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print u'%s: %s / %s' % (iso_code, native, name) sys.exit(0) if config.lang_help: if config.lang_help not in languages: sys.exit('%s is not a recognised language: try --lang-list' % config.lang_help) trans = languages[config.lang_help] print u"Translations for %s / %s" % (trans['name'][0], trans['native'][0]) for kw in trans: if kw in 'name native'.split(): continue print u'%16s: %s' % (kw.title().replace('_', ' '), u', '.join(w for w in trans[kw] if w != '*')) sys.exit(0) if not config.format: default_format = config.defaults["default_format"] config.format = [ default_format ] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. default_format = config.defaults["default_format"] config.format.append(default_format) if 'help' in config.format: print "Available formatters:" formatters.list_formatters(sys.stdout) sys.exit(0) if len(config.outputs) > len(config.format): print 'CONFIG-ERROR: More outfiles (%d) than formatters (%d).' % \ (len(config.outputs), len(config.format)) sys.exit(1) runner = Runner(config) try: failed = runner.run() except ParserError, e: sys.exit("ParseError: %s" % e)
def main(): config = Configuration() if config.version: print "behave " + __version__ sys.exit(0) if config.tags_help: print TAG_HELP sys.exit(0) if config.lang_list: iso_codes = languages.keys() iso_codes.sort() print "Languages available:" for iso_code in iso_codes: native = languages[iso_code]["native"][0] name = languages[iso_code]["name"][0] print u"%s: %s / %s" % (iso_code, native, name) sys.exit(0) if config.lang_help: if config.lang_help not in languages: sys.exit("%s is not a recognised language: try --lang-list" % config.lang_help) trans = languages[config.lang_help] print u"Translations for %s / %s" % (trans["name"][0], trans["native"][0]) for kw in trans: if kw in "name native".split(): continue print u"%16s: %s" % (kw.title().replace("_", " "), u", ".join(w for w in trans[kw] if w != "*")) sys.exit(0) if not config.format: default_format = config.defaults["default_format"] config.format = [default_format] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. default_format = config.defaults["default_format"] config.format.append(default_format) if "help" in config.format: print "Available formatters:" formatters.list_formatters(sys.stdout) sys.exit(0) if len(config.outputs) > len(config.format): print "CONFIG-ERROR: More outfiles (%d) than formatters (%d)." % (len(config.outputs), len(config.format)) sys.exit(1) runner = Runner(config) try: failed = runner.run() except ParserError, e: sys.exit("ParseError: %s" % e)
def main(): config = Configuration() if config.version: print "behave " + __version__ sys.exit(0) if config.tags_help: print TAG_HELP sys.exit(0) if config.lang_list: iso_codes = languages.keys() iso_codes.sort() print "Languages available:" for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print u'%s: %s / %s' % (iso_code, native, name) sys.exit(0) if config.lang_help: if config.lang_help not in languages: sys.exit('%s is not a recognised language: try --lang-list' % config.lang_help) trans = languages[config.lang_help] print u"Translations for %s / %s" % (trans['name'][0], trans['native'][0]) for kw in trans: if kw in 'name native'.split(): continue print u'%16s: %s' % (kw.title().replace('_', ' '), u', '.join(w for w in trans[kw] if w != '*')) sys.exit(0) if not config.format: config.format = ['pretty'] elif 'help' in config.format: print "Available formatters:" formatters.list_formatters(sys.stdout) sys.exit(0) stream = config.output runner = Runner(config) try: failed = runner.run() except ParserError, e: sys.exit(str(e))
def feature_new(request): print(request.POST) form = json.loads(request.body.decode('utf-8')) feature = Feature(description=form['description'], finality=form['finality'], who=form['who'], purpose=form['purpose'], project_id=form['project']) feature.save() scenario = Scenario(given=form['given'], when=form['when'], then=form['then'], title=form['title'], feature=feature) scenario.save() gen_feature_file(feature.id) conf = Configuration('media/features/{}.feature'.format(feature.id)) conf.format = [ conf.default_format ] runner = Runner(conf) runner.run() filename = make_test_funcs(runner.undefined_steps, feature.id) add_to_repo(filename, feature.description) return HttpResponse()
def home(request): """Renders the home page.""" assert isinstance(request, HttpRequest) if request.method != 'POST': form = FeatureForm() return render(request, 'app/index.html', context_instance=RequestContext( request, { 'title': 'Home Page', 'year': datetime.now().year, 'form': form, })) else: form = FeatureForm(request.POST) if form.is_valid(): feature = Feature(description=form.cleaned_data['description'], finality=form.cleaned_data['finality'], who=form.cleaned_data['who'], purpose=form.cleaned_data['purpose']) feature.save() scenario = Scenario(given=form.cleaned_data['given'], when=form.cleaned_data['when'], then=form.cleaned_data['then'], title=form.cleaned_data['title'], feature=feature) scenario.save() gen_feature_file(feature.id) conf = Configuration( os.path.join(settings.PROJECT_ROOT, 'media', 'features', '{}.feature'.format(feature.id))) conf.format = [conf.default_format] runner = Runner(conf) runner.run() filename = make_test_funcs(runner.undefined_steps, feature.id) add_to_repo(filename, feature.description) return render(request, 'app/index.html', context_instance=RequestContext( request, { 'title': 'Home Page', 'year': datetime.now().year, 'form': form, }))
def get_available_steps(step_dirs=None): """ gets all the steps that the core is aware of. useful for doing real-time 'syntax' checking for uis you dont need to call goh_behave prior :param step_dirs: optional. list of dirs to additionally load steps from :return: a list of found steps' text :rtype: list[string] """ # setup config for behave's runner # use dry run so only steps are evaluated, nothing is run config = Configuration([u'--dry-run']) # i dont want std output about the running test, because there is no test # whoa... config.format = [u'null'] # append steps from the bdd core if not step_dirs: step_dirs = [] # no features, we just want the implemented steps feature_dirs = [] runner = HackedRunner(config, feature_dirs, step_dirs) # this is the key part. this will cause behave to run through all the step # dirs and execute them, which will cause steps to be registered. runner.load_step_definitions() # reach into behave's step registry and serialize the known steps into a list found_steps = [] for steps in step_registry.registry.steps.values(): for step in steps: if step.step_type is None or u'step' == step.step_type: for real_key in RealSteps: found_steps.append(u'{} {}'.format(real_key, step.string)) else: found_steps.append(u'{} {}'.format(step.step_type, step.string)) found_steps.append(u'{} {}'.format(u'And', step.string)) return found_steps
def launch_test(self, path, batch_id): c = Configuration("") if not c.format: c.format = [c.default_format] c.paths = [path] c.userdata['lang_code'] = self.lang_code c.userdata['target_id'] = self.target_id c.userdata['base_url'] = self.target_id.target_id.url c.userdata['batch_id'] = batch_id.id c.userdata['scenario_id'] = self.scenario_id.id c.userdata['scenario_line_id'] = self.scenario_line_id.id c.userdata['empty_batch'] = self.empty_batch c.userdata['browser_vm_url'] = self.env[ 'ir.config_parameter'].get_param('test_builder.vm_url') self.config = c r = Runner(c) r.run() if self.scenario_line_id: return r.feature.status else: return c.userdata.get('run_id', None)
def home(request): """Renders the home page.""" assert isinstance(request, HttpRequest) if request.method != 'POST': form = FeatureForm() return render( request, 'app/index.html', context_instance = RequestContext(request, { 'title':'Home Page', 'year':datetime.now().year, 'form': form, }) ) else: form = FeatureForm(request.POST) if form.is_valid(): feature = Feature(description=form.cleaned_data['description'], finality=form.cleaned_data['finality'], who=form.cleaned_data['who'], purpose=form.cleaned_data['purpose']) feature.save() scenario = Scenario(given=form.cleaned_data['given'], when=form.cleaned_data['when'], then=form.cleaned_data['then'], title=form.cleaned_data['title'], feature=feature) scenario.save() gen_feature_file(feature.id) conf = Configuration(os.path.join(settings.PROJECT_ROOT, 'media', 'features', '{}.feature'.format(feature.id))) conf.format = [ conf.default_format ] runner = Runner(conf) runner.run() filename = make_test_funcs(runner.undefined_steps, feature.id) add_to_repo(filename, feature.description) return render( request, 'app/index.html', context_instance = RequestContext(request, { 'title':'Home Page', 'year':datetime.now().year, 'form': form, }) )
def feature_new(request): print(request.POST) form = json.loads(request.body.decode('utf-8')) feature = Feature(description=form['description'], finality=form['finality'], who=form['who'], purpose=form['purpose'], project_id=form['project']) feature.save() scenario = Scenario(given=form['given'], when=form['when'], then=form['then'], title=form['title'], feature=feature) scenario.save() gen_feature_file(feature.id) conf = Configuration('media/features/{}.feature'.format(feature.id)) conf.format = [conf.default_format] runner = Runner(conf) runner.run() filename = make_test_funcs(runner.undefined_steps, feature.id) add_to_repo(filename, feature.description) return HttpResponse()
def main(args=None): config = Configuration(args) if config.version: print("behave " + __version__) return 0 if config.tags_help: print(TAG_HELP) return 0 if config.lang_list: from behave.i18n import languages iso_codes = languages.keys() iso_codes.sort() print("Languages available:") for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print(u'%s: %s / %s' % (iso_code, native, name)) return 0 if config.lang_help: from behave.i18n import languages if config.lang_help not in languages: print('%s is not a recognised language: try --lang-list' % \ config.lang_help) return 1 trans = languages[config.lang_help] print(u"Translations for %s / %s" % (trans['name'][0], trans['native'][0])) for kw in trans: if kw in 'name native'.split(): continue print(u'%16s: %s' % (kw.title().replace('_', ' '), u', '.join(w for w in trans[kw] if w != '*'))) return 0 if not config.format: config.format = [ config.default_format ] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. config.format.append(config.default_format) if 'help' in config.format: print_formatters("Available formatters:") return 0 if len(config.outputs) > len(config.format): print('CONFIG-ERROR: More outfiles (%d) than formatters (%d).' % \ (len(config.outputs), len(config.format))) return 1 failed = True runner = Runner(config) try: failed = runner.run() except ParserError as e: print(u"ParseError: %s" % e) except ConfigError as e: print(u"ConfigError: %s" % e) except FileNotFoundError as e: print(u"FileNotFoundError: %s" % e) except InvalidFileLocationError as e: print(u"InvalidFileLocationError: %s" % e) except InvalidFilenameError as e: print(u"InvalidFilenameError: %s" % e) except Exception as e: # -- DIAGNOSTICS: text = _text(e) print(u"Exception %s: %s" % (e.__class__.__name__, text)) raise if config.show_snippets and runner.undefined_steps: print_undefined_step_snippets(runner.undefined_steps, colored=config.color) return_code = 0 if failed: return_code = 1 return return_code
def main(args=None): config = Configuration(args) if config.version: print("behave " + __version__) return 0 if config.tags_help: print(TAG_HELP) return 0 if config.lang_list: from behave.i18n import languages iso_codes = languages.keys() iso_codes.sort() print("Languages available:") for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print(u'%s: %s / %s' % (iso_code, native, name)) return 0 if config.lang_help: from behave.i18n import languages if config.lang_help not in languages: print('%s is not a recognised language: try --lang-list' % \ config.lang_help) return 1 trans = languages[config.lang_help] print(u"Translations for %s / %s" % (trans['name'][0], trans['native'][0])) for kw in trans: if kw in 'name native'.split(): continue print(u'%16s: %s' % (kw.title().replace('_', ' '), u', '.join(w for w in trans[kw] if w != '*'))) return 0 if not config.format: config.format = [ config.default_format ] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. config.format.append(config.default_format) if 'help' in config.format: from behave.formatter import formatters print("Available formatters:") formatters.list_formatters(sys.stdout) return 0 if len(config.outputs) > len(config.format): print('CONFIG-ERROR: More outfiles (%d) than formatters (%d).' % \ (len(config.outputs), len(config.format))) return 1 failed = True runner = Runner(config) try: failed = runner.run() except ParserError as e: print("ParseError: %s" % e) except ConfigError as e: print("ConfigError: %s" % e) except FileNotFoundError as e: print("FileNotFoundError: %s" % e) except InvalidFileLocationError as e: print("InvalidFileLocationError: %s" % e) except InvalidFilenameError as e: print("InvalidFilenameError: %s" % e) if config.show_snippets and runner.undefined_steps: print_undefined_step_snippets(runner.undefined_steps, colored=config.color) return_code = 0 if failed: return_code = 1 return return_code
def main(): config = Configuration() if config.version: print("behave " + __version__) sys.exit(0) if config.tags_help: print(TAG_HELP) sys.exit(0) if config.lang_list: iso_codes = list(languages.keys()) iso_codes.sort() print("Languages available:") for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print('%s: %s / %s' % (iso_code, native, name)) sys.exit(0) if config.lang_help: if config.lang_help not in languages: sys.exit('%s is not a recognised language: try --lang-list' % config.lang_help) trans = languages[config.lang_help] print("Translations for %s / %s" % (trans['name'][0], trans['native'][0])) for kw in trans: if kw in 'name native'.split(): continue print('%16s: %s' % (kw.title().replace('_', ' '), ', '.join(w for w in trans[kw] if w != '*'))) sys.exit(0) if not config.format: default_format = config.defaults["default_format"] config.format = [ default_format ] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. default_format = config.defaults["default_format"] config.format.append(default_format) if 'help' in config.format: print("Available formatters:") formatters.list_formatters(sys.stdout) sys.exit(0) if len(config.outputs) > len(config.format): print('CONFIG-ERROR: More outfiles (%d) than formatters (%d).' % \ (len(config.outputs), len(config.format))) sys.exit(1) runner = Runner(config) try: failed = runner.run() except ParserError as e: sys.exit("ParseError: %s" % e) except ConfigError as e: sys.exit("ConfigError: %s" % e) except FileNotFoundError as e: sys.exit("FileNotFoundError: %s" % e) except InvalidFileLocationError as e: sys.exit("InvalidFileLocationError: %s" % e) except InvalidFilenameError as e: sys.exit("InvalidFilenameError: %s" % e) if config.show_snippets and runner.undefined: msg = "\nYou can implement step definitions for undefined steps with " msg += "these snippets:\n\n" printed = set() for step in runner.undefined: if step in printed: continue printed.add(step) msg += make_undefined_step_snippet(step) # -- OOPS: Unclear if stream supports ANSI coloring. sys.stderr.write(escapes['undefined'] + msg + escapes['reset']) sys.stderr.flush() if failed: sys.exit(1) # -- OTHERWISE: Successful run. sys.exit(0)
def main(args=None): # pylint: disable=too-many-branches, too-many-statements, too-many-return-statements config = Configuration(args) if config.version: print("behave " + __version__) return 0 if config.tags_help: print(TAG_HELP) return 0 if config.lang_list: from behave.i18n import languages stdout = sys.stdout if six.PY2: # -- PYTHON2: Overcome implicit encode problems (encoding=ASCII). stdout = codecs.getwriter("UTF-8")(sys.stdout) iso_codes = languages.keys() print("Languages available:") for iso_code in sorted(iso_codes): native = languages[iso_code]["native"][0] name = languages[iso_code]["name"][0] print(u"%s: %s / %s" % (iso_code, native, name), file=stdout) return 0 if config.lang_help: from behave.i18n import languages if config.lang_help not in languages: print("%s is not a recognised language: try --lang-list" % \ config.lang_help) return 1 trans = languages[config.lang_help] print(u"Translations for %s / %s" % (trans["name"][0], trans["native"][0])) for kw in trans: if kw in "name native".split(): continue print(u"%16s: %s" % (kw.title().replace("_", " "), u", ".join( w for w in trans[kw] if w != "*"))) return 0 if not config.format: config.format = [config.default_format] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. config.format.append(config.default_format) if "help" in config.format: print_formatters("Available formatters:") return 0 if len(config.outputs) > len(config.format): print("CONFIG-ERROR: More outfiles (%d) than formatters (%d)." % \ (len(config.outputs), len(config.format))) return 1 failed = True runner = Runner(config) try: failed = runner.run() except ParserError as e: print(u"ParserError: %s" % e) except ConfigError as e: print(u"ConfigError: %s" % e) except FileNotFoundError as e: print(u"FileNotFoundError: %s" % e) except InvalidFileLocationError as e: print(u"InvalidFileLocationError: %s" % e) except InvalidFilenameError as e: print(u"InvalidFilenameError: %s" % e) except Exception as e: # -- DIAGNOSTICS: text = _text(e) print(u"Exception %s: %s" % (e.__class__.__name__, text)) raise if config.show_snippets and runner.undefined_steps: print_undefined_step_snippets(runner.undefined_steps, colored=config.color) return_code = 0 if failed: return_code = 1 return return_code
def main(args=None): config = Configuration(args) if config.version: print "behave " + __version__ return 0 if config.tags_help: print TAG_HELP return 0 if config.lang_list: from behave.i18n import languages iso_codes = languages.keys() iso_codes.sort() print "Languages available:" for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print u'%s: %s / %s' % (iso_code, native, name) return 0 if config.lang_help: from behave.i18n import languages if config.lang_help not in languages: print '%s is not a recognised language: try --lang-list' % \ config.lang_help return 1 trans = languages[config.lang_help] print u"Translations for %s / %s" % (trans['name'][0], trans['native'][0]) for kw in trans: if kw in 'name native'.split(): continue print u'%16s: %s' % (kw.title().replace('_', ' '), u', '.join(w for w in trans[kw] if w != '*')) return 0 if not config.format: default_format = config.defaults["default_format"] config.format = [ default_format ] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. default_format = config.defaults["default_format"] config.format.append(default_format) if 'help' in config.format: from behave.formatter import formatters print "Available formatters:" formatters.list_formatters(sys.stdout) return 0 if len(config.outputs) > len(config.format): print 'CONFIG-ERROR: More outfiles (%d) than formatters (%d).' % \ (len(config.outputs), len(config.format)) return 1 failed = True runner = Runner(config) try: failed = runner.run() except ParserError, e: print "ParseError: %s" % e
def get_config(work_dir): config = Configuration() config.paths = [work_dir] config.format = ['pretty'] return config
def goh_behave(feature_dirs=None, step_dirs=None, test_artifact_dir=None, listeners=None, dry_run=False, webdriver_url=None, webdriver_processor=None, tags=None, show_skipped=True, config_file=None, test_config=None): """ runs behave :param feature_dirs: list of paths to feature files to run, or a single path. Defaults to singleton list of "features" :type feature_dirs: list :param step_dirs: list of paths to load steps from. the steps paths will be searched recursssssively :type step_dirs: list :param test_artifact_dir: path to where to store test artifacts. if None, no test artifacts will be written. note that setting this to None will prevent screenshots from automatically being taken after each step :param listeners: list of Listener objects to call for diff pts of the test :type listeners: list :param dry_run: if True, behave will just check if all steps are defined :param webdriver_url: optional. webdriver node/grid url to hit to execute tests :param webdriver_processor: provides the ability to process things like capabilities before they're actually used :param config_file: a configuration file, formatted as JSON data, that contains all of the other parameters listed here (except for listeners or webdriver_processor) :param test_config: a configuration that is a dictionary of the parameters. If a config_file is also passed, the values in the config_file will be written to the test_config as well. Any existing keys will have the values overwritten by the value in the config_file :return: True if the tests passed, else False :rtype: bool :raise ParserError: when a feature file couldnt be parsed :raise FileNotFoundError: when a feature file couldnt be found :raise InvalidFileLocationError: when a feature path was bad :raise InvalidFilenameError: when a feature file name was bad :raise UndefinedStepsError: if some steps were undefined """ if config_file: try: with open(config_file) as config: try: json_config = json.load(config) except ValueError as e: raise ValueError(u'Could not parse {} config file as JSON. See sample.config for an example config file. {}'.format(config_file, e)) if test_config: test_config.update(json_config) else: test_config = json_config except EnvironmentError as e: raise IOError(u'Could not open the {} config file. See sample.config for an example config file. {}'.format(config_file, e)) if test_config: log.debug(u'Using test_config:') for key in test_config: log.debug(u' {}: {}'.format(key, test_config[key])) if u'feature_dirs' in test_config and not feature_dirs: feature_dirs = test_config[u'feature_dirs'] if u'step_dirs' in test_config and not step_dirs: step_dirs = test_config[u'step_dirs'] if u'test_artifact_dir' in test_config and not test_artifact_dir: test_artifact_dir = test_config[u'test_artifact_dir'] if u'dry_run' in test_config: dry_run = test_config[u'dry_run'] if u'webdriver_url' in test_config and not webdriver_url: webdriver_url = test_config[u'webdriver_url'] if u'tags' in test_config and not tags: tags = test_config[u'tags'] if u'show_skipped' in test_config: show_skipped = test_config[u'show_skipped'] if not feature_dirs: feature_dirs = ["features"] if isinstance(feature_dirs, basestring): feature_dirs = [feature_dirs] args = [u''] # first run in dry run mode to catch any undefined steps # if the user specifies dry run mode, then don't do this, since it'll be # done anyway # but we're running it always anyway. so what's the difference? if user # specifies dry run mode, they might also give listeners, etc. # auto dry run mode is meant to happen silently if not dry_run: config = Configuration([u'--dry-run']) config.format = [] _run_behave(feature_dirs, config, step_dirs=step_dirs) # output test artifacts if test_artifact_dir: args.append(u'--junit') args.append(u'--junit-directory') args.append(test_artifact_dir) if dry_run: args.append(u'--dry-run') # setup config for behave's runner config = Configuration(args) config.format = [ # outputs pretty output in to stdout while tests are running u'pretty' ] # Set the tags if there are any if tags: log.debug(u'Running Scenarios with Tag(s): {}'.format(tags)) if isinstance(tags, list): log.debug(u'Running Scenarios with Tag List') config.tags = TagExpression(tags) else: config.tags = TagExpression(tags.split()) config.show_skipped = show_skipped if not show_skipped: log.debug(u'Not showing skipped scenarios.') # only add html reporter if the test artifacts are enabled if test_artifact_dir: config.reporters.append(HtmlReporter(config, test_artifact_dir)) try: return _run_behave( feature_dirs, config, step_dirs=step_dirs, webdriver_url=webdriver_url, webdriver_processor=webdriver_processor, listeners=listeners, test_config = test_config ) except ConfigError as e: # since we control the configuration file, we shouldn't expose this to # the user, since there's nothing they can do log.error(e) return False
if __name__ == "__main__": from behave.formatter import _registry from behave.configuration import Configuration from behave.runner import Runner from teamcity.jb_behave_formatter import TeamcityFormatter _registry.register_as("TeamcityFormatter", TeamcityFormatter) configuration = Configuration() configuration.format = ["TeamcityFormatter"] configuration.stdout_capture = False configuration.stderr_capture = False Runner(configuration).run()
def start(): u''' Точка входа в приложение, которая активируется при запуске его из командной строки. ''' setup_logging() config = {} try: arg_delim_index = sys.argv.index('--') behave_args = sys.argv[arg_delim_index+1:] sys.argv = sys.argv[:arg_delim_index] except ValueError: behave_args = [] opt_parser = OptionParser() opt_parser.add_option('', '--var-file', dest='var_file', help='Load template variables from .py file.', metavar='<FILE>') opt_parser.add_option('', '--cfg-file', dest='cfg_file', help='Load configuration from YAML file.', metavar='<FILE>') (options, _) = opt_parser.parse_args() if options.cfg_file: cfg_fn = options.cfg_file try: with open(cfg_fn, 'r') as fp: config = yaml.load(fp.read()) or {} except Exception as ex: logger.error('Can\'t load {0}: {1}'.format(cfg_fn, unicode(ex))) else: logger.info('Loaded configuration from {0}.'.format(cfg_fn)) if options.var_file: template_vars = load_vars_from_pyfile(options.var_file) # Есть смысл включать режим шаблонизации только при наличии переменных, # ради которых все и затевалось if template_vars: template_vars.pop('__builtins__', {}) sys.meta_path = [TemplateImportHooker(template_vars)] # Изменяем sys.argv для обхода поведения behave<=1.2.3, который # всегда пытается получить опции из командной строки. # TODO: с выходом стабильной 1.2.4 поменять на передачу command_args sys.argv = [sys.argv[0], ] + behave_args behave_cfg = Configuration() if not behave_cfg.format: behave_cfg.format = ['pretty', ] from behave.runner import Runner runner = Runner(behave_cfg) if 'enabled_plugins' in config: runner.hooks = StackedHookDictWrapper() # Ищем все доступные плагины... plugins = find_plugins() plugin_configs = config.get('plugins', {}) for p_id in config['enabled_plugins']: # TODO: убрать, если будет возможность подключать свои плагины assert p_id in plugins, 'Unknown plugin: {}!'.format(p_id) plugin = plugins[p_id] # Подключаем ещё один набор функций окружения. С точки зрения # behave'а, это будет одна функция, которая в свою очередь # по порядку будет вызывать обработчики _каждого_ плагина. logger.info('Loading plugin "{}"...'.format(p_id)) custom_hooks = plugin.prepare_environment(plugin_configs.get(p_id, {})) logger.debug('Plugin "{}" sets hooks: {}'.format(p_id, ', '.join(custom_hooks.keys()))) for hook, handler in custom_hooks.items(): runner.hooks[hook] = handler runner.run()
def main(): # pylint: disable=R0912,R0915 # R0912 Too many branches (17/12) # R0915 Too many statements (57/50) config = Configuration() if config.version: print "behave " + __version__ sys.exit(0) if config.tags_help: print TAG_HELP sys.exit(0) if config.lang_list: iso_codes = languages.keys() iso_codes.sort() print "Languages available:" for iso_code in iso_codes: native = languages[iso_code]['native'][0] name = languages[iso_code]['name'][0] print u'%s: %s / %s' % (iso_code, native, name) sys.exit(0) if config.lang_help: if config.lang_help not in languages: sys.exit('%s is not a recognised language: try --lang-list' % config.lang_help) trans = languages[config.lang_help] print u"Translations for %s / %s" % (trans['name'][0], trans['native'][0]) for kw in trans: if kw in 'name native'.split(): continue print u'%16s: %s' % (kw.title().replace('_', ' '), u', '.join(w for w in trans[kw] if w != '*')) sys.exit(0) if not config.format: default_format = config.defaults["default_format"] config.format = [ default_format ] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. default_format = config.defaults["default_format"] config.format.append(default_format) elif 'help' in config.format: print "Available formatters:" formatters.list_formatters(sys.stdout) sys.exit(0) if len(config.outputs) > len(config.format): print 'CONFIG-ERROR: More outfiles (%d) than formatters (%d).' % \ (len(config.outputs), len(config.format)) sys.exit(1) runner = Runner(config) try: failed = runner.run() except ParserError, e: sys.exit(str(e))
def main(args=None): config = Configuration(args) if config.version: print("behave " + __version__) return 0 if config.tags_help: print(TAG_HELP) return 0 if config.lang_list: from behave.i18n import languages stdout = sys.stdout if six.PY2: # -- PYTHON2: Overcome implicit encode problems (encoding=ASCII). stdout = codecs.getwriter("UTF-8")(sys.stdout) iso_codes = languages.keys() print("Languages available:") for iso_code in sorted(iso_codes): native = languages[iso_code]["native"][0] name = languages[iso_code]["name"][0] print(u"%s: %s / %s" % (iso_code, native, name), file=stdout) return 0 if config.lang_help: from behave.i18n import languages if config.lang_help not in languages: print("%s is not a recognised language: try --lang-list" % config.lang_help) return 1 trans = languages[config.lang_help] print(u"Translations for %s / %s" % (trans["name"][0], trans["native"][0])) for kw in trans: if kw in "name native".split(): continue print(u"%16s: %s" % (kw.title().replace("_", " "), u", ".join(w for w in trans[kw] if w != "*"))) return 0 if not config.format: config.format = [config.default_format] elif config.format and "format" in config.defaults: # -- CASE: Formatter are specified in behave configuration file. # Check if formatter are provided on command-line, too. if len(config.format) == len(config.defaults["format"]): # -- NO FORMATTER on command-line: Add default formatter. config.format.append(config.default_format) if "help" in config.format: print_formatters("Available formatters:") return 0 if len(config.outputs) > len(config.format): print("CONFIG-ERROR: More outfiles (%d) than formatters (%d)." % (len(config.outputs), len(config.format))) return 1 failed = True runner = Runner(config) try: failed = runner.run() except ParserError as e: print(u"ParseError: %s" % e) except ConfigError as e: print(u"ConfigError: %s" % e) except FileNotFoundError as e: print(u"FileNotFoundError: %s" % e) except InvalidFileLocationError as e: print(u"InvalidFileLocationError: %s" % e) except InvalidFilenameError as e: print(u"InvalidFilenameError: %s" % e) except Exception as e: # -- DIAGNOSTICS: text = _text(e) print(u"Exception %s: %s" % (e.__class__.__name__, text)) raise if config.show_snippets and runner.undefined_steps: print_undefined_step_snippets(runner.undefined_steps, colored=config.color) return_code = 0 if failed: return_code = 1 return return_code