Best Python code snippet using slash
test_plugins.py
Source:test_plugins.py
1# pylint: disable=unused-argument2import os3import gossip4import pytest5import slash6from slash import plugins7from slash.plugins import IncompatiblePlugin, PluginInterface8from .utils import NamedPlugin, maybe_decorate9def test_registers_on_none(restore_plugins_on_cleanup, checkpoint):10 @slash.plugins.active # pylint: disable=unused-variable11 class SamplePlugin(PluginInterface):12 def get_name(self):13 return 'sample'14 @plugins.registers_on(None)15 def some_method_here(self):16 checkpoint()17 gossip.trigger('slash.some_method_here')18 assert not checkpoint.called19@pytest.mark.parametrize('class_level_needs', [True, False])20@pytest.mark.parametrize('class_level_provides', [True, False])21def test_registers_on_kwargs(class_level_needs, class_level_provides):22 needs_decorator = plugins.needs('other_requirement')23 provides_decorator = plugins.provides('another_provided_requirement')24 @slash.plugins.active # pylint: disable=unused-variable25 @maybe_decorate(needs_decorator, class_level_needs)26 @maybe_decorate(provides_decorator, class_level_provides)27 class SamplePlugin(PluginInterface):28 def get_name(self):29 return 'sample'30 @plugins.registers_on('some.hook', provides=['provided_requirement'], needs=['some_requirement'], tags=['tag'])31 @maybe_decorate(needs_decorator, not class_level_needs)32 @maybe_decorate(provides_decorator, not class_level_provides)33 def plugin_method(self):34 pass35 @gossip.register('some.hook', provides=['some_requirement', 'other_requirement'])36 def _unused():37 pass38 gossip.trigger('some.hook')39 hook = gossip.get_hook('some.hook')40 [registration] = [reg for reg in hook.get_registrations() if reg.func.__name__ == 'plugin_method']41 assert registration.tags == {'tag'}42 assert registration.needs == frozenset(['some_requirement', 'other_requirement'])43 assert registration.provides == frozenset(['provided_requirement', 'another_provided_requirement'])44def test_registers_on_with_private_methods(restore_plugins_on_cleanup, checkpoint):45 @slash.plugins.active # pylint: disable=unused-variable46 class SamplePlugin(PluginInterface):47 def get_name(self):48 return 'sample'49 @plugins.registers_on('some_hook')50 def _handler(self):51 checkpoint()52 assert not checkpoint.called53 gossip.trigger('some_hook')54 assert checkpoint.called55def test_class_variables_allowed(restore_plugins_on_cleanup):56 @slash.plugins.active # pylint: disable=unused-variable57 class SamplePlugin(PluginInterface):58 ATTRIBUTE = 'some_value'59 def get_name(self):60 return 'sample'61def test_active_decorator(restore_plugins_on_cleanup):62 plugins.manager.uninstall_all()63 @slash.plugins.active64 class SamplePlugin(PluginInterface):65 def get_name(self):66 return 'sample'67 assert isinstance(SamplePlugin, type)68 assert issubclass(SamplePlugin, PluginInterface)69 [active] = plugins.manager.get_active_plugins().values()70 assert isinstance(active, SamplePlugin)71@pytest.mark.parametrize('is_internal', [True, False])72def test_custom_hook_registration(request, is_internal):73 hook_name = 'some_hook'74 with pytest.raises(LookupError):75 gossip.get_hook(hook_name)76 class MyPlugin(PluginInterface):77 def get_name(self):78 return "plugin"79 @plugins.registers_on(hook_name)80 def unknown(self):81 pass82 p = MyPlugin()83 plugins.manager.install(p, activate=True, is_internal=is_internal)84 @request.addfinalizer85 def cleanup(): # pylint: disable=unused-variable86 plugins.manager.uninstall(p)87 registrations = gossip.get_hook(hook_name).get_registrations()88 assert len(registrations) == 189 [r] = registrations90 assert r.func.__func__ is MyPlugin.unknown91 # make sure we deactivate properly as well92 plugins.manager.deactivate(p)93 assert not gossip.get_hook(hook_name).get_registrations()94def test_multiple_registers_on(request):95 hook_names = ['some_hook_{}'.format(i) for i in range(2)]96 class MyPlugin(PluginInterface):97 def get_name(self):98 return "plugin"99 @plugins.registers_on(hook_names[0])100 @plugins.registers_on(hook_names[1])101 def unknown(self):102 pass103 expected_func = MyPlugin.unknown104 p = MyPlugin()105 plugins.manager.install(p, activate=True)106 @request.addfinalizer107 def cleanup(): # pylint: disable=unused-variable108 plugins.manager.uninstall(p)109 for hook_name in hook_names:110 registrations = gossip.get_hook(hook_name).get_registrations()111 assert len(registrations) == 1112 assert registrations[0].func.__func__ is expected_func113 plugins.manager.deactivate(p)114 for hook_name in hook_names:115 assert not gossip.get_hook(hook_name).get_registrations()116def test_register_invalid_hook():117 initially_installed = list(plugins.manager.get_installed_plugins())118 class MyPlugin(PluginInterface):119 def get_name(self):120 return "plugin"121 def unknown(self):122 pass123 with pytest.raises(IncompatiblePlugin):124 plugins.manager.install(MyPlugin(), activate=True)125 assert set(plugins.manager.get_installed_plugins()) == set(initially_installed)126def test_register_custom_hooks_strict_group():127 initially_installed = list(plugins.manager.get_installed_plugins())128 hook_name = "some_group.some_hook"129 gossip.get_or_create_group("some_group").set_strict()130 class MyPlugin(PluginInterface):131 def get_name(self):132 return "plugin"133 @plugins.registers_on(hook_name)134 def unknown(self):135 pass136 with pytest.raises(IncompatiblePlugin):137 plugins.manager.install(MyPlugin(), activate=True)138 assert list(plugins.manager.get_installed_plugins()) == initially_installed139def test_builtin_plugins_hooks_start_condition():140 "make sure that all hooks are either empty, or contain callbacks marked with `slash.<identifier>`"141 for hook in gossip.get_group('slash').get_hooks():142 for registration in hook.get_registrations():143 assert registration.token.startswith('slash.'), 'Callback {} is not a builtin!'.format(hook.full_name)144def test_builtin_plugins_are_installed():145 installed = plugins.manager.get_installed_plugins()146 assert installed147 for filename in os.listdir(os.path.join(os.path.dirname(plugins.__file__), "builtin")):148 if filename.startswith("_") or filename.startswith(".") or not filename.endswith(".py"):149 continue150 plugin_name = filename[:(-3)].replace('_', ' ')151 assert plugin_name in installed152@pytest.mark.usefixtures('disable_vintage_deprecations')153def test_get_installed_plugins():154 class CustomPlugin(PluginInterface):155 def __init__(self, name):156 super(CustomPlugin, self).__init__()157 self._name = name158 def get_name(self):159 return self._name160 some_plugin = CustomPlugin('some-plugin')161 internal_plugin = CustomPlugin('internal-plugin')162 plugins.manager.install(some_plugin)163 plugins.manager.install(internal_plugin, is_internal=True)164 assert some_plugin.get_name() in plugins.manager.get_installed_plugins(include_internals=True)165 assert some_plugin.get_name() in plugins.manager.get_installed_plugins(include_internals=False)166 assert internal_plugin.get_name() in plugins.manager.get_installed_plugins(include_internals=True)167 assert internal_plugin.get_name() not in plugins.manager.get_installed_plugins(include_internals=False)168def test_cannot_install_incompatible_subclasses(no_plugins):169 class Incompatible(object):170 pass171 for invalid in (Incompatible, Incompatible(), PluginInterface, object(), 1, "string"):172 with pytest.raises(IncompatiblePlugin):173 plugins.manager.install(invalid)174 assert plugins.manager.get_installed_plugins() == {}175def test_install_uninstall(no_plugins):176 plugin_name = "some plugin name"177 class CustomPlugin(PluginInterface):178 def get_name(self):179 return plugin_name180 with pytest.raises(LookupError):181 plugins.manager.get_plugin(plugin_name)182 plugin = CustomPlugin()183 plugins.manager.install(plugin)184 assert plugins.manager.get_plugin(plugin_name) is plugin185 plugins.manager.uninstall(plugin)186 with pytest.raises(LookupError):187 plugins.manager.get_plugin(plugin_name)188@pytest.mark.parametrize('cond', [True, False])189def test_register_if(no_plugins, checkpoint, cond):190 @slash.plugins.active # pylint: disable=unused-variable191 class CustomPlugin(NamedPlugin):192 @slash.plugins.register_if(cond)193 def test_start(self):194 checkpoint()195 slash.hooks.test_start() # pylint: disable=no-member196 assert checkpoint.called == cond197def test_register_if_nonexistent_hook(no_plugins, checkpoint):198 @slash.plugins.active # pylint: disable=unused-variable199 class CustomPlugin(NamedPlugin):200 @slash.plugins.register_if(False)201 def nonexistent_hook(self):202 checkpoint()203def test_restoring_state_context():204 class Plugin1(NamedPlugin):205 pass206 @slash.plugins.active207 class Plugin2(NamedPlugin):208 pass209 class Plugin3(NamedPlugin):210 pass211 @slash.plugins.active212 class Plugin4(NamedPlugin):213 pass214 class Plugin5(NamedPlugin):215 pass216 manager = slash.plugins.manager217 manager.install(Plugin3())218 installed = manager.get_installed_plugins().copy()219 active = manager.get_active_plugins().copy()220 with manager.restoring_state_context():221 manager.install(Plugin1())222 manager.uninstall(Plugin2)223 manager.activate('Plugin3')224 manager.deactivate(Plugin4)225 manager.install(Plugin5(), activate=True)226 assert set(manager.get_installed_plugins()) == set(installed)\227 .union({'Plugin1', 'Plugin5'})\228 .difference({'Plugin2'})229 assert set(manager.get_active_plugins()) == set(active).union({'Plugin3', 'Plugin5'}).difference({'Plugin2', 'Plugin4'})230 assert manager.get_installed_plugins() == installed...
conftest.py
Source:conftest.py
...200 else:201 raise NotImplementedError() # pragma: no cover202 return getter203@pytest.fixture204def restore_plugins_on_cleanup(request):205 request.addfinalizer(slash.plugins.manager.install_builtin_plugins)206 request.addfinalizer(slash.plugins.manager.uninstall_all)207@pytest.fixture208def logs_dir(config_override, tmpdir, relative_symlinks):209 returned = tmpdir.join('logs')210 config_override("log.root", str(returned.join("files")))211 config_override("log.last_session_symlink",212 str("../links/last-session" if relative_symlinks else returned.join("links", "last-session")))213 config_override("log.last_session_dir_symlink",214 str("../links/last-session-dir" if relative_symlinks else returned.join("links", "last-session-dir")))215 config_override("log.last_test_symlink",216 str("../links/last-test" if relative_symlinks else returned.join("links", "last-test")))217 config_override("log.last_failed_symlink",218 str("../links/last-failed" if relative_symlinks else returned.join("links", "last-failed")))...
test_plugin_errors.py
Source:test_plugin_errors.py
1# pylint: disable=unused-argument, unused-variable, redefined-outer-name2import slash3import gossip4import pytest5from .utils import NamedPlugin, CustomException, make_runnable_tests6@pytest.mark.parametrize('has_start', [True, False])7def test_plugin_end_not_called_when_start_not_called(gossip_raise_immediately, has_start, checkpoint, restore_plugins_on_cleanup):8 errors = []9 class CustomPlugin(NamedPlugin):10 pass11 class RaisingPlugin(NamedPlugin):12 pass13 if has_start:14 def start(self):15 errors.append('start hook called')16 setattr(CustomPlugin, 'session_start', start)17 def end(self):18 errors.append('end hook called')19 setattr(CustomPlugin, 'session_end', end)20 def do_raise(self):21 checkpoint()22 raise CustomException()23 setattr(RaisingPlugin, 'session_start', do_raise)24 slash.plugins.manager.install(RaisingPlugin(), activate=True)25 slash.plugins.manager.install(CustomPlugin(), activate=True)26 with slash.Session() as s:27 with pytest.raises(CustomException):28 with s.get_started_context():29 def test_something():30 pass31 slash.runner.run_tests(make_runnable_tests(test_something))32 assert checkpoint.called33 assert not errors34@pytest.mark.parametrize('illegal_char', ['\t', '\n', '$', '(', '[', '{', '='])35def test_plugin_with_illegal_name(illegal_char):36 class PluginWithIllegalName(slash.plugins.PluginInterface):37 def __init__(self):38 self._name = 'ab' + illegal_char39 def get_name(self):40 return self._name41 plugin = PluginWithIllegalName()42 with pytest.raises(slash.plugins.IllegalPluginName):43 slash.plugins.manager.install(plugin)44def test_plugin_with_no_session_start_gets_called_session_end(checkpoint):45 @slash.plugins.active46 class MyPlugin(NamedPlugin):47 def session_end(self):48 checkpoint()49 with slash.Session() as s:50 with s.get_started_context():51 pass52 assert checkpoint.called53@pytest.fixture54def gossip_raise_immediately():55 g = gossip.get_group('slash')56 prev = g.get_exception_policy()57 g.set_exception_policy(gossip.RaiseImmediately())58 try:59 yield60 finally:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!