def test_scenario_all_steps(self): """ Test getting all steps from scenario """ step_1 = Mock(state=Step.State.UNTESTED) step_2 = Mock(state=Step.State.UNTESTED) step_3 = Mock(state=Step.State.UNTESTED) scenario = Scenario(1, "Scenario", "Some scenario", None, None, None) scenario.steps.extend([step_1, step_2, step_3]) 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) precond_step_1 = Mock(state=Step.State.UNTESTED) precond_step_2 = Mock(state=Step.State.UNTESTED) scenario_precond = Scenario(2, "Scenario", "Some precond scenario", None, None, None) scenario_precond.steps.extend([precond_step_1, precond_step_2]) scenario.preconditions.append(scenario_precond) scenario.all_steps.should.have.length_of(5) scenario.all_steps[0].should.be.equal(precond_step_1) scenario.all_steps[1].should.be.equal(precond_step_2) scenario.all_steps[2].should.be.equal(step_1) scenario.all_steps[3].should.be.equal(step_2) scenario.all_steps[4].should.be.equal(step_3)
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)
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
def test_scenario_state_with_background(mocker): """ Test getting the Scenario state according to it's Steps states including a Background """ # given background = mocker.MagicMock(steps=[]) scenario = Scenario( 1, "Scenario", "I am a Scenario", "foo.feature", 1, parent=None, tags=None, preconditions=None, background=background, ) # add Steps to this Scenario scenario.steps.extend([mocker.MagicMock(state=Step.State.PASSED)]) # add Steps to the background background.steps.extend([ mocker.MagicMock(state=Step.State.PASSED), mocker.MagicMock(state=Step.State.PASSED), mocker.MagicMock(state=Step.State.PASSED), ]) # get the step to modify step = background.steps[1] # when all Steps are passed the Scenario is passed assert scenario.state == Step.State.PASSED # when a Background Step is failed the Scenario is failed step.state = Step.State.FAILED assert scenario.state == Step.State.FAILED
def test_scenario_state(self): """ Test scenario's state """ step_1 = Mock(state=Step.State.UNTESTED) step_2 = Mock(state=Step.State.UNTESTED) step_3 = Mock(state=Step.State.UNTESTED) scenario = Scenario(1, "Scenario", "Some scenario", None, None, None) scenario.steps.extend([step_1, step_2, step_3]) scenario.state.should.be.equal(Step.State.UNTESTED) step_1.state = Step.State.SKIPPED scenario.state.should.be.equal(Step.State.SKIPPED) step_1.state = Step.State.FAILED scenario.state.should.be.equal(Step.State.FAILED) step_2.state = Step.State.PASSED scenario.state.should.be.equal(Step.State.FAILED) step_3.state = Step.State.PASSED scenario.state.should.be.equal(Step.State.FAILED) step_1.state = Step.State.PASSED scenario.state.should.be.equal(Step.State.PASSED)
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
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
def test_creating_simple_scenario(): """ Test creating a simple Scenario """ # given & when scenario = Scenario( 1, "Scenario", "I am a Scenario", "foo.feature", 1, parent=None, tags=None, preconditions=None, background=None, ) # then assert scenario.id == 1 assert scenario.keyword == "Scenario" assert scenario.sentence == "I am a Scenario" assert scenario.path == "foo.feature" assert scenario.line == 1 assert scenario.parent is None assert scenario.tags == [] assert scenario.preconditions == [] assert scenario.background is None
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", ))
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')
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
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)
def test_merge_non_existing_step(self): """ Test merging non existing step """ 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) 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")
def test_scenario_has_to_run(self): """ Test scenario's has to run functionality """ feature = Mock() s = Scenario(1, "Scenario", "Some scenario", None, None, feature) s.absolute_id = 1 s.has_to_run.when.called_with(None).should.return_value(True) s.has_to_run.when.called_with([1]).should.return_value(True) s.has_to_run.when.called_with([1, 2]).should.return_value(True) s.has_to_run.when.called_with([2]).should.return_value(False)
def test_scenario_constants(self): """ Test scenario's constants """ feature = Mock(constants=[("feature_var_1", "1"), ("feature_var_2", "2")]) scenario = Scenario(1, "Scenario", "Some scenario", None, None, feature) scenario.context.constants.extend([("scenario_var_1", "3"), ("scenario_var_2", "4")]) scenario.constants.should.have.length_of(4) scenario.constants[0].should.be.equal(("scenario_var_1", "3")) scenario.constants[1].should.be.equal(("scenario_var_2", "4")) scenario.constants[2].should.be.equal(("feature_var_1", "1")) scenario.constants[3].should.be.equal(("feature_var_2", "2"))
def test_scenario_failed_step(mocker): """ Test getting the first failed Step from a Scenario """ # given background = mocker.MagicMock(steps=[]) scenario = Scenario( 1, "Scenario", "I am a Scenario", "foo.feature", 1, parent=None, tags=None, preconditions=None, background=background, ) # when # add Steps to this Scenario scenario.steps.extend([mocker.MagicMock(state=Step.State.PASSED)]) # add Steps to the Background background.steps.extend([ mocker.MagicMock(state=Step.State.PASSED), mocker.MagicMock(state=Step.State.PASSED), mocker.MagicMock(state=Step.State.PASSED), ]) # when no Step failed assert scenario.failed_step is None # when a Scenario Step fails it should be returned scenario.steps[0].state = Step.State.FAILED assert scenario.failed_step == scenario.steps[0] scenario.steps[0].state = Step.State.PASSED # when a Background Step fails it should be returned background.steps[0].state = Step.State.FAILED assert scenario.failed_step == background.steps[0] background.steps[0].state = Step.State.PASSED # when a Background and a Scenario Step fails the # Background Step should be returned background.steps[0].state = Step.State.FAILED scenario.steps[0].state = Step.State.FAILED assert scenario.failed_step == background.steps[0]
def test_scenario_state(mocker): """ Test getting the Scenario state according to it's Steps states """ # given scenario = Scenario( 1, "Scenario", "I am a Scenario", "foo.feature", 1, parent=None, tags=None, preconditions=None, background=None, ) # add Steps to this Scenario scenario.steps.extend([ mocker.MagicMock(state=Step.State.PASSED), mocker.MagicMock(state=Step.State.PASSED), mocker.MagicMock(state=Step.State.PASSED), ]) # get the step to modify step = scenario.steps[1] # when all Steps are passed the Scenario is passed assert scenario.state == Step.State.PASSED # when one Step is failed the Scenario is failed step.state = Step.State.FAILED assert scenario.state == Step.State.FAILED # when one Step is pending the Scenario is pending step.state = Step.State.PENDING assert scenario.state == Step.State.PENDING # when one Step is skipped the Scenario is skipped step.state = Step.State.SKIPPED assert scenario.state == Step.State.SKIPPED # when one Step is untested the Scenario is untested step.state = Step.State.UNTESTED assert scenario.state == Step.State.UNTESTED
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)
def test_scenario_get_failed_step(self): """ Test getting first failed step from scenario """ step_1 = Mock(state=Step.State.UNTESTED) step_2 = Mock(state=Step.State.UNTESTED) step_3 = Mock(state=Step.State.UNTESTED) scenario = Scenario(1, "Scenario", "Some scenario", None, None, None) scenario.steps.extend([step_1, step_2, step_3]) scenario.failed_step.should.be.equal(None) step_1.state = Step.State.FAILED scenario.failed_step.should.be.equal(step_1) step_1.state = Step.State.PASSED scenario.failed_step.should.be.equal(None) step_2.state = Step.State.FAILED scenario.failed_step.should.be.equal(step_2)
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")
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
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")
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)