Esempio n. 1
0
def collect_unittest(paths):
    suite = unittest.TestSuite()
    loader = unittest.defaultTestLoader
    resolver = resolve.ModuleNameResolver()
    paths = utils.sequence(paths)

    for filepath in paths:
        try:
            name, package = resolver.resolve(filepath)
        except ImportError:
            # .py file not in the search path
            name = filepath_to_identifier(filepath)
            package = None

        try:
            if package:
                module = utils.import_module(name)
            else:
                module = imp.load_source(name, filepath)
        except ImportError:
            LOGGER.warn("ImportError occurred while loading module", exc_info=True)
        else:
            tests = loader.loadTestsFromModule(module)
            if tests.countTestCases():
                suite.addTest(tests)
                LOGGER.info("Found %d test(s) in module '%s'" % (tests.countTestCases(), module.__name__))
            else:
                LOGGER.warn("No tests found in module '%s'" % module.__name__)
    return suite
Esempio n. 2
0
def read_module_code(filename, typebits=None, search_path=None,
        resolver=None,
        allow_compilation_failure=False,
        allow_standalone=False):
    """
    Read python module file, and return ``ModuleCode`` instance.
    If *typebits* argument is not ``None``, *filename* must be
    filepath without file extention.
    If *typebits* argument is ``None``, it is detected by filename.
    """

    if typebits is None:
        filename, _, typebits = module_file_typebits(filename)
    if resolver is None:
        resolver = ModuleNameResolver(search_path)

    code = None
    try:
        # Since editing .py files will not affect .pyc and .pyo files soon,
        # give priority to .py files.
        if typebits & PYTHON_SOURCE_MASK:
            # .py
            sourcepath = filename + '.py'
            code = compile_source(sourcepath)
        elif typebits & (PYTHON_OPTIMIZED_MASK | PYTHON_COMPILED_MASK):
            # .pyc, .pyo
            if typebits & PYTHON_OPTIMIZED_MASK:
                sourcepath = filename + '.pyo'
            else:
                sourcepath = filename + '.pyc'
                code = load_compiled(sourcepath)
        else:
            assert False, "illegal typebits: %d" % typebits
    except (SyntaxError, ImportError):
        LOGGER.warn(
            "Exception occurred while loading compiled bytecode",
            exc_info=True)
        if not allow_compilation_failure:
            raise

    try:
        module_name, package_name = resolver.resolve(sourcepath)
    except ImportError:
        if not allow_standalone:
            raise
        module_name = filepath_to_identifier(sourcepath)
        package_name = None

    return ModuleCode(module_name, package_name, sourcepath, code)
Esempio n. 3
0
 def test_filepath_to_identifier(self):
     self.assertEqual('foo_py_0f5843fe4a2bd32647fb23a438d1e43b1342c4aa', utils.filepath_to_identifier('foo.py'))