Best Python code snippet using Contexts
integration_tests.py
Source:integration_tests.py  
...23        self.plugin.import_module.return_value = self.module24        self.plugin.identify_class.return_value = CONTEXT25        self.plugin.identify_method.return_value = ASSERTION26    def because_we_run_with_the_plugin(self):27        contexts.run_with_plugins([self.plugin])28    def it_should_run_the_spec_in_the_file(self):29        assert self.ran30    def cleanup_the_file_system(self):31        os.remove(self.filename)32    def write_file(self):33        self.filename = os.path.join(TEST_DATA_DIR, self.module_name + ".py")34        with open(self.filename, 'w+') as f:35            f.write('')36class WhenRunningAFile:37    def establish_that_there_is_a_file_in_the_filesystem(self):38        self.module_name = "test_file"39        self.write_file()40        self.module = types.ModuleType(self.module_name)41        class When:42            ran = False43            def it(self):44                self.__class__.ran = True45        self.module.When = When46        self.not_implemented_plugin = mock.Mock(wraps=PluginInterface())47        del self.not_implemented_plugin.import_module48        self.noop_plugin = mock.Mock(wraps=PluginInterface())49        self.plugin = mock.Mock(spec=PluginInterface)50        self.plugin.import_module.return_value = self.module51        self.plugin.identify_class.return_value = CONTEXT52        self.plugin.identify_method.return_value = ASSERTION53        self.too_late_plugin = mock.Mock(spec=PluginInterface)54        self.plugin_master = mock.Mock()55        self.plugin_master.not_implemented_plugin = self.not_implemented_plugin56        self.plugin_master.noop_plugin = self.noop_plugin57        self.plugin_master.plugin = self.plugin58        self.plugin_master.too_late_plugin = self.too_late_plugin59    def because_we_run_the_file(self):60        run_object(self.filename, [61            self.not_implemented_plugin,62            self.noop_plugin,63            self.plugin,64            self.too_late_plugin65        ])66    def it_should_ask_the_plugins_to_import_the_correct_file(self):67        self.plugin_master.assert_has_calls([68            mock.call.noop_plugin.import_module(TEST_DATA_DIR, self.module_name),69            mock.call.plugin.import_module(TEST_DATA_DIR, self.module_name)70        ])71    @assertion72    def it_should_not_ask_any_plugins_after_the_one_that_returned(self):73        assert not self.too_late_plugin.import_module.called74    def it_should_run_the_module_that_the_plugin_imported(self):75        assert self.module.When.ran76    def it_should_call_suite_started_with_the_module(self):77        self.plugin.suite_started.assert_called_once_with(self.module)78    def it_should_call_suite_ended_with_the_module(self):79        self.plugin.suite_started.assert_called_once_with(self.module)80    def cleanup_the_file_system(self):81        os.remove(self.filename)82    def write_file(self):83        self.filename = os.path.join(TEST_DATA_DIR, self.module_name + ".py")84        with open(self.filename, 'w+') as f:85            f.write('')86class WhenRunningAFileInAPackage:87    def establish_that_there_is_a_file_in_the_filesystem(self):88        self.module_name = "test_file"89        self.package_name = "package_with_one_file"90        self.full_module_name = self.package_name + '.' + self.module_name91        self.module_list = [types.ModuleType(self.package_name), types.ModuleType(self.full_module_name)]92        self.setup_tree()93        self.plugin = mock.Mock(spec=PluginInterface)94        self.plugin.import_module.side_effect = self.module_list95    def because_we_run_the_file(self):96        run_object(self.filename, [self.plugin])97    def it_should_import_the_package_before_the_file(self):98        assert self.plugin.import_module.call_args_list == [99            mock.call(TEST_DATA_DIR, self.package_name),100            mock.call(TEST_DATA_DIR, self.package_name + '.' + self.module_name)101        ]102    def it_should_not_ask_permission_to_import_the_package(self):103        assert not self.plugin.identify_folder.called104    def it_should_discard_the_package_module(self):105        # it only needs to have been imported, not run106        self.plugin.process_module_list.assert_called_once_with(self.module_list[-1:])107    def cleanup_the_file_system(self):108        shutil.rmtree(self.folder_location)109    def setup_tree(self):110        self.folder_location = os.path.join(TEST_DATA_DIR, self.package_name)111        os.mkdir(self.folder_location)112        self.filename = os.path.join(self.folder_location, self.module_name + ".py")113        with open(os.path.join(self.folder_location, '__init__.py'), 'w+') as f:114            f.write('')115        with open(self.filename, 'w+') as f:116            f.write('')117class WhenRunningAFileInASubPackage:118    def establish_that_there_is_a_file_in_the_filesystem(self):119        self.module_name = "test_file"120        self.package_name = "package_with_one_subpackage"121        self.subpackage_name = "subpackage_with_one_file"122        self.full_subpackage_name = self.package_name + '.' + self.subpackage_name123        self.full_module_name = self.full_subpackage_name + '.' + self.module_name124        self.module_list = [types.ModuleType(self.package_name), types.ModuleType(self.full_subpackage_name), types.ModuleType(self.full_module_name)]125        self.setup_tree()126        self.plugin = mock.Mock(spec=PluginInterface)127        self.plugin.import_module.side_effect = self.module_list128    def because_we_run_the_file(self):129        run_object(self.filename, [self.plugin])130    def it_should_import_the_package_before_the_file(self):131        assert self.plugin.import_module.call_args_list == [132            mock.call(TEST_DATA_DIR, self.package_name),133            mock.call(TEST_DATA_DIR, self.full_subpackage_name),134            mock.call(TEST_DATA_DIR, self.full_module_name)135        ]136    def it_should_discard_the_package_modules(self):137        self.plugin.process_module_list.assert_called_once_with(self.module_list[-1:])138    def cleanup_the_file_system(self):139        shutil.rmtree(self.folder_location)140    def setup_tree(self):141        self.folder_location = os.path.join(TEST_DATA_DIR, self.package_name)142        os.mkdir(self.folder_location)143        subpackage_folder = os.path.join(self.folder_location, self.subpackage_name)144        os.mkdir(subpackage_folder)145        with open(os.path.join(self.folder_location, '__init__.py'), 'w+') as f:146            f.write('')147        with open(os.path.join(subpackage_folder, '__init__.py'), 'w+') as f:148            f.write('')149        self.filename = os.path.join(subpackage_folder, self.module_name + ".py")150        with open(self.filename, 'w+') as f:151            f.write('')152class WhenRunningInitDotPy:153    def establish_that_there_is_a_file_in_the_filesystem(self):154        self.package_name = "package_with_one_file"155        self.setup_tree()156        self.module = types.ModuleType(self.package_name)157        self.plugin = mock.Mock(spec=PluginInterface)158        self.plugin.import_module.return_value = self.module159    def because_we_run_the_file(self):160        run_object(self.filename, [self.plugin])161    def it_should_import_it_as_a_package(self):162        self.plugin.import_module.assert_called_once_with(TEST_DATA_DIR, self.package_name)163    def it_should_not_discard_the_package_module(self):164        self.plugin.process_module_list.assert_called_once_with([self.module])165    def cleanup_the_file_system(self):166        shutil.rmtree(self.folder_location)167    def setup_tree(self):168        self.folder_location = os.path.join(TEST_DATA_DIR, self.package_name)169        self.filename = os.path.join(self.folder_location, '__init__.py')170        os.mkdir(self.folder_location)171        with open(self.filename, 'w+') as f:172            f.write('')173class WhenAPluginFailsToImportAModule:174    def establish_that_the_plugin_throws_an_exception(self):175        self.exception = Exception()176        self.plugin = mock.Mock(spec=PluginInterface)177        self.plugin.import_module.side_effect = self.exception178        self.module_name = 'accident_prone_test_module'179        self.setup_filesystem()180    def because_we_run_the_folder(self):181        run_object(self.filename, [self.plugin])182    def it_should_pass_the_exception_into_unexpected_error_on_the_plugin(self):183        self.plugin.unexpected_error.assert_called_once_with(self.exception)184    def cleanup_the_file_system(self):185        shutil.rmtree(self.folder_path)186    def setup_filesystem(self):187        self.folder_path = os.path.join(TEST_DATA_DIR, 'plugin_failing_folder')188        os.mkdir(self.folder_path)189        self.filename = os.path.join(self.folder_path, self.module_name + ".py")190        with open(self.filename, 'w+') as f:191            f.write("")192class WhenAPluginSuppliesAFolderToRun:193    def establish_that_there_is_a_folder_containing_modules(self):194        self.module_name = "wanted_file1"195        self.setup_filesystem()196        self.ran = False197        self.module = types.ModuleType(self.module_name)198        class Class:199            def it(s):200                self.ran = True201        self.module.Class = Class202        self.plugin = mock.Mock(spec=PluginInterface)203        self.plugin.get_object_to_run.return_value = self.folder_path204        self.plugin.identify_file.return_value = TEST_FILE205        self.plugin.import_module.return_value = self.module206        self.plugin.identify_class.return_value = CONTEXT207        self.plugin.identify_method.return_value = ASSERTION208    def because_we_run_with_the_plugin(self):209        contexts.run_with_plugins([self.plugin])210    def it_should_run_the_file(self):211        assert self.ran212    def cleanup_the_file_system(self):213        shutil.rmtree(self.folder_path)214    def setup_filesystem(self):215        self.folder_path = os.path.join(TEST_DATA_DIR, 'non_package_folder')216        os.mkdir(self.folder_path)217        filename = os.path.join(self.folder_path, self.module_name + ".py")218        with open(filename, 'w+') as f:219            f.write('')220class WhenRunningAFolderWhichIsNotAPackage:221    def establish_that_there_is_a_folder_containing_modules(self):222        self.module_names = ["wanted_file1", "wanted_file2", "an_innocent_module"]223        self.setup_filesystem()...launch.py
Source:launch.py  
...232               "sputnik.webserver.plugins.receiver.accountant.AccountantReceiver",233               "sputnik.webserver.plugins.receiver.engine.EngineReceiver",234               "sputnik.webserver.plugins.receiver.administrator.AdministratorReceiver",235               "sputnik.webserver.rest.RESTProxy"]...plugin.py
Source:plugin.py  
...136        if not plugin:137            raise PluginException("Plugin %s requires %s." % \138                    (self.plugin_path, path))139        return plugin140def run_with_plugins(reactor, plugin_paths, callback, *args, **kwargs):141    plugin_manager = PluginManager()142    plugins = [plugin_manager.load(plugin_path) for plugin_path in plugin_paths]143    @inlineCallbacks144    def init_and_run():145        for plugin in plugins:146            try:147                result = yield plugin_manager.init(plugin)148            except Exception as e:149                error("Not all plugins initialized. Aborting for plugin: %s." % plugin)150                return151        callback(plugin_manager, *args, **kwargs)152    @inlineCallbacks153    def cleanup():154        plugins.reverse()...setup.py
Source:setup.py  
...12    wrapped_initopts = command.initialize_options13    def initialize_options_with_plugins(self):14        self.install_vim = None15        wrapped_initopts(self)16    def run_with_plugins(self):17        if(self.install_vim and not self.user):18            raise Exception("Sorry, the vim plugin is currently only support for --user installs. Patches welcome.")19        wrapped_run(self)20        if(self.install_vim):21            install_vim_plugin(self.install_scripts)22    command.run = run_with_plugins23    command.initialize_options = initialize_options_with_plugins24    return command25@handle_plugins26class install(setuptools.command.install.install):27    pass28setup(name='odpdown',29      description='Generate OpenDocument Presentation (odp) files'30      ' from markdown',...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!!
