예제 #1
0
    def test_should_run_with_name_select(self):
        scenario_name = u"first scenario"
        scenario = model.Scenario("foo.feature", 17, u"Scenario", scenario_name)
        self.config.name = ['first .*', 'second .*']
        self.config.name_re = Configuration.build_name_re(self.config.name)

        assert scenario.should_run_with_name_select(self.config)
예제 #2
0
    def parse_steps(self, text, filename=None):
        """
        Parse support for execute_steps() functionality that supports step with:
          * multiline text
          * table

        :param text:  Text that contains 0..* steps
        :return: List of parsed steps (as model.Step objects).
        """
        assert isinstance(text, six.text_type)
        if not self.language:
            self.language = DEFAULT_LANGUAGE
        self.reset()
        self.filename = filename
        self.statement = model.Scenario(filename, 0, u"scenario", u"")
        self.state = "steps"

        for line in text.split("\n"):
            self.line += 1
            if not line.strip() and self.state != "multiline":
                # -- SKIP EMPTY LINES, except in multiline string args.
                continue
            self.action(line)

        # -- FINALLY:
        if self.table:
            self.action_table("")
        steps = self.statement.steps
        return steps
예제 #3
0
 def _build_scenario_statement(self, keyword, line):
     name = line[len(keyword) + 1:].strip()
     self.statement = model.Scenario(self.filename, self.line,
                                     keyword, name, tags=self.tags)
     self.feature.add_scenario(self.statement)
     # -- RESET STATE:
     self.tags = []
예제 #4
0
    def test_scenario_hooks_not_run_if_scenario_not_being_run(self):
        self.config.tags.check.return_value = False

        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo')

        scenario.run(self.runner)

        assert not self.run_hook.called
예제 #5
0
    def action_steps(self, line):
        stripped = line.lstrip()
        if stripped.startswith('"""') or stripped.startswith("'''"):
            self.state = 'multiline'
            self.multiline_start = self.line
            self.multiline_terminator = stripped[:3]
            self.multiline_leading = line.index(stripped[0])
            return True

        line = line.strip()

        step = self.parse_step(line)
        if step:
            self.statement.steps.append(step)
            return True

        if line.startswith('@'):
            self.tags.extend([model.Tag(tag.strip(), self.line)
                for tag in line[1:].split('@')])
            return True

        scenario_kwd = self.match_keyword('scenario', line)
        if scenario_kwd:
            name = line[len(scenario_kwd) + 1:].strip()
            self.statement = model.Scenario(self.filename, self.line,
                                            scenario_kwd, name, tags=self.tags)
            self.tags = []
            self.feature.add_scenario(self.statement)
            return True

        scenario_outline_kwd = self.match_keyword('scenario_outline', line)
        if scenario_outline_kwd:
            name = line[len(scenario_outline_kwd) + 1:].strip()
            self.statement = model.ScenarioOutline(self.filename, self.line,
                                                   scenario_outline_kwd, name,
                                                   tags=self.tags)
            self.tags = []
            self.feature.add_scenario(self.statement)
            self.state = 'steps'
            return True

        examples_kwd = self.match_keyword('examples', line)
        if examples_kwd:
            if not isinstance(self.statement, model.ScenarioOutline):
                message = 'Examples must only appear inside scenario outline'
                raise ParserError(message, self.line)
            name = line[len(examples_kwd) + 1:].strip()
            self.examples = model.Examples(self.filename, self.line,
                                           examples_kwd, name)
            self.statement.examples.append(self.examples)
            self.state = 'table'
            return True

        if line.startswith('|'):
            self.state = 'table'
            return self.action_table(line)

        return False
예제 #6
0
    def test_run_invokes_formatter_scenario_and_steps_correctly(self):
        self.config.stdout_capture = False
        self.config.log_capture = False
        self.config.tags.check.return_value = True
        steps = [Mock(), Mock()]
        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo',
                                  steps=steps)

        scenario.run(self.runner)

        self.formatter.scenario.assert_called_with(scenario)
        [step.run.assert_called_with(self.runner) for step in steps]
예제 #7
0
    def test_handles_stdout_and_log_capture(self):
        self.config.stdout_capture = True
        self.config.log_capture = True
        self.config.tags.check.return_value = True

        steps = [Mock(), Mock()]
        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo',
                                  steps=steps)

        scenario.run(self.runner)

        self.runner.setup_capture.assert_called_with()
        self.runner.teardown_capture.assert_called_with()
예제 #8
0
    def test_failed_step_causes_context_failure_to_be_set(self):
        self.config.stdout_capture = False
        self.config.log_capture = False
        self.config.tags.check.return_value = True

        steps = [Mock(), Mock()]
        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo',
                                  steps=steps)
        steps[0].run.return_value = False

        assert scenario.run(self.runner)

        self.context._set_root_attribute.assert_called_with('failed', True)
예제 #9
0
    def test_failed_step_causes_remaining_steps_to_be_skipped(self):
        self.config.stdout_capture = False
        self.config.log_capture = False
        self.config.tags.check.return_value = True

        steps = [Mock(), Mock()]
        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo',
                                  steps=steps)
        steps[0].run.return_value = False

        assert scenario.run(self.runner)

        eq_(steps[1].status, 'skipped')
예제 #10
0
    def test_skipped_steps_set_step_status_and_scenario_status_if_not_set(self):
        self.config.stdout_capture = False
        self.config.log_capture = False
        self.config.tags.check.return_value = False

        steps = [Mock(), Mock()]
        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo',
                                  steps=steps)

        scenario.run(self.runner)

        assert False not in [s.status == 'skipped' for s in steps]
        eq_(scenario.status, 'skipped')
예제 #11
0
    def action_feature(self, line):
        line = line.strip()

        if line.startswith('@'):
            self.tags.extend(self.parse_tags(line))
            return True

        background_kwd = self.match_keyword('background', line)
        if background_kwd:
            name = line[len(background_kwd) + 1:].strip()
            self.statement = model.Background(self.filename, self.line,
                                              background_kwd, name)
            self.feature.background = self.statement
            self.state = 'steps'
            return True

        scenario_kwd = self.match_keyword('scenario', line)
        if scenario_kwd:
            name = line[len(scenario_kwd) + 1:].strip()
            self.statement = model.Scenario(self.filename,
                                            self.line,
                                            scenario_kwd,
                                            name,
                                            tags=self.tags)
            self.tags = []
            self.feature.add_scenario(self.statement)
            self.state = 'steps'
            return True

        scenario_outline_kwd = self.match_keyword('scenario_outline', line)
        if scenario_outline_kwd:
            name = line[len(scenario_outline_kwd) + 1:].strip()
            self.statement = model.ScenarioOutline(self.filename,
                                                   self.line,
                                                   scenario_outline_kwd,
                                                   name,
                                                   tags=self.tags)
            self.tags = []
            self.feature.add_scenario(self.statement)
            self.state = 'steps'
            return True

        self.feature.description.append(line)
        return True
예제 #12
0
    def test_undefined_step_causes_failed_scenario_status(self):
        self.config.stdout_capture = False
        self.config.log_capture = False
        self.config.tags.check.return_value = True

        passed_step = Mock()
        undefined_step = Mock()

        steps = [passed_step, undefined_step]
        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo',
                                  steps=steps)
        passed_step.run.return_value = True
        passed_step.status = 'passed'
        undefined_step.run.return_value = False
        undefined_step.status = 'undefined'

        assert scenario.run(self.runner)
        eq_(undefined_step.status, 'undefined')
        eq_(scenario.status, 'failed')
        self.context._set_root_attribute.assert_called_with('failed', True)
예제 #13
0
    def test_failed_step_causes_remaining_steps_to_be_skipped(self):
        self.config.stdout_capture = False
        self.config.log_capture = False
        self.config.tags.check.return_value = True

        steps = [Mock(), Mock()]
        scenario = model.Scenario('foo.feature', 17, u'Scenario', u'foo',
                                  steps=steps)
        steps[0].run.return_value = False
        steps[1].step_type = "when"
        steps[1].name = "step1"

        def step1_function(context):
            pass
        my_step_registry = step_registry.StepRegistry()
        my_step_registry.add_step_definition("when", "step1", step1_function)

        with patch("behave.step_registry.registry", my_step_registry):
            assert scenario.run(self.runner)
            eq_(steps[1].status, 'skipped')
예제 #14
0
 def parse_scenario(self, json_element):
     """
     self.add_feature_element({
         'keyword': scenario.keyword,
         'name': scenario.name,
         'tags': scenario.tags,
         'location': scenario.location,
         'steps': [],
     })
     """
     keyword = json_element.get("keyword", u"")
     name    = json_element.get("name", u"")
     description = json_element.get("description", [])
     tags    = json_element.get("tags", [])
     location = json_element.get("location", u"")
     json_steps = json_element.get("steps", [])
     steps = self.parse_steps(json_steps)
     filename, line = location.split(":")
     scenario = model.Scenario(filename, line, keyword, name, tags, steps)
     scenario.description = description
     return scenario
예제 #15
0
    def test_scenario_around_hook(self):
        class CtxMgr(object):
            has_entered = has_exited = False

            def __init__(self, *a, **kw):
                pass

            def __enter__(self):
                CtxMgr.has_entered = True

            def __exit__(self, *a, **kw):
                CtxMgr.has_exited = True

        self.runner.hooks['AroundScenario'] = CtxMgr

        scenario_name = u"surrounded_scenario"
        scenario = model.Scenario("foo.feature", 17, u"Scenario",
                                  scenario_name)

        scenario.run(self.runner)

        assert (CtxMgr.has_exited & CtxMgr.has_exited) == True