Example #1
0
def run_aiida_db_tests(tests_to_run, verbose=False):
    """
    Run all tests specified in tests_to_run.
    Return the list of test results.
    """
    # Empty test suite that will be populated
    test_suite = unittest.TestSuite()

    actually_run_tests = []
    num_tests_expected = 0

    # To avoid adding more than once the same test
    # (e.g. if you type both db and db.xxx)
    found_modulenames = set()

    for test in set(tests_to_run):
        try:
            modulenames = get_db_test_list()[test]
        except KeyError:
            if verbose:
                print('Unknown DB test {}... skipping'.format(test),
                      file=sys.stderr)
            continue
        actually_run_tests.append(test)

        for modulename in modulenames:
            if modulename not in found_modulenames:
                try:
                    test_suite.addTest(
                        unittest.defaultTestLoader.loadTestsFromName(
                            modulename))
                except AttributeError as exception:
                    try:
                        import importlib
                        importlib.import_module(modulename)
                    except ImportError as exception:
                        print(
                            "[CRITICAL] The module '{}' has an import error and the tests cannot be run:\n{}"
                            .format(modulename,
                                    traceback.format_exc(exception)),
                            file=sys.stderr)
                        sys.exit(1)
                found_modulenames.add(modulename)

        num_tests_expected = test_suite.countTestCases()

    if verbose:
        print('DB tests that will be run: {} (expecting {} tests)'.format(
            ','.join(actually_run_tests), num_tests_expected),
              file=sys.stderr)
        results = unittest.TextTestRunner(failfast=False,
                                          verbosity=2).run(test_suite)
    else:
        results = unittest.TextTestRunner(failfast=False).run(test_suite)

    if verbose:
        print('Run tests: {}'.format(results.testsRun))

    return results
Example #2
0
def run_aiida_db_tests(tests_to_run, verbose=False):
    """
    Run all tests specified in tests_to_run.
    Return the list of test results.
    """
    # Empty test suite that will be populated
    test_suite = TestSuite()

    actually_run_tests = []
    num_tests_expected = 0

    # To avoid adding more than once the same test
    # (e.g. if you type both db and db.xxx)
    found_modulenames = set()

    for test in set(tests_to_run):
        try:
            modulenames = get_db_test_list()[test]
        except KeyError:
            if verbose:
                print >> sys.stderr, "Unknown DB test {}... skipping".format(
                    test)
            continue
        actually_run_tests.append(test)

        for modulename in modulenames:
            if modulename not in found_modulenames:
                test_suite.addTest(test_loader.loadTestsFromName(modulename))
                found_modulenames.add(modulename)

        num_tests_expected = test_suite.countTestCases()

    if verbose:
        print >> sys.stderr, (
            "DB tests that will be run: {} (expecting {} tests)".format(
                ",".join(actually_run_tests), num_tests_expected))

    results = unittest.TextTestRunner(failfast=False).run(test_suite)

    if verbose:
        print "Run tests: {}".format(results.testsRun)

    return results
Example #3
0
__authors__ = "The AiiDA team."

tests_to_run = settings_profile.__dict__.get('aiida_test_list', None)
if tests_to_run is None:
    # This is intended as a safety mechanism. This should help to ensure that
    # tests are  run only after setting the flag in the djsite.settings.settings
    # module, and more importantly only after changing the DB and REPO to be
    # test ones.
    raise InternalError(
        "aiida_test_list is not set, but you are trying to run the tests...")

actually_run_tests = []
num_tests_expected = 0
for test in set(tests_to_run):
    try:
        modulenames = get_db_test_list()[test]
    except KeyError:
        print >> sys.stderr, "Unknown DB test {}... skipping".format(test)
        continue

    for modulename in modulenames:
        module = importlib.import_module(modulename)
        for objname, obj in module.__dict__.iteritems():
            if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
                # See if the object has test methods.
                # Add only if there is at least one test_ method
                testmethods = [
                    m for m in inspect.getmembers(obj,
                                                  predicate=inspect.ismethod)
                    if m[0].startswith("test_")
                ]