Example #1
0
def test_step_can_assign_a_step_impl(mocker):
    """A Step can be assigned a Step Implementation"""
    # given
    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")

    # when
    step.assign_implementation(step_impl_mock, step_impl_match_mock)

    # then
    assert step.step_impl is step_impl_mock
    assert step.step_impl_match is step_impl_match_mock
Example #2
0
def test_step_fail_to_run_if_already_run(mocker):
    """A Step should fail to run if it was already run / has another state then UNTESTED"""
    # given
    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")
    step.assign_implementation(step_impl_mock, step_impl_match_mock)
    step.state = State.PASSED

    # then
    with pytest.raises(RadishError):
        # when
        step.run(None)
Example #3
0
def test_step_should_evaluate_its_matched_step_impl_arguments(mocker):
    """A Step should evlauate the arguments of its matched Step Implementation"""
    # given
    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")
    step_impl_match_mock.evaluate.return_value = ([], {})
    step.assign_implementation(step_impl_mock, step_impl_match_mock)

    # when
    step.run(None)

    # then
    step_impl_match_mock.evaluate.assert_called_once_with()
Example #4
0
def test_step_should_change_state_to_passed_if_step_impl_func_not_raised(mocker):
    """
    A Step should change its State to PASSED if the ran
    Step Implementation function did not raise any Exception
    """
    # given
    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")
    step_impl_match_mock.evaluate.return_value = ([], {})
    step.assign_implementation(step_impl_mock, step_impl_match_mock)

    # when
    step.run(None)

    # then
    assert step.state is State.PASSED
Example #5
0
def test_step_fail_if_step_impl_func_raises(mocker):
    """A Step should fail if the Step Implementation function raised an Exception"""
    # given
    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step.fail = mocker.MagicMock(name="Step fail function")
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    exception = Exception("buuh!")
    step_impl_mock.func.side_effect = exception
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")
    step_impl_match_mock.evaluate.return_value = ([], {})
    step.assign_implementation(step_impl_mock, step_impl_match_mock)

    # when
    step.run(None)

    # then
    step.fail.assert_called_once_with(exception)
Example #6
0
def test_step_should_pass_evaluated_args_to_step_impl_func(mocker):
    """
    A Step should pass the evaluated args from the Step Implementation match function
    to the Step Implementation function.
    """
    # given
    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")
    step_impl_match_mock.evaluate.return_value = (["foo", "bar"], {})
    step.assign_implementation(step_impl_mock, step_impl_match_mock)

    # when
    step.run(None)

    # then
    step_impl_mock.func.assert_called_once_with(step, "foo", "bar")
Example #7
0
def test_step_should_not_set_passed_state_if_state_changed_during_run(mocker):
    """A Step should not set its State to PASSED if the state was changed during the run"""
    # given
    def step_change_state(step):
        step.skip()

    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    step_impl_mock.func = step_change_state
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")
    step_impl_match_mock.evaluate.return_value = ([], {})
    step.assign_implementation(step_impl_mock, step_impl_match_mock)

    # when
    step.run(None)

    # then
    assert step.state is State.SKIPPED
Example #8
0
def test_step_should_set_state_to_running_before_running_step_impl(mocker):
    """A Step should set its State to RUNNING before it runs the Step Implementation function"""
    # given
    class WrapperForMockerSpy:
        def step_func(self, step):
            assert step.state is State.RUNNING

    w = WrapperForMockerSpy()
    mocker.spy(w, "step_func")

    step = Step(1, "keyword", "used_keyword", "text", None, None, None, None)
    step_impl_mock = mocker.MagicMock(name="Step Impl")
    step_impl_mock.func = w.step_func
    step_impl_match_mock = mocker.MagicMock(name="Step Impl Match")
    step_impl_match_mock.evaluate.return_value = ([], {})
    step.assign_implementation(step_impl_mock, step_impl_match_mock)

    # when
    step.run(None)

    # then
    w.step_func.assert_called_once_with(step)
Example #9
0
def match_step(step: Step, registry):
    """Match the given ``Step`` with a registered Step Implementation from the ``StepRegistry``.

    If no match can be made an error is raised.
    If a match can be made the found ``StepImpl`` is assigned to the ``Step``.
    """
    # get all possible Step Implementation from the registry
    # depending on the Step Keyword.
    step_impls = registry.step_implementations(step.keyword)

    # resolve the Constant Tags for the matching
    # FIXME(TF): it's quite shitty that the matcher has to know about constants ...
    step_text = _resolve_constant_tags_in_step_text(step.text, step.scenario.constants)

    potentional_matches = []
    for step_impl in step_impls:
        try:
            matcher = matchers[type(step_impl.pattern)]
        except KeyError:
            raise StepImplementationPatternNotSupported(step_impl)

        match, match_length = matcher(step_text, step_impl)
        if match:
            if len(step_text) == match_length:
                # if perfect match can be made we return it no
                # matter of the other potentional matches
                step.assign_implementation(step_impl, match)
                return

            distance_to_perfect = abs(len(step_text) - match_length)
            potentional_matches.append(((step_impl, match), distance_to_perfect))

    if potentional_matches:
        # get best match
        best_step_impl, best_match = min(potentional_matches, key=lambda x: x[1])[0]
        step.assign_implementation(best_step_impl, best_match)
        return

    raise StepImplementationNotFoundError(step)