Ejemplo n.º 1
0
    def test_merge_steps(self):
        """
            Test merging steps from feature files with registered steps
        """
        matcher = Matcher()
        steps = {
            re.compile(r"Given I have the number (\d+)"): "some_func",
            re.compile(r"I add (\d+) to my number"): "some_other_func"
        }

        feature = Feature(1, "Feature", "Some feature", "test.feature", 1)
        scenario = Scenario(1, "Scenario", "Adding numbers", "test.feature", 2,
                            feature)
        scenario.steps.append(
            Step(1, "Given I have the number 5", "test.feature", 3, scenario,
                 False))
        scenario.steps.append(
            Step(2, "When I add 2 to my number", "test.feature", 4, scenario,
                 False))
        feature.scenarios.append(scenario)

        matcher.merge_steps([feature], steps)

        scenario.steps[0].definition_func.should.be.equal("some_func")
        scenario.steps[0].arguments.should.be.equal(("5", ))
        scenario.steps[1].definition_func.should.be.equal("some_other_func")
        scenario.steps[1].arguments.should.be.equal(("2", ))
Ejemplo n.º 2
0
def run_features(core):
    """
        Run the parsed features

        :param Core core: the radish core object
    """
    # FIXME: load dynamically
    import radish.extensions.argumentexpressions
    import radish.extensions.time_recorder
    import radish.extensions.syslog_writer
    import radish.extensions.console_writer
    import radish.extensions.endreport_writer
    import radish.extensions.failure_inspector
    import radish.extensions.failure_debugger
    import radish.extensions.bdd_xml_writer

    # set needed configuration
    world.config.expand = True

    # load user's custom python files
    loader = Loader(world.config.basedir)
    loader.load_all()

    # match feature file steps with user's step definitions
    Matcher.merge_steps(core.features, StepRegistry().steps)

    # run parsed features
    if world.config.marker == "time.time()":
        world.config.marker = int(time())

    # scenario choice
    amount_of_scenarios = sum(len(f.scenarios) for f in core.features_to_run)
    if world.config.scenarios:
        world.config.scenarios = [int(s) for s in world.config.scenarios.split(",")]
        for s in world.config.scenarios:
            if s <= 0 or s > amount_of_scenarios:
                raise ScenarioNotFoundError(s, amount_of_scenarios)

    # tags
    if world.config.feature_tags:
        world.config.feature_tags = [t for t in world.config.feature_tags.split(",")]
        for tag in world.config.feature_tags:
            if not any(f for f in core.features if tag in [t.name for t in f.tags]):
                raise FeatureTagNotFoundError(tag)

    if world.config.scenario_tags:
        world.config.scenario_tags = [t for t in world.config.scenario_tags.split(",")]
        for tag in world.config.scenario_tags:
            if not any(s for f in core.features for s in f.scenarios if tag in [t.name for t in s.tags]):
                raise ScenarioTagNotFoundError(tag)

    runner = Runner(HookRegistry(), early_exit=world.config.early_exit)
    runner.start(core.features_to_run, marker=world.config.marker)
Ejemplo n.º 3
0
    def test_merge_steps(self):
        """
            Test merging steps from feature files with registered steps
        """
        matcher = Matcher()
        steps = {re.compile(r"Given I have the number (\d+)"): "some_func", re.compile(r"I add (\d+) to my number"): "some_other_func"}

        feature = Feature(1, "Feature", "Some feature", "test.feature", 1)
        scenario = Scenario(1, "Scenario", "Adding numbers", "test.feature", 2, feature)
        scenario.steps.append(Step(1, "Given I have the number 5", "test.feature", 3, scenario, False))
        scenario.steps.append(Step(2, "When I add 2 to my number", "test.feature", 4, scenario, False))
        feature.scenarios.append(scenario)

        matcher.merge_steps([feature], steps)

        scenario.steps[0].definition_func.should.be.equal("some_func")
        scenario.steps[0].arguments.should.be.equal(("5",))
        scenario.steps[1].definition_func.should.be.equal("some_other_func")
        scenario.steps[1].arguments.should.be.equal(("2",))
Ejemplo n.º 4
0
    def test_match_steps(self):
        """
            Test matching steps from feature files with registered steps
        """
        matcher = Matcher()
        steps = {re.compile(r"Given I have the number (\d+)"): "some_func", re.compile(r"I add (\d+) to my number"): "some_other_func"}

        arguments, keyword_arguments, func = matcher.match("Given I have the number 5", steps)
        arguments.should.be.equal(("5",))
        keyword_arguments.should.be.equal({})
        func.should.be.equal("some_func")

        arguments, keyword_arguments, func = matcher.match("When I add 2 to my number", steps)
        arguments.should.be.equal(("2",))
        keyword_arguments.should.be.equal({})
        func.should.be.equal("some_other_func")

        match = matcher.match("when I call a non-existing step", steps)
        match.should.be.none
Ejemplo n.º 5
0
    def test_match_steps(self):
        """
            Test matching steps from feature files with registered steps
        """
        matcher = Matcher()
        steps = {
            re.compile(r"Given I have the number (\d+)"): "some_func",
            re.compile(r"I add (\d+) to my number"): "some_other_func"
        }

        arguments, keyword_arguments, func = matcher.match(
            "Given I have the number 5", steps)
        arguments.should.be.equal(("5", ))
        keyword_arguments.should.be.equal({})
        func.should.be.equal("some_func")

        arguments, keyword_arguments, func = matcher.match(
            "When I add 2 to my number", steps)
        arguments.should.be.equal(("2", ))
        keyword_arguments.should.be.equal({})
        func.should.be.equal("some_other_func")

        match = matcher.match("when I call a non-existing step", steps)
        match.should.be.none
Ejemplo n.º 6
0
    def behave_like(self, sentence):
        """
            Make step behave like another one

            :param string sentence: the sentence of the step to behave like
        """
        # check if this step has already failed from a previous behave_like call
        if self.state is Step.State.FAILED:
            return

        # create step according to given sentence
        new_step = Step(None, sentence, self.path, self.line, self.parent, True)
        Matcher.merge_step(new_step, StepRegistry().steps)

        # run or debug step
        if world.config.debug_steps:
            new_step.debug()
        else:
            new_step.run()

        # re-raise exception if the failed
        if new_step.state is Step.State.FAILED:
            new_step.failure.exception.args = ("Step '{}' failed: '{}'".format(sentence, new_step.failure.exception.message),)
            raise new_step.failure.exception
Ejemplo n.º 7
0
    def test_merge_non_existing_step(self):
        """
            Test merging non existing step
        """
        matcher = Matcher()
        steps = {
            re.compile(r"Given I have the number (\d+)"): "some_func",
            re.compile(r"I add (\d+) to my number"): "some_other_func"
        }

        feature = Feature(1, "Feature", "Some feature", "test.feature", 1)
        scenario = Scenario(1, "Scenario", "Adding numbers", "test.feature", 2,
                            feature)
        scenario.steps.append(
            Step(1, "When I call a non-existing step", "test.feature", 3,
                 scenario, False))
        feature.scenarios.append(scenario)

        matcher.merge_steps.when.called_with([feature], steps).should.throw(
            StepDefinitionNotFoundError,
            "Cannot find step definition for step 'When I call a non-existing step' in test.feature:3"
        )