Пример #1
0
def runtest_inner(test, generate, verbose, quiet,
                     testdir=None, huntrleaks=False, junit_xml_dir=None):
    test_support.unload(test)
    if not testdir:
        testdir = findtestdir()
    outputdir = os.path.join(testdir, "output")
    outputfile = os.path.join(outputdir, test)
    if verbose:
        cfp = None
    else:
        cfp = cStringIO.StringIO()

    try:
        save_stdout = sys.stdout
        if junit_xml_dir:
            from test.junit_xml import Tee, write_direct_test
            indirect_test = None
            save_stderr = sys.stderr
            sys.stdout = stdout = Tee(sys.stdout)
            sys.stderr = stderr = Tee(sys.stderr)
        try:
            if cfp:
                sys.stdout = cfp
                print test              # Output file starts with test name
            if test.startswith('test.'):
                abstest = test
            else:
                # Always import it from the test package
                abstest = 'test.' + test
            start = time.time()
            the_package = __import__(abstest, globals(), locals(), [])
            the_module = getattr(the_package, test)
            # Most tests run to completion simply as a side-effect of
            # being imported.  For the benefit of tests that can't run
            # that way (like test_threaded_import), explicitly invoke
            # their test_main() function (if it exists).
            indirect_test = getattr(the_module, "test_main", None)
            if indirect_test is not None:
                indirect_test()
            elif junit_xml_dir:
                write_direct_test(junit_xml_dir, abstest, time.time() - start,
                                  stdout=stdout.getvalue(),
                                  stderr=stderr.getvalue())
            if huntrleaks:
                dash_R(the_module, test, indirect_test, huntrleaks)
        finally:
            sys.stdout = save_stdout
            if junit_xml_dir:
                sys.stderr = save_stderr
    except test_support.ResourceDenied, msg:
        if not quiet:
            print test, "skipped --", msg
            sys.stdout.flush()
        if junit_xml_dir:
            write_direct_test(junit_xml_dir, abstest, time.time() - start,
                              'skipped', sys.exc_info(),
                              stdout=stdout.getvalue(),
                              stderr=stderr.getvalue())
        return -2
def runtest_inner(test,
                  verbose,
                  quiet,
                  test_times,
                  testdir=None,
                  huntrleaks=False,
                  junit_xml_dir=None):
    test_support.unload(test)
    if not testdir:
        testdir = findtestdir()
    if verbose:
        capture_stdout = None
    else:
        capture_stdout = cStringIO.StringIO()

    from test.junit_xml import Tee, write_direct_test
    try:
        save_stdout = sys.stdout

        indirect_test = None
        if junit_xml_dir:
            save_stderr = sys.stderr
            sys.stdout = stdout = Tee(sys.stdout)
            sys.stderr = stderr = Tee(sys.stderr)
        try:
            if capture_stdout:
                sys.stdout = capture_stdout
            if test.startswith('test.'):
                abstest = test
            else:
                # Always import it from the test package
                abstest = 'test.' + test
            start_time = time.time()
            the_package = __import__(abstest, globals(), locals(), [])
            the_module = getattr(the_package, test)
            # Old tests run to completion simply as a side-effect of
            # being imported.  For tests based on unittest or doctest,
            # explicitly invoke their test_main() function (if it exists).
            indirect_test = getattr(the_module, "test_main", None)
            test_time = None
            if indirect_test is not None:
                indirect_test()
            elif junit_xml_dir:
                test_time = time.time() - start_time
                write_direct_test(junit_xml_dir,
                                  abstest,
                                  test_time,
                                  stdout=stdout.getvalue(),
                                  stderr=stderr.getvalue())
            if huntrleaks:
                dash_R(the_module, test, indirect_test, huntrleaks)
            if test_time is None:
                test_time = time.time() - start_time
            test_times.append((test_time, test))
        finally:
            sys.stdout = save_stdout
            if junit_xml_dir:
                sys.stderr = save_stderr
                test_time = time.time() - start_time
    except test_support.ResourceDenied, msg:
        if not quiet:
            print test, "skipped --", msg
            sys.stdout.flush()
        if junit_xml_dir:
            write_direct_test(junit_xml_dir,
                              abstest,
                              test_time,
                              'skipped',
                              sys.exc_info(),
                              stdout=stdout.getvalue(),
                              stderr=stderr.getvalue())
        return -2
Пример #3
0
def runtest_inner(test,
                  verbose,
                  quiet,
                  test_times,
                  testdir=None,
                  huntrleaks=False,
                  junit_xml_dir=None):
    support.unload(test)
    if not testdir:
        testdir = findtestdir()
    if verbose:
        capture_stdout = None
    else:
        capture_stdout = io.StringIO()

    from test.junit_xml import Tee, write_direct_test
    try:
        save_stdout = sys.stdout

        indirect_test = None
        if junit_xml_dir:
            save_stderr = sys.stderr
            sys.stdout = stdout = Tee(sys.stdout)
            sys.stderr = stderr = Tee(sys.stderr)
        try:
            if capture_stdout:
                sys.stdout = capture_stdout
            if test.startswith('test.'):
                abstest = test
            else:
                # Always import it from the test package
                abstest = 'test.' + test
            start_time = time.time()
            the_package = __import__(abstest, globals(), locals(), [])
            the_module = getattr(the_package, test)
            # Old tests run to completion simply as a side-effect of
            # being imported.  For tests based on unittest or doctest,
            # explicitly invoke their test_main() function (if it exists).
            indirect_test = getattr(the_module, "test_main", None)
            test_time = None
            if indirect_test is not None:
                indirect_test()
            elif junit_xml_dir:
                test_time = time.time() - start_time
                write_direct_test(junit_xml_dir,
                                  abstest,
                                  test_time,
                                  stdout=stdout.getvalue(),
                                  stderr=stderr.getvalue())
            if huntrleaks:
                dash_R(the_module, test, indirect_test, huntrleaks)
            if test_time is None:
                test_time = time.time() - start_time
            test_times.append((test_time, test))
        finally:
            sys.stdout = save_stdout
            if junit_xml_dir:
                sys.stderr = save_stderr
                test_time = time.time() - start_time
    except support.ResourceDenied as msg:
        if not quiet:
            print(test, "skipped --", msg)
            sys.stdout.flush()
        if junit_xml_dir:
            write_direct_test(junit_xml_dir,
                              abstest,
                              test_time,
                              'skipped',
                              sys.exc_info(),
                              stdout=stdout.getvalue(),
                              stderr=stderr.getvalue())
        return -2
    except (ImportError, unittest.SkipTest) as msg:
        if not quiet:
            print(test, "skipped --", msg)
            sys.stdout.flush()
        if junit_xml_dir:
            write_direct_test(junit_xml_dir,
                              abstest,
                              test_time,
                              'skipped',
                              sys.exc_info(),
                              stdout=stdout.getvalue(),
                              stderr=stderr.getvalue())
        return -1
    except KeyboardInterrupt:
        raise
    except support.TestFailed as msg:
        print("test", test, "failed --", msg)
        sys.stdout.flush()
        if junit_xml_dir and indirect_test is None:
            write_direct_test(junit_xml_dir,
                              abstest,
                              test_time,
                              'failure',
                              sys.exc_info(),
                              stdout=stdout.getvalue(),
                              stderr=stderr.getvalue())
        return 0
    except:
        type, value = sys.exc_info()[:2]
        print("test", test, "crashed --", str(type) + ":", value)
        sys.stdout.flush()
        if verbose:
            traceback.print_exc(file=sys.stdout)
            sys.stdout.flush()
        if junit_xml_dir and indirect_test is None:
            write_direct_test(junit_xml_dir,
                              abstest,
                              test_time,
                              'error',
                              sys.exc_info(),
                              stdout=stdout.getvalue(),
                              stderr=stderr.getvalue())
        return 0
    else:
        # Except in verbose mode, tests should not print anything
        if verbose or huntrleaks:
            return 1
        output = capture_stdout.getvalue()
        if not output:
            return 1
        print("test", test, "produced unexpected output:")
        print("*" * 70)
        print(output)
        print("*" * 70)
        sys.stdout.flush()
        return 0