def test_load_module(self): # Load a known module, so we expect the load to succeed. # We'll specify the module name, though. module_a = load_module('a', __file__, None) self.assertEqual(module_a.__name__, 'a') # Do the same with a different name to ensure it doesn't hit a cache. module_b = load_module('b', __file__, None) self.assertEqual(module_b.__name__, 'b') # Specify the file path components separately. (module_dir, module_name) = split(__file__) module_c = load_module('c', module_name, module_dir) self.assertEqual(splitext(module_c.__file__)[0], splitext(__file__)[0]) # Remove file extension. (module_name, _) = splitext(module_name) module_d = load_module('d', module_name, module_dir) self.assertEqual(splitext(module_d.__file__)[0], splitext(__file__)[0]) # Try loading a module that does not exist. try: load_module('e', 'non-existing-file', module_dir) self.fail('Expected ImportError') except ImportError as _: pass
file is ignored. The Python package nxapi contains utility functions which are independent of this configuration. The modules in package nxapi do not import the config. Thus the test code can import package nxapi (to test the functions there) without any configuration. This is better for unit tests which do not require integration to a remote device. For integration testing, the test code will load the config and pass the device co-ordinates to the library functions (as parameters). @author: Ken Jarrad ([email protected]) """ from __future__ import print_function import os from nxapi.context import load_module from os import getenv from os.path import join, dirname inventory_config = [] config_module = None _default_config_module_dir = join(dirname(__file__), '..', 'config') _default_config_module_file = 'simulation' if not inventory_config: network_profile = getenv('NETWORK_PROFILE', _default_config_module_file) config_module = load_module('config', network_profile, _default_config_module_dir) inventory_config = config_module.inventory