def test_before_each_feature(self): """ Test before.each_feature from extension console_writer """ HookRegistry() data = threading.local() data.console = None def patched_write(text): text = re.sub(r"\x1b[^m]*m", "", text) data.console = text feature = Feature(1, "Feature", "Some feature", "somefile.feature", 1) with patch("radish.extensions.console_writer.write", side_effect=patched_write): HookRegistry().call("before", "each_feature", feature) data.console.should.be.equal( "Feature: Some feature # somefile.feature") feature.description.append("This is some description") feature.description.append("Because I want to test it") with patch("radish.extensions.console_writer.write", side_effect=patched_write): HookRegistry().call("before", "each_feature", feature) data.console.should.be.equal( """Feature: Some feature # somefile.feature This is some description Because I want to test it""")
def reset_registries(): """ Fixture to automatically reset singleton registries """ StepRegistry().clear() HookRegistry().reset() ExtensionRegistry().reset()
def test_hookregistry_call_hook_func_if_tag_matched(mocker): # given registry = HookRegistry() to_be_called_hook_func = mocker.MagicMock( name="before_each_scenario_func_tagged") not_to_be_called_hook = mocker.MagicMock(name="before_each_scenario_func") registry.register( what="each_scenario", when="before", func=to_be_called_hook_func, on_tags=["some-tag"], order=1, ) registry.register( what="each_scenario", when="before", func=not_to_be_called_hook, on_tags=["another-tag"], order=1, ) tagged_model = mocker.MagicMock(name="TaggedModel") tagged_model.get_all_tags.return_value = [ Tag(name="some-tag", path="", line=0) ] # when registry.call("each_scenario", "before", False, tagged_model) # then to_be_called_hook_func.assert_has_calls([mock.call(mock.ANY)]) not_to_be_called_hook.assert_has_calls([])
def test_hookregistry_call_hook_func_if_match(mocker): # given registry = HookRegistry() to_be_called_hook_func = mocker.MagicMock(name="before_each_scenario_func") not_to_be_called_hook = mocker.MagicMock(name="after_each_scenario_func") registry.register( what="each_scenario", when="before", func=to_be_called_hook_func, on_tags=[], order=1, ) registry.register( what="each_scenario", when="after", func=not_to_be_called_hook, on_tags=[], order=1, ) # when registry.call("each_scenario", "before", False, mocker.MagicMock(name="TaggedModel")) # then to_be_called_hook_func.assert_has_calls([mock.call(mock.ANY)]) not_to_be_called_hook.assert_has_calls([])
def test_after_each_step(self): """ Test after.each_step 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_mock = Mock() scenario_mock.parent = None step = Step(1, "I test the console writer", "somefile.feature", 3, scenario_mock, False) step.parent = Mock() step.parent.id = 1 step.parent.parent = Mock(spec=Feature) step.parent.parent.id = 1 with patch("radish.extensions.console_writer.write", side_effect=patched_write): HookRegistry().call("after", "each_step", step) data.console.should.be.equal("\rI test the console writer")
def test_after_each_step_failed(self): """ Test after.each_step from extension console_writer with failed step """ data = threading.local() data.console = None def patched_write(text): text = re.sub(r"\x1b[^m]*m", "", text) data.console = text scenario_mock = Mock() scenario_mock.parent = None step = Step(1, "I test the console writer", "somefile.feature", 3, scenario_mock, False) step.parent = MagicMock() step.parent.id = 1 step.parent.parent = Mock(spec=Feature) step.parent.parent.id = 1 step.state = step.State.FAILED try: assert False, "Some assertion happend" except AssertionError as e: step.failure = utils.Failure(e) with patch("radish.extensions.console_writer.write", side_effect=patched_write): HookRegistry().call("after", "each_step", step) data.console.should.be.equal("""\rI test the console writer AssertionError: Some assertion happend""")
def test_calling_registered_hooks(self): """ Test calling registered hooks """ hookregistry = HookRegistry() data = threading.local() data.called_hooked_a = False data.called_hooked_b = False data.called_hooked_c = False def hooked_a(): data.called_hooked_a = True def hooked_b(): data.called_hooked_b = True def hooked_c(): data.called_hooked_c = True before.each_feature(hooked_a) after.each_step(hooked_b) before.each_feature(hooked_c) hookregistry.call("before", "each_feature") data.called_hooked_a.should.be.true data.called_hooked_b.should.be.false data.called_hooked_c.should.be.true hookregistry.call("after", "each_step") data.called_hooked_a.should.be.true data.called_hooked_b.should.be.true data.called_hooked_c.should.be.true
def test_register_as_hook(self): """ Test registering function as hook """ empty_hook = {"before": [], "after": []} hookregistry = HookRegistry() hookregistry.hooks.should.have.length_of(4) hookregistry.hooks["all"].should.be.equal(empty_hook) hookregistry.hooks["each_feature"].should.be.equal(empty_hook) hookregistry.hooks["each_scenario"].should.be.equal(empty_hook) hookregistry.hooks["each_step"].should.be.equal(empty_hook) hookregistry.hooks["all"]["before"].should.have.length_of(0) hookregistry.register("before", "all", "some_func") hookregistry.hooks["all"]["before"].should.have.length_of(1) hookregistry.hooks["all"]["after"].should.have.length_of(0) hookregistry.hooks["each_feature"]["before"].should.have.length_of(0) hookregistry.register("before", "each_feature", "some_func") hookregistry.hooks["each_feature"]["before"].should.have.length_of(1) hookregistry.hooks["each_feature"]["after"].should.have.length_of(0) hookregistry.hooks["each_scenario"]["before"].should.have.length_of(0) hookregistry.register("before", "each_scenario", "some_func") hookregistry.hooks["each_scenario"]["before"].should.have.length_of(1) hookregistry.hooks["each_scenario"]["after"].should.have.length_of(0) hookregistry.hooks["each_step"]["before"].should.have.length_of(0) hookregistry.register("before", "each_step", "some_func") hookregistry.hooks["each_step"]["before"].should.have.length_of(1) hookregistry.hooks["each_step"]["after"].should.have.length_of(0)
def test_calling_registered_hooks_with_arguments(self): """ Test calling registered hooks with arguments """ hookregistry = HookRegistry() data = threading.local() data.hooked_a_feature = None data.hooked_b_step = None data.hooked_c_feature = None def hooked_a(feature): data.hooked_a_feature = feature def hooked_b(step): data.hooked_b_step = step def hooked_c(feature): data.hooked_c_feature = feature before.each_feature(hooked_a) after.each_step(hooked_b) before.each_feature(hooked_c) hookregistry.call("before", "each_feature", "this is a feature") data.hooked_a_feature = "this is a feature" data.hooked_b_step = None data.hooked_c_feature = "this is a feature" hookregistry.call("after", "each_step", "this is a step") data.hooked_a_feature = "this is a feature" data.hooked_b_step = "this is a step" data.hooked_c_feature = "this is a feature"
def tearDown(self): """ Tear down the test Delete all singletons """ StepRegistry().clear() HookRegistry().reset()
def test_initializing_hook_registry(self): """ Test the initialization of the hook registry """ empty_hook = {"before": [], "after": []} hookregistry = HookRegistry() hookregistry.hooks.should.have.length_of(4) hookregistry.hooks["all"].should.be.equal(empty_hook) hookregistry.hooks["each_feature"].should.be.equal(empty_hook) hookregistry.hooks["each_scenario"].should.be.equal(empty_hook) hookregistry.hooks["each_step"].should.be.equal(empty_hook) HookRegistry.Hook.should.have.property("all") HookRegistry.Hook.should.have.property("each_feature") HookRegistry.Hook.should.have.property("each_scenario") HookRegistry.Hook.should.have.property("each_step")
def test_decorating_with_hook(self): """ Test decorating function as hook """ hookregistry = HookRegistry() def some_func(): pass hookregistry.hooks["each_feature"]["before"].should.have.length_of(0) before.each_feature(some_func) hookregistry.hooks["each_feature"]["before"].should.have.length_of(1) hookregistry.hooks["each_feature"]["before"][0].should.be.equal( some_func) hookregistry.hooks["each_step"]["after"].should.have.length_of(0) after.each_step(some_func) hookregistry.hooks["each_step"]["after"].should.have.length_of(1) hookregistry.hooks["each_step"]["after"][0].should.be.equal(some_func)
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 hookregistry(): """ Fixture to create and get a clean HookRegistry instance. """ registry = HookRegistry() yield registry
def tearDownClass(cls): """ Reset hook registry """ HookRegistry().reset()