Пример #1
0
    def test_scenario_has_to_run(self):
        """
            Test scenario's has to run functionality
        """
        feature = Mock(tags=[Scenario.Tag("feature_bar"), Scenario.Tag("feature_foo")])

        s = Scenario(1, "Scenario", "Some scenario", None, None, feature, [Scenario.Tag("foo", None), Scenario.Tag("bar", None), Scenario.Tag("bad_case", None)])
        s.absolute_id = 1
        s.has_to_run.when.called_with(None, None, ["foo"]).should.return_value(True)
        s.has_to_run.when.called_with(None, None, ["good_case", "foo"]).should.return_value(True)
        s.has_to_run.when.called_with(None, None, ["good_case", "bar", "bad_case"]).should.return_value(True)
        s.has_to_run.when.called_with(None, None, ["good_case"]).should.return_value(False)

        s.has_to_run.when.called_with([1], None, None).should.return_value(True)
        s.has_to_run.when.called_with([1, 2], None, None).should.return_value(True)
        s.has_to_run.when.called_with([2], None, None).should.return_value(False)

        s.has_to_run.when.called_with([1], None, ["good_case"]).should.return_value(True)
        s.has_to_run.when.called_with([1, 2], None, ["foo", "bad_case"]).should.return_value(True)
        s.has_to_run.when.called_with([5, 4], None, ["bad_case"]).should.return_value(True)
        s.has_to_run.when.called_with([6], None, ["good_case"]).should.return_value(False)

        s.has_to_run.when.called_with(None, ["feature"], None).should.return_value(False)
        s.has_to_run.when.called_with(None, ["feature_bar"], None).should.return_value(True)
        s.has_to_run.when.called_with(None, ["feature", "feature_bar"], None).should.return_value(True)
        s.has_to_run.when.called_with(None, ["feature_foo"], None).should.return_value(True)
Пример #2
0
    def test_scenario_after_parse_hook(self):
        """
            Test scenario after parse hook
        """
        step_1 = Mock(state=Step.State.UNTESTED, id=1)
        step_2 = Mock(state=Step.State.UNTESTED, id=2)
        step_3 = Mock(state=Step.State.UNTESTED, id=3)
        scenario = Scenario(1, "Scenario", "Some scenario", None, None, None)
        scenario.steps.extend([step_1, step_2, step_3])

        step_1.parent = scenario
        step_2.parent = scenario
        step_3.parent = scenario

        scenario.all_steps.should.have.length_of(3)
        scenario.all_steps[0].should.be.equal(step_1)
        scenario.all_steps[1].should.be.equal(step_2)
        scenario.all_steps[2].should.be.equal(step_3)

        scenario_precond = Scenario(2, "Scenario", "Some precond scenario", None, None, None)
        precond_step_1 = Mock(state=Step.State.UNTESTED, id=1)
        precond_step_2 = Mock(state=Step.State.UNTESTED, id=2)
        scenario_precond.steps.extend([precond_step_1, precond_step_2])

        precond_step_1.parent = scenario_precond
        precond_step_2.parent = scenario_precond

        scenario.preconditions.append(scenario_precond)

        # check before 'after_parse': step id and parent should be wrong
        scenario.all_steps[0].id.should.be.equal(1)
        scenario.all_steps[1].id.should.be.equal(2)
        scenario.all_steps[2].id.should.be.equal(1)
        scenario.all_steps[3].id.should.be.equal(2)
        scenario.all_steps[4].id.should.be.equal(3)
        scenario.all_steps[0].parent.should.be.equal(scenario_precond)
        scenario.all_steps[1].parent.should.be.equal(scenario_precond)
        scenario.all_steps[2].parent.should.be.equal(scenario)
        scenario.all_steps[3].parent.should.be.equal(scenario)
        scenario.all_steps[4].parent.should.be.equal(scenario)

        scenario.after_parse()

        scenario.all_steps[0].id.should.be.equal(1)
        scenario.all_steps[1].id.should.be.equal(2)
        scenario.all_steps[2].id.should.be.equal(3)
        scenario.all_steps[3].id.should.be.equal(4)
        scenario.all_steps[4].id.should.be.equal(5)
        scenario.all_steps[0].parent.should.be.equal(scenario)
        scenario.all_steps[1].parent.should.be.equal(scenario)
        scenario.all_steps[2].parent.should.be.equal(scenario)
        scenario.all_steps[3].parent.should.be.equal(scenario)
        scenario.all_steps[4].parent.should.be.equal(scenario)
Пример #3
0
    def test_before_each_scenario(self):
        """
            Test before.each_scenario from extension console_writer
        """
        data = threading.local()
        data.console = None

        def patched_write(text):
            text = re.sub(r"\x1b[^m]*m", "", text)
            data.console = text

        scenario = Scenario(1, "Scenario", "Some scenario", "somefile.feature", 2, None)
        scenario.parent = Mock(spec=Feature)
        scenario.parent.id = 1

        with patch("radish.extensions.console_writer.write", side_effect=patched_write):
            HookRegistry().call("before", "each_scenario", scenario)

            data.console.should.be.equal("\n    Scenario: Some scenario")
Пример #4
0
def test_scenario_has_to_run(scenario_id, scenario_choice, expected_has_to_run):
    """
    Test logic to check whether a Scenario has to run or not
    """
    # given
    scenario = Scenario(
        1,
        "Scenario",
        "I am a Scenario",
        "foo.feature",
        1,
        parent=None,
        tags=None,
        preconditions=None,
        background=None,
    )
    scenario.absolute_id = scenario_id

    # when
    actual_has_to_run = scenario.has_to_run(scenario_choice)

    # then
    assert actual_has_to_run is expected_has_to_run
Пример #5
0
def test_get_scenario_constants():
    """
    Test getting all constants from a Scenario
    """
    # given
    feature = Feature(1,
                      "Feature",
                      "I am a feature",
                      "foo.feature",
                      1,
                      tags=None)
    scenario = Scenario(
        1,
        "Scenario",
        "I am a Scenario",
        "foo.feature",
        2,
        parent=feature,
        tags=None,
        preconditions=None,
        background=None,
    )
    # add Feature constants
    feature.context.constants = [("foo", "1"), ("bar", "42")]
    # add Scenario constants
    scenario.context.constants = [("some_foo", "${foo}3"),
                                  ("answer", "${bar}")]

    # when
    constants = scenario.constants

    assert len(constants) == 4
    assert constants[0] == ("some_foo", "13")
    assert constants[1] == ("answer", "42")
    assert constants[2] == ("foo", "1")
    assert constants[3] == ("bar", "42")
Пример #6
0
def test_scenario_all_steps(mocker):
    """
    Test getting all Steps which are part of a Scenario
    """
    # given
    background = mocker.MagicMock(all_steps=[])
    precondition_scenario = mocker.MagicMock(all_steps=[])
    scenario = Scenario(
        1,
        "Scenario",
        "I am a Scenario",
        "foo.feature",
        1,
        parent=None,
        tags=None,
        preconditions=[precondition_scenario],
        background=background,
    )

    # when
    # add Steps to this Scenario
    scenario.steps.extend([mocker.MagicMock(state=Step.State.PASSED)])
    # add Steps to the Background
    background.all_steps.extend([
        mocker.MagicMock(state=Step.State.PASSED),
        mocker.MagicMock(state=Step.State.PASSED),
        mocker.MagicMock(state=Step.State.PASSED),
    ])
    # add Steps to the precondition Scenario
    precondition_scenario.all_steps.extend([
        mocker.MagicMock(state=Step.State.PASSED),
        mocker.MagicMock(state=Step.State.PASSED),
    ])

    # then
    assert len(scenario.all_steps) == 6
Пример #7
0
    def test_returncode_of_runner(self):
        """
            Test returncode of run functions in Runner
        """
        def some_passed_step(step):
            pass

        def some_failed_step(step):
            raise AttributeError("I expect this error to test the behavior of a failed step")

        feature = Feature(1, "Feature", "Some feature", "somefile.feature", 1)

        scenario = Scenario(1, 1, "Scenario", "Some scenario", "somefile.feature", 2, feature)
        feature.scenarios.append(scenario)

        step1 = Step(1, "Some passed step", "somefile.feature", 3, scenario, True)
        step1.definition_func = some_passed_step
        step1.arguments = tuple()
        step1.keyword_arguments = {}
        scenario.steps.append(step1)

        step2 = Step(2, "Some failed step", "somefile.feature", 4, scenario, True)
        step2.definition_func = some_failed_step
        step2.arguments = tuple()
        step2.keyword_arguments = {}
        scenario.steps.append(step2)

        hook_mock = Mock()
        hook_mock.call.return_value = True
        runner = Runner(hook_mock)
        returncode = runner.run_feature(feature)
        returncode.should.be.equal(1)
        step1.state.should.be.equal(Step.State.PASSED)
        step2.state.should.be.equal(Step.State.FAILED)
        scenario.state.should.be.equal(Step.State.FAILED)
        feature.state.should.be.equal(Step.State.FAILED)
Пример #8
0
def test_scenario_after_parse_logic(mocker):
    """
    Test logic which is used to complete the parsing of Scenario
    """
    # given
    background = Background(1, "Background", "I am a Background",
                            "foo.feature", 1)
    precondition_scenario = Scenario(2,
                                     "Scenario",
                                     "I am a Scenario",
                                     "foo.feature",
                                     1,
                                     parent=None)
    scenario = Scenario(
        1,
        "Scenario",
        "I am a Scenario",
        "foo.feature",
        1,
        parent=None,
        tags=None,
        preconditions=[precondition_scenario],
        background=background,
    )
    # add Steps to this Scenario
    scenario.steps.extend(
        [mocker.MagicMock(id=99, as_background=False, as_precondition=False)])
    # set Scenario Step parents
    for step in scenario.steps:
        step.parent = scenario
    # add Steps to the Background
    background.steps.extend([
        mocker.MagicMock(id=5, as_background=False, as_precondition=False),
        mocker.MagicMock(id=6, as_background=False, as_precondition=False),
        mocker.MagicMock(id=66, as_background=False, as_precondition=False),
    ])
    # set Background Step parents
    for background_step in background.steps:
        background_step.parent = background
    # add Steps to the precondition Scenario
    precondition_scenario.steps.extend([
        mocker.MagicMock(id=5, as_background=False, as_precondition=False),
        mocker.MagicMock(id=77, as_background=False, as_precondition=False),
    ])
    # set Precondition Scenario Step parents
    for step in precondition_scenario.steps:
        step.parent = precondition_scenario

    # when after_parse() was not called it's not completed
    assert scenario.complete is False

    # when
    scenario.after_parse()
    steps = scenario.all_steps

    # then - the Scenario is completed
    assert scenario.complete is True

    # then - the step id's are in valid order
    assert steps[0].id == 1
    assert steps[1].id == 2
    assert steps[2].id == 3
    assert steps[3].id == 4
    assert steps[4].id == 5
    assert steps[5].id == 6

    # then - check as_background flags
    assert all(step.as_background for step in background.steps)
    # then - check as_precondition flags
    assert all(step.as_precondition for step in precondition_scenario.steps)
Пример #9
0
 def after_parse(self):
     Scenario.after_parse(self)
     self.build_scenarios()
Пример #10
0
def test_scenario_after_parse_logic(mocker):
    """
    Test logic which is used to complete the parsing of Scenario
    """
    # given
    background = Background(1, "Background", "I am a Background", "foo.feature", 1)
    precondition_scenario = Scenario(
        2, "Scenario", "I am a Scenario", "foo.feature", 1, parent=None
    )
    scenario = Scenario(
        1,
        "Scenario",
        "I am a Scenario",
        "foo.feature",
        1,
        parent=None,
        tags=None,
        preconditions=[precondition_scenario],
        background=background,
    )
    # add Steps to this Scenario
    scenario.steps.extend(
        [mocker.MagicMock(id=99, as_background=False, as_precondition=False)]
    )
    # set Scenario Step parents
    for step in scenario.steps:
        step.parent = scenario
    # add Steps to the Background
    background.steps.extend(
        [
            mocker.MagicMock(id=5, as_background=False, as_precondition=False),
            mocker.MagicMock(id=6, as_background=False, as_precondition=False),
            mocker.MagicMock(id=66, as_background=False, as_precondition=False),
        ]
    )
    # set Background Step parents
    for background_step in background.steps:
        background_step.parent = background
    # add Steps to the precondition Scenario
    precondition_scenario.steps.extend(
        [
            mocker.MagicMock(id=5, as_background=False, as_precondition=False),
            mocker.MagicMock(id=77, as_background=False, as_precondition=False),
        ]
    )
    # set Precondition Scenario Step parents
    for step in precondition_scenario.steps:
        step.parent = precondition_scenario

    # when after_parse() was not called it's not completed
    assert scenario.complete is False

    # when
    scenario.after_parse()
    steps = scenario.all_steps

    # then - the Scenario is completed
    assert scenario.complete is True

    # then - the step id's are in valid order
    assert steps[0].id == 1
    assert steps[1].id == 2
    assert steps[2].id == 3
    assert steps[3].id == 4
    assert steps[4].id == 5
    assert steps[5].id == 6

    # then - check as_background flags
    assert all(step.as_background for step in background.steps)
    # then - check as_precondition flags
    assert all(step.as_precondition for step in precondition_scenario.steps)