Ejemplo n.º 1
0
    def discover(self,
                 current_path,
                 file_pattern="test*.py",
                 top_level_dir=None):
        """
        I take a path to a directory and discover all the tests inside files
        matching file_pattern.

        If path is not a readable directory, I raise an ImportError.

        If I don't find anything, I return None.  Otherwise I return a
        GreenTestSuite
        """
        current_abspath = os.path.abspath(current_path)
        if not os.path.isdir(current_abspath):
            raise ImportError("'{}' is not a directory".format(
                str(current_path)))
        suite = GreenTestSuite()
        try:
            for file_or_dir_name in sorted(os.listdir(current_abspath)):
                path = os.path.join(current_abspath, file_or_dir_name)
                # Recurse into directories, attempting to skip virtual environments
                bin_activate = os.path.join(path, "bin", "activate")
                if os.path.isdir(path) and not os.path.isfile(bin_activate):
                    # Don't follow symlinks
                    if os.path.islink(path):
                        continue
                    # Don't recurse into directories that couldn't be a package name
                    if not python_dir_pattern.match(file_or_dir_name):
                        continue

                    subdir_suite = self.discover(
                        path,
                        file_pattern=file_pattern,
                        top_level_dir=top_level_dir or current_path,
                    )

                    if subdir_suite:
                        suite.addTest(subdir_suite)

                elif os.path.isfile(path):
                    # Skip irrelevant files
                    if not python_file_pattern.match(file_or_dir_name):
                        continue
                    if not fnmatch(file_or_dir_name, file_pattern):
                        continue

                    # Try loading the file as a module
                    module_suite = self.loadFromModuleFilename(path)
                    if module_suite:
                        suite.addTest(module_suite)
        except OSError:
            debug("WARNING: Test discovery failed at path {}".format(
                current_path))

        return flattenTestSuite(suite) if suite.countTestCases() else None
Ejemplo n.º 2
0
    def discover(self, current_path, file_pattern='test*.py',
                 top_level_dir=None):
        """
        I take a path to a directory and discover all the tests inside files
        matching file_pattern.

        If path is not a readable directory, I raise an ImportError.

        If I don't find anything, I return None.  Otherwise I return a
        GreenTestSuite
        """
        current_abspath = os.path.abspath(current_path)
        if not os.path.isdir(current_abspath):
            raise ImportError(
                    "'{}' is not a directory".format(str(current_path)))
        suite = GreenTestSuite()
        try:
            for file_or_dir_name in sorted(os.listdir(current_abspath)):
                path = os.path.join(current_abspath, file_or_dir_name)
                # Recurse into directories, attempting to skip virtual environments
                bin_activate = os.path.join(path, 'bin', 'activate')
                if os.path.isdir(path) and not os.path.isfile(bin_activate):
                    # Don't follow symlinks
                    if os.path.islink(path):
                        continue
                    # Don't recurse into directories that couldn't be a package name
                    if not python_dir_pattern.match(file_or_dir_name):
                        continue

                    subdir_suite = self.discover(
                        path, file_pattern=file_pattern,
                        top_level_dir=top_level_dir or current_path
                    )

                    if subdir_suite:
                        suite.addTest(subdir_suite)

                elif os.path.isfile(path):
                    # Skip irrelevant files
                    if not python_file_pattern.match(file_or_dir_name):
                        continue
                    if not fnmatch(file_or_dir_name, file_pattern):
                        continue

                    # Try loading the file as a module
                    module_suite = self.loadFromModuleFilename(path)
                    if module_suite:
                        suite.addTest(module_suite)
        except OSError:
            debug("WARNING: Test discovery failed at path {}".format(current_path))

        return flattenTestSuite(suite) if suite.countTestCases() else None
Ejemplo n.º 3
0
 def test_addTest_testPattern(self):
     """
     Setting test_pattern will cause a test to be filtered.
     """
     mock_test = MagicMock()
     mock_test._testMethodName = 'test_hello'
     mock_test2 = MagicMock()
     mock_test2._testMethodName = 'test_goodbye'
     args = copy.deepcopy(default_args)
     args.test_pattern = '_good*'
     gts = GreenTestSuite(args=args)
     gts.addTest(mock_test)
     self.assertEqual(gts._tests, [])
     gts.addTest(mock_test2)
     self.assertEqual(gts._tests, [mock_test2])
Ejemplo n.º 4
0
 def test_addTest_testPattern(self):
     """
     Setting test_pattern will cause a test to be filtered.
     """
     mock_test = MagicMock()
     mock_test._testMethodName = "test_hello"
     mock_test2 = MagicMock()
     mock_test2._testMethodName = "test_goodbye"
     args = copy.deepcopy(default_args)
     args.test_pattern = "_good*"
     gts = GreenTestSuite(args=args)
     gts.addTest(mock_test)
     self.assertEqual(gts._tests, [])
     gts.addTest(mock_test2)
     self.assertEqual(gts._tests, [mock_test2])
Ejemplo n.º 5
0
def discover(current_path, file_pattern='test*.py'):
    """
    I take a path to a directory and discover all the tests inside files
    matching file_pattern.

    If path is not a readable directory, I raise an ImportError.

    If I don't find anything, I return None.  Otherwise I return a
    GreenTestSuite
    """
    current_abspath = os.path.abspath(current_path)
    if not os.path.isdir(current_abspath):
        raise ImportError("'{}' is not a directory".format(str(current_path)))
    suite = GreenTestSuite()
    for file_or_dir_name in sorted(os.listdir(current_abspath)):
        path = os.path.join(current_abspath, file_or_dir_name)
        # Recurse into directories, attempting to skip virtual environments
        if os.path.isdir(path) and not os.path.isfile(
                os.path.join(path, 'bin', 'activate')):
            # Don't follow symlinks
            if os.path.islink(path):
                continue
            # Don't recurse into directories that couldn't be a package name
            if not python_dir_pattern.match(file_or_dir_name):
                continue
            subdir_suite = discover(path, file_pattern=file_pattern)
            if subdir_suite:
                suite.addTest(subdir_suite)

        elif os.path.isfile(path):
            # Skip irrelevant files
            if not python_file_pattern.match(file_or_dir_name):
                continue
            if not fnmatch(file_or_dir_name, file_pattern):
                continue

            # Try loading the file as a module
            module_suite = loadFromModuleFilename(path)
            if module_suite:
                suite.addTest(module_suite)

    return ((suite.countTestCases() and suite) or None)
Ejemplo n.º 6
0
Archivo: loader.py Proyecto: tony/green
def discover(current_path, file_pattern='test*.py'):
    """
    I take a path to a directory and discover all the tests inside files
    matching file_pattern.

    If path is not a readable directory, I raise an ImportError.

    If I don't find anything, I return None.  Otherwise I return a
    GreenTestSuite
    """
    current_abspath = os.path.abspath(current_path)
    if not os.path.isdir(current_abspath):
        raise ImportError(
                "'%s' is not a directory".format(str(current_path)))
    suite = GreenTestSuite()
    for file_or_dir_name in sorted(os.listdir(current_abspath)):
        path = os.path.join(current_abspath, file_or_dir_name)
        # Recurse into directories, attempting to skip virtual environments
        if os.path.isdir(path) and not os.path.isfile(os.path.join(path, 'bin', 'activate')):
            subdir_suite = discover(path, file_pattern=file_pattern)
            if subdir_suite:
                suite.addTest(subdir_suite)

        elif os.path.isfile(path):
            # Skip irrelevant files
            if not python_file_pattern.match(file_or_dir_name):
                continue
            if not fnmatch(file_or_dir_name, file_pattern):
                continue

            # Try loading the file as a module
            module_suite = loadFromModuleFilename(path)
            if module_suite:
                suite.addTest(module_suite)

    return ((suite.countTestCases() and suite) or None)