def test_get_functions_from_module_with_testplan(self): """Test get_functions_from_module method when test regex is supplied""" # Mock module to pass to get_functions_from_module mock_module_object = MagicMock() # Testplan to be used to select test functions test_plan = ["test_network_connectivity"] # List to serve as a return value of inspect.getmember containing # mixed types. function_list = [("test_network_connectivity", lambda: 2 * 2), ("test_file_existence", lambda: 2 * 2), ("test2", "string_type")] # Patch inspect module with patch("dodo.introspector.inspect") as mock_inspect: mock_inspect.getmembers = MagicMock() mock_inspect.getmembers.return_value = function_list actual_value = Introspector.get_functions_from_module( mock_module_object, None, test_plan) mock_inspect.getmembers.assert_called_with(mock_module_object) self.assertEqual(len(actual_value), 1) self.assertEqual(actual_value[0][0], "test_network_connectivity") self.assertEqual( isinstance(actual_value[0][1], types.FunctionType), True)
def test_get_modules_from_package(self): """Test get_modules_from_package method""" with patch("dodo.introspector.pkgutil") as mock_pkgutil: mock_package = MagicMock() fake_path = "/some/fake/path" mock_package.__path__ = fake_path modules = Introspector.get_modules_from_package(mock_package) mock_pkgutil.iter_modules.assert_called_with(fake_path) self.assertEqual(type(modules), list)
def execute_tests(self): """Execute tests""" # TODO: Use Python 3 Path module # Get the absolute path of supplied directory in case only directory # name is supplied instead of full path absolute_path = os.path.abspath(self._package_dir) # Parent directory is a parent directory of a package. i.e In # /tmp/testdir, /tmp is a parent directory parent_dir = os.path.normpath(os.path.join(absolute_path, "..")) # We need to put parent directory of package in PYTHONPATH to import it sys.path.append(parent_dir) # Get the basename of path. i.e In /tmp/testdir, testdir is a base name package_name = os.path.basename(absolute_path) # NOTE: Introspector.find_modules method doesn't retrieve modules # from top level directory of a package, so We need to process that # separately. # Import package dynamically to get modules in top level directory package_instance = importlib.import_module(package_name) # Get modules from top level package directory modules = Introspector.get_modules_from_package(package_instance) # Get modules recursively all_modules = Introspector.find_modules(self._package_dir) modules.extend(all_modules) # Get functions from all the modules self._module_functions = self._populate_module_functions_list( modules, package_name) self._functions_results = self._execute_test_functions( self._module_functions)
def test_find_modules(self): """Test find_modules method""" test_path = "/some/test/path" find_packages_return_value = [ "/test/path1", "/test/path2", "/test/path3" ] # Patch setuptools module with patch("dodo.introspector.find_packages") as mock_find_packages,\ patch("dodo.introspector.iter_modules") as mock_iter_modules: mock_find_packages.return_value = find_packages_return_value actual_output = Introspector.find_modules(test_path) self.assertEqual(set(find_packages_return_value), actual_output) self.assertEqual(mock_find_packages.call_count, 1) self.assertEqual(mock_iter_modules.call_count, len(find_packages_return_value))
def _populate_module_functions_list(self, modules, package_name): """Iterate through all the modules and return dictionary with <module-name>:<[functions]> """ module_functions = dict() module_functions_get = module_functions.get for module in modules: try: tmp = importlib.import_module( "{0}.{1}".format(package_name, module)) functions = Introspector.get_functions_from_module( tmp, self._testregex, self._testplan) function_list = module_functions_get(module, []) function_list.extend(functions) module_functions[module] = function_list except Exception as _: exc_type, exc_value, exc_tb = sys.exc_info() print("".join( (traceback.format_exception(exc_type, exc_value, exc_tb)))) return module_functions
def test_get_functions_from_module_with_all(self): """Test get_functions_from_module method when Testplan or Test RegEx is not supplied. """ # Mock module to pass to get_functions_from_module mock_module_object = MagicMock() # List to serve as a return value of inspect.getmember containing # mixed types. function_list = [("test1", lambda: 2 * 2)] # Patch inspect module with patch("dodo.introspector.inspect") as mock_inspect: mock_inspect.getmembers = MagicMock() mock_inspect.getmembers.return_value = function_list actual_value = Introspector.get_functions_from_module( mock_module_object, None, None) mock_inspect.getmembers.assert_called_with(mock_module_object) self.assertEqual(mock_inspect.isfunction.call_count, 1) self.assertEqual(len(actual_value), 1) self.assertEqual( isinstance(actual_value[0][1], types.FunctionType), True)