def __init__(self): self.set = StorySet() self.stories = self.set.stories self.failures = [] self.errors = [] self.traces = {} self.directory = None self.parser = argparse.ArgumentParser() self.parser.add_argument("directory")
class BaseOutput(metaclass=ABCMeta): """Base class for ouput. Ideally, all outputs should inherit from this class. It contains methods to help control how the reports are displayed. """ def __init__(self): self.set = StorySet() self.stories = self.set.stories self.failures = [] self.errors = [] self.traces = {} self.directory = None self.parser = argparse.ArgumentParser() self.parser.add_argument("directory") def parse_args(self): """Parse the arguments from the argument parser.""" args = self.parser.parse_args() self.handle_args(args) def handle_args(self, args): """Handle the command-line arguments.""" self.directory = args.directory def load(self): """Load the steps and stories of a given directory.""" directory = self.directory try: self.set.load(directory) except LanguageSyntaxError as err: self.handle_syntax_error(err) sys.exit(1) def run(self): """Run the different stories.""" for story_name in self.stories: self.run_story(story_name) def run_story(self, story_name): """Run a specific story. In this method, the different assert_* errors are being tested to produce a report. """ story = self.set.stories[story_name] for scenario in story.scenarios: self.run_scenario(story_name, scenario) def run_scenario(self, story_name, scenario): """Run a specific scenario.""" try: self.set.run_scenario(story_name, scenario) except StepNotFound as err: self.failures.append(err) self.traces[scenario.identifier] = traceback.format_exc() self.handle_step_not_found(err) except StepAssertionError as err: self.failures.append(err) self.traces[scenario.identifier] = traceback.format_exc() self.handle_assertion(err) except Exception as err: self.errors.append((scenario, err)) self.traces[scenario.identifier] = traceback.format_exc() self.handle_exception(err) else: self.handle_success(story_name) self.display_report() @abstractmethod def handle_syntax_error(self, error): """Handle a syntax error in one of the user stories.""" pass @abstractmethod def handle_step_not_found(self, error): """Handle the error if a step cannot be found.""" pass @abstractmethod def handle_assertion(self, error): """Handle the error when an assertion fails.""" pass @abstractmethod def handle_exception(self, exception): """Handle the exception when an error occures while executing.""" pass @abstractmethod def handle_success(self, story_name): """Handle when a user story passes without errors.""" pass def display_report(self): """Display a report.""" self.display_main_report() self.display_report_failures() self.display_report_errors() @abstractmethod def display_main_report(self): """Display the main report (statistics).""" pass @abstractmethod def display_report_failures(self): """Display the report on failures.""" pass @abstractmethod def display_report_errors(self): """Display the report on errors.""" pass