示例#1
0
def main(testGroup=['test'], restoreEnvironmentDefaults=False, limit=None):
    '''Run all tests. Group can be test and external

    >>> print(None)
    None
    '''
    globs = __import__('music21').__dict__.copy()
    docTestOptions = (doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE)
    # in case there are any tests here, get a suite to load up later
    s1 = doctest.DocTestSuite(
        __name__,
        globs=globs,
        optionflags=docTestOptions
        )

    modGather = ModuleGather()
    modules = modGather.load(restoreEnvironmentDefaults)

    verbosity = 2
    if 'verbose' in sys.argv:
        verbosity = 1 # this seems to hide most display

    environLocal.printDebug('looking for Test classes...\n')
    # look over each module and gather doc tests and unittests
    totalModules = 0
    
    for module in common.sortModules(modules):
        unitTestCases = []
        if limit is not None:
            if totalModules > limit:
                break
        totalModules += 1
        # get Test classes in module
        if not hasattr(module, 'Test'):
            environLocal.printDebug('%s has no Test class' % module)
        else:
            if 'test' in testGroup:
                unitTestCases.append(module.Test)
        if not hasattr(module, 'TestExternal'):
            pass
            #environLocal.printDebug('%s has no TestExternal class\n' % module)
        else:
            if 'external' in testGroup or 'testExternal' in testGroup:
                unitTestCases.append(module.TestExternal)

        # for each Test class, load this into a suite
        for testCase in unitTestCases:
            s2 = unittest.defaultTestLoader.loadTestsFromTestCase(testCase)
            s1.addTests(s2)
        try:
            globs = __import__('music21').__dict__.copy()
            s3 = doctest.DocTestSuite(
                module,
                globs=globs,
                optionflags=docTestOptions,
                )
            s1.addTests(s3)
        except ValueError:
            environLocal.printDebug('%s cannot load Doctests' % module)
            continue
        
        allLocals = [getattr(module, x) for x in dir(module)]
        common.addDocAttrTestsToSuite(s1, allLocals, outerFilename=module.__file__, globs=globs, optionflags=docTestOptions)
    
    common.fixTestsForPy2and3(s1)
    
    environLocal.printDebug('running Tests...\n')
            
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', RuntimeWarning)  # import modules...
        runner = unittest.TextTestRunner(verbosity=verbosity)
        finalTestResults = runner.run(s1)  
    
    if cov is not None:
        cov.stop()
        cov.save()
    
    if (len(finalTestResults.errors) > 0 or
        len(finalTestResults.failures) > 0 or
        len(finalTestResults.unexpectedSuccesses) > 0):
            exit(1)
    else:
        exit(0)
示例#2
0
def main(testGroup=['test'], restoreEnvironmentDefaults=False, limit=None):
    '''Run all tests. Group can be test and external

    >>> print(None)
    None
    '''
    globs = __import__('music21').__dict__.copy()
    docTestOptions = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE)
    # in case there are any tests here, get a suite to load up later
    s1 = doctest.DocTestSuite(__name__,
                              globs=globs,
                              optionflags=docTestOptions)

    modGather = ModuleGather()
    modules = modGather.load(restoreEnvironmentDefaults)

    verbosity = 2
    if 'verbose' in sys.argv:
        verbosity = 1  # this seems to hide most display

    environLocal.printDebug('looking for Test classes...\n')
    # look over each module and gather doc tests and unittests
    totalModules = 0

    for module in common.sortModules(modules):
        unitTestCases = []
        if limit is not None:
            if totalModules > limit:
                break
        totalModules += 1
        # get Test classes in module
        if not hasattr(module, 'Test'):
            environLocal.printDebug('%s has no Test class' % module)
        else:
            if 'test' in testGroup:
                unitTestCases.append(module.Test)
        if not hasattr(module, 'TestExternal'):
            pass
            #environLocal.printDebug('%s has no TestExternal class\n' % module)
        else:
            if 'external' in testGroup or 'testExternal' in testGroup:
                unitTestCases.append(module.TestExternal)

        # for each Test class, load this into a suite
        for testCase in unitTestCases:
            s2 = unittest.defaultTestLoader.loadTestsFromTestCase(testCase)
            s1.addTests(s2)
        try:
            globs = __import__('music21').__dict__.copy()
            s3 = doctest.DocTestSuite(
                module,
                globs=globs,
                optionflags=docTestOptions,
            )
            s1.addTests(s3)
        except ValueError:
            environLocal.printDebug('%s cannot load Doctests' % module)
            continue

        allLocals = [getattr(module, x) for x in dir(module)]
        common.addDocAttrTestsToSuite(s1,
                                      allLocals,
                                      outerFilename=module.__file__,
                                      globs=globs,
                                      optionflags=docTestOptions)

    common.fixTestsForPy2and3(s1)

    environLocal.printDebug('running Tests...\n')

    with warnings.catch_warnings():
        warnings.simplefilter('ignore', RuntimeWarning)  # import modules...
        runner = unittest.TextTestRunner(verbosity=verbosity)
        finalTestResults = runner.run(s1)

    if cov is not None:
        cov.stop()
        cov.save()

    if (len(finalTestResults.errors) > 0 or len(finalTestResults.failures) > 0
            or len(finalTestResults.unexpectedSuccesses) > 0):
        exit(1)
    else:
        exit(0)
def runOneModuleWithoutImp(args):
    modGath = args[0] # modGather object
    fp = args[1]
    verbosity = False
    timeStart = time.time()
    moduleObject = modGath.getModuleWithoutImp(fp)
    environLocal.printDebug('running %s \n' % fp)
    if moduleObject == 'skip':
        success = '%s is skipped \n' % fp
        environLocal.printDebug(success)
        return ModuleResponse('Skipped', fp, success)
    elif moduleObject == 'notInTree':
        success = '%s is in the music21 directory but not imported in music21. Skipped -- fix!' % modGath._getNamePeriod(fp)
        environLocal.printDebug(success)
        return ModuleResponse("NotInTree", fp, success)

    
    try:
        moduleName = modGath._getName(fp)
        globs = __import__('music21').__dict__.copy()
        docTestOptions = (doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE)
        s1 = doctest.DocTestSuite(
            globs=globs,
            optionflags=docTestOptions,
            )
        
        # get Test classes in moduleObject
        if not hasattr(moduleObject, 'Test'):
            environLocal.printDebug('%s has no Test class' % moduleObject)
        else:
            s1.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(moduleObject.Test))
        try:
            globs = __import__('music21').__dict__.copy()
            s3 = doctest.DocTestSuite(moduleObject,
                globs=globs,
                optionflags=docTestOptions,
                )
            s1.addTests(s3)
        except ValueError:
            environLocal.printDebug('%s cannot load Doctests' % moduleObject)
            pass        

        common.fixTestsForPy2and3(s1)
        
        
        environLocal.printDebug('running Tests...\n')
        runner = unittest.TextTestRunner(verbosity=verbosity)
        try:
            testResult = runner.run(s1)  
            
            # need to make testResult pickleable by removing the instancemethod parts...
            errors = []
            for e in testResult.errors:
                errors.append(e[1])
            failures = []
            for f in testResult.failures:
                failures.append(f[1])
            runTime = int(time.time() - timeStart)
            return ModuleResponse("TestsRun", fp, moduleName, testResult.wasSuccessful(), 
                                  str(testResult), errors, failures, testResult.testsRun, runTime)
        except Exception as excp:
            environLocal.printDebug('*** Exception in running %s: %s...\n' % (moduleName, excp))
            return ModuleResponse("TrappedException", fp, moduleName, None, str(excp))
    except Exception as excp:
        environLocal.printDebug('*** Large Exception in running %s: %s...\n' % (fp, excp))
        return ModuleResponse("LargeException", fp, None, None, str(excp))
示例#4
0
def runOneModuleWithoutImp(args):
    modGath = args[0]  # modGather object
    fp = args[1]
    verbosity = False
    timeStart = time.time()
    moduleObject = modGath.getModuleWithoutImp(fp)
    environLocal.printDebug('running %s \n' % fp)
    if moduleObject == 'skip':
        success = '%s is skipped \n' % fp
        environLocal.printDebug(success)
        return ModuleResponse('Skipped', fp, success)
    elif moduleObject == 'notInTree':
        success = '%s is in the music21 directory but not imported in music21. Skipped -- fix!' % modGath._getNamePeriod(
            fp)
        environLocal.printDebug(success)
        return ModuleResponse("NotInTree", fp, success)

    try:
        moduleName = modGath._getName(fp)
        globs = __import__('music21').__dict__.copy()
        docTestOptions = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE)
        s1 = doctest.DocTestSuite(
            globs=globs,
            optionflags=docTestOptions,
        )

        # get Test classes in moduleObject
        if not hasattr(moduleObject, 'Test'):
            environLocal.printDebug('%s has no Test class' % moduleObject)
        else:
            s1.addTests(
                unittest.defaultTestLoader.loadTestsFromTestCase(
                    moduleObject.Test))
        try:
            globs = __import__('music21').__dict__.copy()
            s3 = doctest.DocTestSuite(
                moduleObject,
                globs=globs,
                optionflags=docTestOptions,
            )
            s1.addTests(s3)
        except ValueError:
            environLocal.printDebug('%s cannot load Doctests' % moduleObject)
            pass

        common.fixTestsForPy2and3(s1)

        environLocal.printDebug('running Tests...\n')
        runner = unittest.TextTestRunner(verbosity=verbosity)
        try:
            testResult = runner.run(s1)

            # need to make testResult pickleable by removing the instancemethod parts...
            errors = []
            for e in testResult.errors:
                errors.append(e[1])
            failures = []
            for f in testResult.failures:
                failures.append(f[1])
            runTime = int(time.time() - timeStart)
            return ModuleResponse("TestsRun", fp, moduleName,
                                  testResult.wasSuccessful(), str(testResult),
                                  errors, failures, testResult.testsRun,
                                  runTime)
        except Exception as excp:
            environLocal.printDebug('*** Exception in running %s: %s...\n' %
                                    (moduleName, excp))
            return ModuleResponse("TrappedException", fp, moduleName, None,
                                  str(excp))
    except Exception as excp:
        environLocal.printDebug('*** Large Exception in running %s: %s...\n' %
                                (fp, excp))
        return ModuleResponse("LargeException", fp, None, None, str(excp))