示例#1
0
def test_run_debug_step_function_with_posargs(debug_or_run, mocker,
                                              mock_utils_debugger):
    """
    Test running/debugging a Step with a function and positional arguments
    """
    # mock step function which is to use
    mocker.spy(StepHelper, "step_func")

    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )
    step.definition_func = StepHelper.step_func
    step.argument_match = mocker.MagicMock()
    step.argument_match.evaluate.return_value = ((1, 2), {})

    # when
    method = getattr(step, debug_or_run)
    state = method()

    # then
    assert state == Step.State.PASSED
    StepHelper.step_func.assert_called_once_with(step, 1, 2)
示例#2
0
    def test_running_a_feature(self):
        """
            Test running a feature
        """
        data = threading.local()
        data.step_was_called = False

        def some_step(step):
            data.step_was_called = True

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

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

        step = Step(1, "Some step", "somefile.feature", 3, scenario, True)
        step.definition_func = some_step
        argument_match_mock = Mock()
        argument_match_mock.evaluate.return_value = (tuple(), {})
        step.argument_match = argument_match_mock
        scenario.steps.append(step)

        hook_mock = Mock()
        hook_mock.call.return_value = True
        runner = Runner(hook_mock)
        runner.run_feature(feature)
        step.state.should.be.equal(Step.State.PASSED)
        data.step_was_called.should.be.true
示例#3
0
def test_run_debug_step_function_mark_skipped(
    debug_or_run, mocker, mock_utils_debugger
):
    """
    Test running/debugging a Step which marks itself as skipped
    """
    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )
    step.definition_func = StepHelper.step_skip_func
    step.argument_match = mocker.MagicMock()
    step.argument_match.evaluate.return_value = (tuple(), {})

    # when
    method = getattr(step, debug_or_run)
    state = method()

    # then
    assert state == Step.State.SKIPPED == step.state
示例#4
0
def test_run_debug_step_function_with_exception(
    debug_or_run, mocker, mock_utils_debugger
):
    """
    Test running/debugging a Step which raises an Exception
    """
    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )
    step.definition_func = StepHelper.step_fail_func
    step.argument_match = mocker.MagicMock()
    step.argument_match.evaluate.return_value = (tuple(), {})

    # when
    method = getattr(step, debug_or_run)
    state = method()

    # then
    assert state == step.state == Step.State.FAILED
    assert step.failure is not None
    assert step.failure.reason == "failing step"
    assert step.failure.name == "AssertionError"
示例#5
0
def test_run_debug_step_function_with_posargs(
    debug_or_run, mocker, mock_utils_debugger
):
    """
    Test running/debugging a Step with a function and positional arguments
    """
    # mock step function which is to use
    mocker.spy(StepHelper, "step_func")

    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )
    step.definition_func = StepHelper.step_func
    step.argument_match = mocker.MagicMock()
    step.argument_match.evaluate.return_value = ((1, 2), {})

    # when
    method = getattr(step, debug_or_run)
    state = method()

    # then
    assert state == Step.State.PASSED
    StepHelper.step_func.assert_called_once_with(step, 1, 2)
示例#6
0
def test_run_step_with_invalid_defintion_func():
    """
    Test running a Step with an invalid definition function
    """
    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )

    # when the Step doesn't have a definition function
    with pytest.raises(RadishError) as exc:
        step.run()
    # then the Step fails with an Exception
    assert str(exc.value) == "The step 'I am a Step' does not have a step definition"

    # when the Step has a non-callable definition function
    step.definition_func = "not-callable"
    with pytest.raises(RadishError) as exc:
        step.run()
    # then the step fails with an Exception
    assert str(exc.value) == "The step 'I am a Step' does not have a step definition"
示例#7
0
    def test_running_a_feature(self):
        """
            Test running a feature
        """
        data = threading.local()
        data.step_was_called = False

        def some_step(step):
            data.step_was_called = True

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

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

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

        hook_mock = Mock()
        hook_mock.call.return_value = True
        runner = Runner(hook_mock)
        runner.run_feature(feature)
        step.state.should.be.equal(Step.State.PASSED)
        data.step_was_called.should.be.true
示例#8
0
def test_run_step_with_invalid_defintion_func():
    """
    Test running a Step with an invalid definition function
    """
    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )

    # when the Step doesn't have a definition function
    with pytest.raises(RadishError) as exc:
        step.run()
    # then the Step fails with an Exception
    assert str(
        exc.value) == "The step 'I am a Step' does not have a step definition"

    # when the Step has a non-callable definition function
    step.definition_func = "not-callable"
    with pytest.raises(RadishError) as exc:
        step.run()
    # then the step fails with an Exception
    assert str(
        exc.value) == "The step 'I am a Step' does not have a step definition"
示例#9
0
def test_run_debug_step_function_mark_skipped(debug_or_run, mocker,
                                              mock_utils_debugger):
    """
    Test running/debugging a Step which marks itself as skipped
    """
    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )
    step.definition_func = StepHelper.step_skip_func
    step.argument_match = mocker.MagicMock()
    step.argument_match.evaluate.return_value = (tuple(), {})

    # when
    method = getattr(step, debug_or_run)
    state = method()

    # then
    assert state == Step.State.SKIPPED == step.state
示例#10
0
    def test_running_all(self):
        """
            Test running a all features
        """
        data = threading.local()
        data.step_was_called = False

        def some_step(step):
            data.step_was_called = True

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

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

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

        hook_mock = Mock()
        hook_mock.call.return_value = True
        runner = Runner(hook_mock)
        runner.start([feature], None)
        step.state.should.be.equal(Step.State.PASSED)
        data.step_was_called.should.be.true
示例#11
0
    def test_run_step_with_keyword_arguments_passed(self):
        """
            Test running a passing step with keyword arguments
        """
        data = threading.local()
        data.step_was_run = False
        data.number = None
        data.string = None

        def step_passed(step, number, string):
            data.step_was_run = True
            data.number = int(number)
            data.string = string

        step = Step(1, "I call a passing step with string argument 'Tschau' and number argument 42", "somefile.feature", 3, None, True)
        step.definition_func = step_passed
        match = re.search("I call a passing step with string argument '(?P<string>.*?)' and number argument (?P<number>\d+)", step.sentence)
        step.arguments = match.groups()
        step.keyword_arguments = match.groupdict()

        step.state.should.be.equal(Step.State.UNTESTED)
        step.run.when.called_with().should.return_value(Step.State.PASSED)
        data.step_was_run.should.be.true
        data.number.should.be.equal(42)
        data.string.should.be.equal("Tschau")
示例#12
0
def test_run_debug_step_function_with_exception(debug_or_run, mocker,
                                                mock_utils_debugger):
    """
    Test running/debugging a Step which raises an Exception
    """
    # given
    step = Step(
        1,
        "I am a Step",
        "foo.feature",
        1,
        parent=None,
        runable=True,
        context_class=None,
    )
    step.definition_func = StepHelper.step_fail_func
    step.argument_match = mocker.MagicMock()
    step.argument_match.evaluate.return_value = (tuple(), {})

    # when
    method = getattr(step, debug_or_run)
    state = method()

    # then
    assert state == step.state == Step.State.FAILED
    assert step.failure is not None
    assert step.failure.reason == "failing step"
    assert step.failure.name == "AssertionError"
示例#13
0
    def test_running_a_scenario(self):
        """
            Test running a scenario
        """
        data = threading.local()
        data.step_was_called = False

        def some_step(step):
            data.step_was_called = True

        step = Step(1, "Some step", "somefile.feature", 3, None, True)
        step.definition_func = some_step
        step.arguments = tuple()
        step.keyword_arguments = {}

        scenario = Scenario(1, 1, "Scenario", "Some scenario", "somefile.feature", 2, None)
        scenario.steps.append(step)

        hook_mock = Mock()
        hook_mock.call.return_value = True
        runner = Runner(hook_mock)
        returncode = runner.run_scenario(scenario)
        returncode.should.be.equal(0)
        step.state.should.be.equal(Step.State.PASSED)
        data.step_was_called.should.be.true
示例#14
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
        argument_match_mock = Mock()
        argument_match_mock.evaluate.return_value = (tuple(), {})
        step1.argument_match = argument_match_mock
        scenario.steps.append(step1)

        step2 = Step(2, "Some failed step", "somefile.feature", 4, scenario,
                     True)
        step2.definition_func = some_failed_step
        argument_match_mock = Mock()
        argument_match_mock.evaluate.return_value = (tuple(), {})
        step2.argument_match = argument_match_mock
        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)
示例#15
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)
示例#16
0
    def test_run_step_passed(self):
        """
            Test running a passing step
        """
        data = threading.local()
        data.step_was_run = False

        def step_passed(step):
            data.step_was_run = True

        step = Step(1, "I call a passing step", "somefile.feature", 3, None, True)
        step.definition_func = step_passed
        step.arguments = re.search(step.sentence, step.sentence).groups()

        step.state.should.be.equal(Step.State.UNTESTED)
        step.run.when.called_with().should.return_value(Step.State.PASSED)
        data.step_was_run.should.be.true
示例#17
0
def test_run_debug_step_function_with_kwargs(debug_or_run, mocker, mock_utils_debugger):
    """
    Test running/debugging a Step with a function and keyword arguments
    """
    # mock step function which is to use
    mocker.spy(StepHelper, 'step_func')

    # given
    step = Step(1, 'I am a Step', 'foo.feature', 1, parent=None, runable=True, context_class=None)
    step.definition_func = StepHelper.step_func
    step.argument_match = mocker.MagicMock()
    step.argument_match.evaluate.return_value = (tuple(), {'foo': '1', 'bar': '2'})

    # when
    method = getattr(step, debug_or_run)
    state = method()

    # then
    assert state == Step.State.PASSED
    StepHelper.step_func.assert_called_once_with(step, foo='1', bar='2')
示例#18
0
    def test_run_step_failed(self):
        """
            Test running a failing step
        """
        data = threading.local()
        data.step_was_run = False

        def step_failed(step):
            data.step_was_run = True
            assert False, "This step fails by design"

        step = Step(1, "I call a failing step", "somefile.feature", 3, None, True)
        step.definition_func = step_failed
        step.arguments = re.search(step.sentence, step.sentence).groups()

        step.state.should.be.equal(Step.State.UNTESTED)
        step.run.when.called_with().should.return_value(Step.State.FAILED)
        step.failure.shouldnt.be.none
        step.failure.reason.should.be.equal("This step fails by design")
        data.step_was_run.should.be.true
示例#19
0
    def test_running_a_step(self):
        """
            Test running a step
        """
        data = threading.local()
        data.step_was_called = False

        def some_step(step):
            data.step_was_called = True

        step = Step(1, "Some step", "somefile.feature", 3, None, True)
        step.definition_func = some_step
        step.arguments = tuple()
        step.keyword_arguments = {}

        hook_mock = Mock()
        hook_mock.call.return_value = True
        runner = Runner(hook_mock)
        returncode = runner.run_step(step)
        returncode.should.be.equal(0)
        step.state.should.be.equal(Step.State.PASSED)
        data.step_was_called.should.be.true
示例#20
0
    def test_running_a_step(self):
        """
            Test running a step
        """
        data = threading.local()
        data.step_was_called = False

        def some_step(step):
            data.step_was_called = True

        step = Step(1, "Some step", "somefile.feature", 3, None, True)
        step.definition_func = some_step
        argument_match_mock = Mock()
        argument_match_mock.evaluate.return_value = (tuple(), {})
        step.argument_match = argument_match_mock

        hook_mock = Mock()
        hook_mock.call.return_value = True
        runner = Runner(hook_mock)
        returncode = runner.run_step(step)
        returncode.should.be.equal(0)
        step.state.should.be.equal(Step.State.PASSED)
        data.step_was_called.should.be.true