def __init__(self, base_path, scenarios=None, verbosity=0, xml_filename=None): """ lettuce.Runner will try to find a terrain.py file and import it from within `base_path` """ self.single_feature = None if os.path.isfile(base_path) and os.path.exists(base_path): self.single_feature = base_path base_path = os.path.dirname(base_path) sys.path.insert(0, base_path) self.loader = fs.FeatureLoader(base_path) self.verbosity = verbosity self.scenarios = scenarios and map(int, scenarios.split(",")) or None sys.path.remove(base_path) if verbosity is 0: from lettuce.plugins import non_verbose as output elif verbosity is 3: from lettuce.plugins import shell_output as output else: from lettuce.plugins import colored_shell_output as output reload(output) self.output = output self._junit_xml_result = None if xml_filename: self._xml_file = open(xml_filename, 'w+') self._junit_xml_result = JUnitXmlResult(self._xml_file)
class Runner(object): """ Main lettuce's test runner Takes a base path as parameter (string), so that it can look for features and step definitions on there. """ def __init__(self, base_path, scenarios=None, verbosity=0, xml_filename=None): """ lettuce.Runner will try to find a terrain.py file and import it from within `base_path` """ self.single_feature = None if os.path.isfile(base_path) and os.path.exists(base_path): self.single_feature = base_path base_path = os.path.dirname(base_path) sys.path.insert(0, base_path) self.loader = fs.FeatureLoader(base_path) self.verbosity = verbosity self.scenarios = scenarios and map(int, scenarios.split(",")) or None sys.path.remove(base_path) if verbosity is 0: from lettuce.plugins import non_verbose as output elif verbosity is 3: from lettuce.plugins import shell_output as output else: from lettuce.plugins import colored_shell_output as output reload(output) self.output = output self._junit_xml_result = None if xml_filename: self._xml_file = open(xml_filename, 'w+') self._junit_xml_result = JUnitXmlResult(self._xml_file) def _create_junit_xml(self, total): """ Write an JUnit compatible XML """ for feature in total.feature_results: for scenario in feature.scenario_results: if scenario.passed: self._junit_xml_result.addSuccess({'classname': '%s (%s)' % (scenario.scenario.with_file, scenario.scenario.feature.name), 'name': scenario.scenario.name, 'duration': scenario.duration}) else: for step in scenario.steps_failed: step.why.traceback = "Step: %s\n %s" %(step.sentence, step.why.traceback) self._junit_xml_result.addFailure({ 'classname': '%s (%s)' % (scenario.scenario.with_file, scenario.scenario.feature.name), 'name': scenario.scenario.name, 'duration': scenario.duration, 'failureException': type(step.why.exception)}, step.why) self._junit_xml_result.stopTestRun(total.scenarios_ran) self._xml_file.close() def run(self): """ Find and load step definitions, and them find and load features under `base_path` specified on constructor """ if self._junit_xml_result: self._junit_xml_result.startTestRun() started_at = datetime.now() self.loader.find_and_load_step_definitions() call_hook('before', 'all') results = [] if self.single_feature: features_files = [self.single_feature] else: features_files = self.loader.find_feature_files() if not features_files: self.output.print_no_features_found(self.loader.base_dir) return failed = False try: for filename in features_files: feature = Feature.from_file(filename) results.append(feature.run(self.scenarios)) except exceptions.LettuceSyntaxError, e: sys.stderr.write(e.msg) failed = True finally: