Esempio n. 1
0
    def testTwoResults(self):
        if due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
            return

        unittest.installHandler()

        result = unittest.TestResult()
        unittest.registerResult(result)
        new_handler = signal.getsignal(signal.SIGINT)

        result2 = unittest.TestResult()
        unittest.registerResult(result2)
        self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)

        result3 = unittest.TestResult()

        def test(result):
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)

        try:
            test(result)
        except KeyboardInterrupt:
            self.fail("KeyboardInterrupt not handled")

        if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
            self.assertTrue(result.shouldStop)
            self.assertTrue(result2.shouldStop)
        self.assertFalse(result3.shouldStop)
Esempio n. 2
0
File: run.py Progetto: encukou/samba
 def runTests(self):
     if (self.catchbreak
         and getattr(unittest, 'installHandler', None) is not None):
         unittest.installHandler()
     self.result = self.testRunner.run(self.test)
     if self.exit:
         sys.exit(not self.result.wasSuccessful())
Esempio n. 3
0
    def __init__(self, tests=[], from_gui=False, categories=['system', 'unit', 'gui', 'verification'], timing=False):
        """Store the list of tests to preform.

        The test list should be something like ['N_state_model.test_stereochem_analysis'].  The first part is the imported test case class, the second is the specific test.


        @keyword tests:         The list of tests to preform.  If left at [], then all tests will be run.
        @type tests:            list of str
        @keyword from_gui:      A flag which indicates if the tests are being run from the GUI or not.
        @type from_gui:         bool
        @keyword categories:    The list of test categories to run, for example ['system', 'unit', 'gui', 'verification'] for all tests.
        @type categories:       list of str
        @keyword timing:        A flag which if True will enable timing of individual tests.
        @type timing:           bool
        """

        # Store the args.
        self.tests = tests
        self.from_gui = from_gui
        self.categories = categories

        # Set up the test runner.
        if from_gui:
            self.runner = GuiTestRunner(stream=sys.stdout, timing=timing)
        else:
            self.runner = RelaxTestRunner(stream=sys.stdout, timing=timing)

        # Let the tests handle the keyboard interrupt (for Python 2.7 and higher).
        if hasattr(unittest, 'installHandler'):
            unittest.installHandler()
Esempio n. 4
0
    def run(self):
        try:
            # Use system 'coverage' if available
            import coverage
            use_coverage = True
        except:
            use_coverage = False

        tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
        t = unittest.TextTestRunner(verbosity=1)

        if use_coverage:
            coverage.erase()
            coverage.start()

        if hasattr(unittest, "installHandler"):
            try:
                unittest.installHandler()
            except:
                print "installHandler hack failed"

        try:
            result = t.run(tests)
        except KeyboardInterrupt:
            sys.exit(1)

        if use_coverage:
            coverage.stop()

        sys.exit(int(bool(len(result.failures) > 0 or
                          len(result.errors) > 0)))
Esempio n. 5
0
    def testSecondInterrupt(self):
        if due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
            return

        result = unittest.TestResult()
        unittest.installHandler()
        unittest.registerResult(result)

        def test(result):
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
            result.breakCaught = True
            if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
                self.assertTrue(result.shouldStop)
            os.kill(pid, signal.SIGINT)
            if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
                self.fail("Second KeyboardInterrupt not raised")

        try:
            test(result)
        except KeyboardInterrupt:
            pass
        else:
            if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
                self.fail("Second KeyboardInterrupt not raised")
        self.assertTrue(result.breakCaught)
Esempio n. 6
0
    def testTwoResults(self):
        unittest.installHandler()

        result = unittest.TestResult()
        unittest.registerResult(result)
        new_handler = signal.getsignal(signal.SIGINT)

        result2 = unittest.TestResult()
        unittest.registerResult(result2)
        self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)

        result3 = unittest.TestResult()

        def test(result):
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)

        try:
            test(result)
        except KeyboardInterrupt:
            self.fail("KeyboardInterrupt not handled")

        self.assertTrue(result.shouldStop)
        self.assertTrue(result2.shouldStop)
        self.assertFalse(result3.shouldStop)
Esempio n. 7
0
 def testRemoveHandler(self):
     default_handler = signal.getsignal(signal.SIGINT)
     unittest.installHandler()
     unittest.removeHandler()
     self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
     unittest.removeHandler()
     self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
Esempio n. 8
0
    def run(self):
        try:
            import coverage
            use_cov = True
        except:
            use_cov = False
            cov = None

        if use_cov:
            # The latter is required to not give errors on f23, probably
            # a temporary bug.
            omit = ["/usr/*", "/*/tests/*", "/builddir/*"]
            cov = coverage.coverage(omit=omit)
            cov.erase()
            cov.start()

        import tests as testsmodule
        testsmodule.cov = cov
        testsmodule.utils.REGENERATE_OUTPUT = bool(self.regenerate_output)

        if hasattr(unittest, "installHandler"):
            try:
                unittest.installHandler()
            except:
                print "installHandler hack failed"

        tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
        if self.only:
            newtests = []
            for suite1 in tests:
                for suite2 in suite1:
                    for testcase in suite2:
                        if self.only in str(testcase):
                            newtests.append(testcase)

            if not newtests:
                print "--only didn't find any tests"
                sys.exit(1)
            tests = unittest.TestSuite(newtests)
            print "Running only:"
            for test in newtests:
                print "%s" % test
            print

        t = unittest.TextTestRunner(verbosity=1)

        try:
            result = t.run(tests)
        except KeyboardInterrupt:
            sys.exit(1)

        if use_cov:
            cov.stop()
            cov.save()

        err = int(bool(len(result.failures) > 0 or
                       len(result.errors) > 0))
        if not err and use_cov and self.coverage:
            cov.report(show_missing=False)
        sys.exit(err)
Esempio n. 9
0
 def run(self):    
   build = self.get_finalized_command("build")
   build.run()
   
   if self.catch:
     unittest.installHandler()
   
   sys.path.insert(0, str(Path(build.build_lib).resolve()))
   
   test_loader = unittest.defaultTestLoader
   
   if self.test_name:
     tests = test_loader.loadTestsFromName(self.test_name, None)
   else:
     tests = test_loader.discover(self.start_dir, self.pattern)
   
   test_runner = unittest.TextTestRunner(
     verbosity = self.verbose,
     failfast = self.failfast,
     buffer = self.buffer
   )
   
   test_results = test_runner.run(tests)
     
   del sys.path[0]
   
   if self.exit and not test_results.wasSuccessful():
     raise SystemExit()
Esempio n. 10
0
def run_tests(file):
    suite = get_tests_suite(file)
    unittest.installHandler()
    logging.disable(logging.INFO)
    results = unittest.TextTestRunner(verbosity=2).run(suite)
    logging.disable(logging.NOTSET)
    unittest.removeHandler()
    return results
Esempio n. 11
0
    def run(self,
            test_modules=__all__,
            pretend=False,
            logfile='test/test.log',
            loglevel=forcebalance.output.INFO,
            **kwargs):
            
        self.check()

        self.logger.setLevel(loglevel)

        # first install unittest interrupt handler which gracefully finishes current test on Ctrl+C
        unittest.installHandler()

        # create blank test suite and fill it with test suites loaded from each test module
        tests = unittest.TestSuite()
        systemTests = unittest.TestSuite()
        for suite in unittest.defaultTestLoader.discover('test'):
            for module in suite:
                for test in module:
                    modName,caseName,testName = test.id().split('.')
                    if modName in test_modules:
                        if modName=="test_system": systemTests.addTest(test)
                        else: tests.addTest(test)

        tests.addTests(systemTests) # integration tests should be run after other tests

        result = ForceBalanceTestResult()
        
        forcebalance.output.getLogger("forcebalance").addHandler(forcebalance.output.NullHandler())

        ### START TESTING ###
        # run any pretest tasks before first test
        result.startTestRun(tests)

        # if pretend option is enabled, skip all tests instead of running them
        if pretend:
            for test in tests:
                result.addSkip(test)

        # otherwise do a normal test run
        else:
            unittest.registerResult(result)
            try:
                tests.run(result)
            except KeyboardInterrupt:
                # Adding this allows us to determine
                # what is causing tests to hang indefinitely.
                import traceback
                traceback.print_exc()
                self.logger.exception(msg="Test run cancelled by user")
            except:
                self.logger.exception(msg="An unexpected exception occurred while running tests\n")

        result.stopTestRun(tests)
        ### STOP TESTING ###

        return result
Esempio n. 12
0
def main():
    installHandler()
    option_parser = optparse.OptionParser(
        usage="%prog [options] [filenames]",
        description="pyjaco unittests script."
        )
    option_parser.add_option(
        "-a",
        "--run-all",
        action="store_true",
        dest="run_all",
        default=False,
        help="run all tests (including the known-to-fail)"
        )
    option_parser.add_option(
        "-x",
        "--no-error",
        action="store_true",
        dest="no_error",
        default=False,
        help="ignores error (don't display them after tests)"
        )
    option_parser.add_option(
        "-f",
        "--only-failing",
        action="store_true",
        dest="only_failing",
        default=False,
        help="run only failing tests (to check for improvements)"
        )
    options, args = option_parser.parse_args()
    
    with open("py-builtins.js", "w") as f:
        builtins = BuiltinGenerator().generate_builtins()
        f.write(builtins)
    
    runner = testtools.runner.Py2JsTestRunner(verbosity=2)
    results = None
    try:
        if options.run_all:
            results = runner.run(testtools.tests.ALL)
        elif options.only_failing:
            results = runner.run(testtools.tests.KNOWN_TO_FAIL)
        elif args:
            results = runner.run(testtools.tests.get_tests(args))
        else:
            results = runner.run(testtools.tests.NOT_KNOWN_TO_FAIL)
    except KeyboardInterrupt:
        pass
    if not options.no_error and results and results.errors:
        print
        print "errors:"
        print "  (use -x to skip this part)"
        for test, error in results.errors:
            print
            print "*", str(test), "*"
            print error
Esempio n. 13
0
def run_tests(options, testsuite, runner=None):
    if runner is None:
        runner = unittest.TextTestRunner()
        runner.verbosity = options.verbose
        runner.failfast = options.failfast
    if options.catchbreak:
        unittest.installHandler()
    result = runner.run(testsuite)
    return result.wasSuccessful()
Esempio n. 14
0
    def testRemoveHandler(self):
        default_handler = signal.getsignal(signal.SIGINT)
        unittest.installHandler()
        unittest.removeHandler()
        self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)

        # check that calling removeHandler multiple times has no ill-effect
        unittest.removeHandler()
        self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
Esempio n. 15
0
    def testRemoveHandlerAsDecorator(self):
        default_handler = signal.getsignal(signal.SIGINT)
        unittest.installHandler()

        @unittest.removeHandler
        def test():
            self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)

        test()
        self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
Esempio n. 16
0
 def testInstallHandler(self):
     default_handler = signal.getsignal(signal.SIGINT)
     unittest.installHandler()
     self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
     try:
         pid = os.getpid()
         os.kill(pid, signal.SIGINT)
     except KeyboardInterrupt:
         self.fail('KeyboardInterrupt not handled')
     self.assertTrue(unittest.signals._interrupt_handler.called)
Esempio n. 17
0
    def do(self):
        if self.catchbreak and getattr(unittest, 'installHandler', None):
            unittest.installHandler()

        ClusterFixture.env = self.get_environment()

        runner = TestToolsTestRunner(sys.stdout)
        results = runner.run(self.get_tests())

        sys.exit(not results.wasSuccessful())
Esempio n. 18
0
    def run(self,
            test_modules=__all__,
            pretend=False,
            logfile='test/test.log',
            loglevel=logging.INFO,
            **kwargs):

        self.logger.setLevel(loglevel)

        # first install unittest interrupt handler which gracefully finishes current test on Ctrl+C
        unittest.installHandler()

        # create blank test suite and fill it with test suites loaded from each test module
        tests = unittest.TestSuite()
        for module in test_modules:
            try:
                m=__import__(module)
                module_tests=unittest.defaultTestLoader.loadTestsFromModule(m)
                tests.addTest(module_tests)
            except ImportError:
                self.logger.error("No such test module: %s\n" % module)
            except:
                self.logger.critical("Error loading '%s'\n" % module)
                print traceback.print_exc()

        result = ForceBalanceTestResult()

        ### START TESTING ###
        # run any pretest tasks before first test
        result.startTestRun(tests)

        # if pretend option is enabled, skip all tests instead of running them
        if pretend:
            for module in tests:
                for test in module:
                    try:
                        result.addSkip(test)
                    # addSkip will fail if run on TestSuite objects
                    except AttributeError: continue

        # otherwise do a normal test run
        else:
            self.console = sys.stdout
            sys.stdout = open(logfile, 'w')

            unittest.registerResult(result)
            tests.run(result)

            sys.stdout.close()
            sys.stdout = self.console

        result.stopTestRun(tests)
        ### STOP TESTING ###

        return result
Esempio n. 19
0
    def run_tests(self, test_suite):
        # Install a signal handler to catch Ctrl-C and display the results
        # (but only if running >2.6).
        if sys.version_info[0] > 2 or sys.version_info[1] > 6:
            unittest.installHandler()

        # Run the tests
        runner = FbJsonTestRunner(verbosity=self.options.verbosity)
        result = runner.run(test_suite)

        return result
Esempio n. 20
0
 def testRemoveResult(self):
     result = unittest.TestResult()
     unittest.registerResult(result)
     unittest.installHandler()
     self.assertTrue(unittest.removeResult(result))
     self.assertFalse(unittest.removeResult(unittest.TestResult()))
     try:
         pid = os.getpid()
         os.kill(pid, signal.SIGINT)
     except KeyboardInterrupt:
         pass
     self.assertFalse(result.shouldStop)
Esempio n. 21
0
    def run_parallel(
            self, test_suites, test_runner, result_type=None,
            results_path=None):

        exit_code = 0
        proc = None
        unittest.installHandler()
        processes = []
        manager = Manager()
        results = manager.dict()
        manager.dict()
        start = time.time()

        test_mapping = {}
        for test_suite in test_suites:
            # Give each test suite an uuid so it can be
            # matched to the correct test result
            test_id = str(uuid.uuid4())
            test_mapping[test_id] = test_suite

            proc = Process(
                target=self.execute_test,
                args=(test_runner, test_id, test_suite, results))
            processes.append(proc)
            proc.start()

        for proc in processes:
            proc.join()

        finish = time.time()

        errors, failures, _ = self.dump_results(start, finish, results)

        if result_type is not None:
            all_results = []
            for test_id, result in results.items():
                tests = test_mapping[test_id]
                result_parser = SummarizeResults(
                    vars(result), tests, (finish - start))
                all_results += result_parser.gather_results()

            reporter = Reporter(
                result_parser=result_parser, all_results=all_results)
            reporter.generate_report(
                result_type=result_type, path=results_path)

        if failures or errors:
            exit_code = 1

        return exit_code
Esempio n. 22
0
    def testHandlerReplacedButCalled(self):
        unittest.installHandler()
        handler = signal.getsignal(signal.SIGINT)

        def new_handler(frame, signum):
            handler(frame, signum)

        signal.signal(signal.SIGINT, new_handler)
        try:
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
        except KeyboardInterrupt:
            pass
        else:
            self.fail("replaced but delegated handler doesn't raise interrupt")
Esempio n. 23
0
    def testInstallHandler(self):
        if due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
            return

        default_handler = signal.getsignal(signal.SIGINT)
        unittest.installHandler()
        self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)

        try:
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
        except KeyboardInterrupt:
            self.fail("KeyboardInterrupt not handled")

        self.assertTrue(unittest.signals._interrupt_handler.called)
Esempio n. 24
0
    def testHandlerReplacedButCalled(self):
        if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
            self.skipTest('test requires SIGINT to not be ignored')
        unittest.installHandler()
        handler = signal.getsignal(signal.SIGINT)

        def new_handler(frame, signum):
            handler(frame, signum)

        signal.signal(signal.SIGINT, new_handler)
        try:
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
        except KeyboardInterrupt:
            pass
        self.fail("replaced but delegated handler doesn't raise interrupt")
Esempio n. 25
0
 def runTests(self):
     if self.catchbreak and getattr(unittest, "installHandler", None) is not None:
         unittest.installHandler()
     if self.testRunner is None:
         self.testRunner = TestToolsTestRunner
     if isinstance(self.testRunner, classtypes()):
         try:
             testRunner = self.testRunner(verbosity=self.verbosity, failfast=self.failfast, buffer=self.buffer)
         except TypeError:
             # didn't accept the verbosity, buffer or failfast arguments
             testRunner = self.testRunner()
     else:
         # it is assumed to be a TestRunner instance
         testRunner = self.testRunner
     self.result = testRunner.run(self.test)
     if self.exit:
         sys.exit(not self.result.wasSuccessful())
Esempio n. 26
0
def execute_tests(dir_under_test, testname):

    if testname is not None and "." in testname:
        tests = []
        tests.append(tuple(testname.split(".", 2)))
    else:
        tests = __search_for_tests()

    # let's move to the directory under test
    crt_dir = os.getcwd()
    os.chdir(dir_under_test)

    # execute each module
    # pylint: disable=broad-except
    # we disable the broad-except because we want to actually catch all possible exceptions
    try:
        # sorting the tests by the numeric order in the class name
        tests = sorted(tests, key=lambda x: int(re.search(r"[0-9]+", x[1]).group(0)))
        config.logger.debug("Discovered test clases: %s", pprint.pformat(tests))
        unittest.installHandler()
        suite = unittest.TestSuite()
        loader = unittest.TestLoader()
        result = unittest.TestResult()
        result.failfast = True
        for module_file, test_name in tests:
            suite.addTest(loader.loadTestsFromName("%s.%s" % (module_file, test_name)))
        config.logger.info("Running %d test(s)", suite.countTestCases())
        suite.run(result)

        for error in result.errors:
            config.logger.error("Exception on test: %s\n%s", error[0],
                                "\n".join(["-- %s" % x for x in error[1].split("\n")]))

        for failure in result.failures:
            config.logger.error("Failed test: %s:\n%s\n", failure[0],
                                "\n".join(["--  %s" % x for x in failure[1].split("\n")]))

        config.logger.info("Test results: %d ran, %d errors, %d failures", result.testsRun, len(result.errors), len(result.failures))

    except Exception as exc:
        import traceback
        config.logger.error("Exception while running test. Tracedump: \n%s", traceback.format_exc(exc))
    finally:
        os.chdir(crt_dir)
    return len(result.failures)
Esempio n. 27
0
    def testHandlerReplacedButCalled(self):
        # If our handler has been replaced (is no longer installed) but is
        # called by the *new* handler, then it isn't safe to delay the
        # SIGINT and we should immediately delegate to the default handler
        unittest.installHandler()

        handler = signal.getsignal(signal.SIGINT)
        def new_handler(frame, signum):
            handler(frame, signum)
        signal.signal(signal.SIGINT, new_handler)

        try:
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
        except KeyboardInterrupt:
            pass
        else:
            self.fail("replaced but delegated handler doesn't raise interrupt")
Esempio n. 28
0
    def testInterruptCaught(self):
        default_handler = signal.getsignal(signal.SIGINT)
        result = unittest.TestResult()
        unittest.installHandler()
        unittest.registerResult(result)
        self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)

        def test(result):
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
            result.breakCaught = True
            self.assertTrue(result.shouldStop)

        try:
            test(result)
        except KeyboardInterrupt:
            self.fail('KeyboardInterrupt not handled')
        self.assertTrue(result.breakCaught)
Esempio n. 29
0
    def run_tests(self, test_suite):
        # Install a signal handler to catch Ctrl-C and display the results
        # (but only if running >2.6).
        if sys.version_info[0] > 2 or sys.version_info[1] > 6:
            unittest.installHandler()

        # Run the tests
        runner = BuckTestRunner(self, test_suite,
                                verbosity=self.options.verbosity,
                                show_output=self.options.show_output)
        result = runner.run(test_suite)

        if self.options.collect_coverage and self.options.show_output:
            self.cov.stop()
            if self.cov.html_report:
                self.cov.html_report()
            else:
                self.cov.report(file=sys.stdout)

        return result
Esempio n. 30
0
    def testSecondInterrupt(self):
        result = unittest.TestResult()
        unittest.installHandler()
        unittest.registerResult(result)

        def test(result):
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
            result.breakCaught = True
            self.assertTrue(result.shouldStop)
            os.kill(pid, signal.SIGINT)
            self.fail("Second KeyboardInterrupt not raised")

        try:
            test(result)
        except KeyboardInterrupt:
            pass
        else:
            self.fail("Second KeyboardInterrupt not raised")
        self.assertTrue(result.breakCaught)
Esempio n. 31
0
def main(argv=None):
    '''Run either all tests, or those specified in argv'''
    if argv is None:
        argv = sys.argv

    # parse options
    covreport = False
    fasttest = False
    fulltest = False
    failfast = False
    loglevel = logging.WARNING
    opts, args = getopt.gnu_getopt(argv[1:], 'hVD', [
        'help', 'coverage', 'fast', 'failfast', 'ff', 'full', 'debug',
        'verbose'
    ])
    for o, a in opts:
        if o in ('-h', '--help'):
            print('''\
usage: %s [OPTIONS] [MODULES]

Where MODULE should a module name from ./tests/
If no module is given the whole test suite is run.

Options:
  -h, --help     print this text
  --fast         skip a number of slower tests and mock filesystem
  --failfast     stop after the first test that fails
  --ff           alias for "--fast --failfast"
  --full         full test for using filesystem without mock
  --coverage     report test coverage statistics
  -V, --verbose  run with verbose output from logging
  -D, --debug    run with debug output from logging
''' % argv[0])
            return
        elif o == '--coverage':
            if coverage:
                covreport = True
            else:
                print('''\
Can not run test coverage without module 'coverage'.
On Ubuntu or Debian install package 'python3-coverage'.
''',
                      file=sys.stderr)
                sys.exit(1)
        elif o == '--fast':
            fasttest = True
            # set before any test classes are loaded !
        elif o == '--failfast':
            failfast = True
        elif o == '--ff':  # --fast --failfast
            fasttest = True
            failfast = True
        elif o == '--full':
            fulltest = True
        elif o in ('-V', '--verbose'):
            loglevel = logging.INFO
        elif o in ('-D', '--debug'):
            loglevel = logging.DEBUG
        else:
            assert False, 'Unkown option: %s' % o

    # Set logging handler (don't use basicConfig here, we already installed stuff)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
    logger = logging.getLogger()
    logger.setLevel(loglevel)
    logger.addHandler(handler)
    #logging.captureWarnings(True) # FIXME - make all test pass with this enabled

    # Start tracing - before importing the tests
    if coverage:
        cov = coverage.coverage(source=['zim'], branch=True)
        cov.erase()  # clean up old date set
        cov.exclude('assert ')
        cov.exclude('raise NotImplementedError')
        cov.start()

    # Build the test suite
    import tests
    tests.FAST_TEST = fasttest
    tests.FULL_TEST = fulltest

    loader = unittest.TestLoader()
    try:
        if args:
            suite = unittest.TestSuite()
            for name in args:
                module = name if name.startswith('tests.') else 'tests.' + name
                test = loader.loadTestsFromName(module)
                suite.addTest(test)
        else:
            suite = tests.load_tests(loader, None, None)
    except AttributeError as error:
        # HACK: unittest raises and attribute errors if import of test script
        # fails try to catch this and show the import error instead - else raise
        # original error
        import re
        m = re.match(r"'module' object has no attribute '(\w+)'",
                     error.args[0])
        if m:
            module = m.group(1)
            m = __import__('tests.' + module)  # should raise ImportError
        raise error

    # And run it
    unittest.installHandler()  # Fancy handling for ^C during test
    result = \
     unittest.TextTestRunner(verbosity=2, failfast=failfast, descriptions=False).run(suite)

    # Stop tracing
    if coverage:
        cov.stop()
        cov.save()

    # Check the modules were loaded from the right location
    # (so no testing based on modules from a previous installed version...)
    mylib = os.path.abspath('./zim')
    if os.name == 'nt':
        mylib = mylib.replace('/', '\\')  # on msys the path is "/" seperated
    for module in [
            m for m in list(sys.modules.keys())
            if m == 'zim' or m.startswith('zim.')
    ]:
        if sys.modules[module] is None:
            continue
        file = sys.modules[module].__file__
        if os.name == 'nt':
            file = file.replace('/', '\\')
        assert file.startswith(mylib), \
         'Module %s was loaded from %s' % (module, file)

    test_report(result, 'test_report.html')
    print('\nWrote test report to test_report.html')

    # print timings
    with open('test_times.csv', 'w') as out:
        for name, time in sorted(tests.TIMINGS,
                                 reverse=True,
                                 key=lambda t: t[1]):
            out.write("%s,%f\n" % (name, time))
        print("Wrote test_times.csv")

    # Create coverage output if asked to do so
    if covreport:
        print('Writing coverage reports...')
        cov.html_report(directory='./coverage', omit=['zim/inc/*'])
        print('Done - Coverage reports can be found in ./coverage/')

    exitcode = 1 if result.errors or result.failures else 0
    sys.exit(exitcode)
Esempio n. 32
0
 def setUpClass(cls):
     logger = logging.getLogger('')
     logger.addHandler(logging.NullHandler())
     unittest.installHandler()
Esempio n. 33
0
    def setUp(self):
        """Handle Ctrl+C signal to stop tests prematurely.
        """

        installHandler()
Esempio n. 34
0
def main():
	logger.info("LuxCore Unit tests")

	try:
		pyluxcore.Init(LuxCoreLogHandler)
		logger.info("LuxCore %s" % pyluxcore.Version())
		logger.info("LuxCore has OpenCL: %r" % pyluxcoreunittests.tests.utils.LuxCoreHasOpenCL())

		# Parse command line options

		parser = argparse.ArgumentParser(description='Runs LuxCore test suite.')
		parser.add_argument('--config',
			help='custom configuration properties for the unit tests')
		parser.add_argument('--resume', action='store_true',
			help='resume a previously interrupted test session')
		parser.add_argument('--filter',
			help='select only the tests matching the specified regular expression')
		parser.add_argument('--list', action='store_true',
			help='list all available tests')
		parser.add_argument('--subset', action='store_true',
			help='list all tests available tests')
		parser.add_argument('--verbose', default=2,
			help='set the verbosity level (i.e 0, 1, 2 or 3)')
		args = parser.parse_args()

		global printLuxCoreLog
		if int(args.verbose) >= 3:
			printLuxCoreLog = True

		if not args.resume:
			# Delete all images in the images directory
			logger.info("Deleting all images...")
			folder = "images"
			for f in [png for png in os.listdir(folder) if png.endswith(".png")]:
				filePath = os.path.join(folder, f)
				os.unlink(filePath)
			logger.info("ok")

		# Read the custom configuration file
		if args.config:
			LuxCoreTest.customConfigProps.SetFromFile(args.config)

		# Mostly used to save time (to not hit the cap) on Travis CI
		pyluxcoreunittests.tests.utils.USE_SUBSET = args.subset

		# Discover all tests

		propertiesSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.properties", top_level_dir=".")
		basicSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.basic", top_level_dir=".")
		lightSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.lights", top_level_dir=".")
		materialSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.materials", top_level_dir=".")
		textureSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.textures", top_level_dir=".")
		sceneSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.scene", top_level_dir=".")
		haltSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.halt", top_level_dir=".")
		serializationSuite = unittest.TestLoader().discover("pyluxcoreunittests.tests.serialization", top_level_dir=".")

		allTests = unittest.TestSuite([propertiesSuite, basicSuite, lightSuite, materialSuite,
			textureSuite, sceneSuite, haltSuite, serializationSuite])

		# List the tests if required

		if args.list:
			logger.info("All tests available:")
			l = ListAllTests(allTests)
			count = 0
			for t in l:
				logger.info("#%d  %s" % (count, t))
				count += 1
			logger.info("%d test(s) listed" % count)
			return

		# Filter the tests if required

		if args.filter:
			logger.info("Filtering tests by: %s" % args.filter)
			allTests = unittest.TestSuite(FilterTests(args.filter, allTests))

		# Skips the already done tests if required

		doneCount = 0
		if args.resume:
			with open("totaltestsdone.txt","r") as f:
				doneCount = int(f.readlines()[0])
			logger.info("Tests already done: %d" % doneCount)

			TailTestsImpl_index = 0
			allTests = unittest.TestSuite(TailTests(doneCount, allTests))

		# To catch Ctrl-C
		unittest.installHandler()

		result = unittest.TextTestRunner(stream=StreamToLogger(), verbosity=int(args.verbose)).run(allTests)
		
		# Save the numebr of tests tun for a potential later resume
		with open("totaltestsdone.txt","w") as f:
			f.write(str(result.testsRun + doneCount) + "\n")

		sys.exit(not result.wasSuccessful())
	finally:
		pyluxcore.SetLogHandler(None)
Esempio n. 35
0
    def run(self):
        os.environ["__BUGZILLA_UNITTEST"] = "1"

        try:
            import coverage
            usecov = int(coverage.__version__.split(".")[0]) >= 3
        except:
            usecov = False

        if usecov:
            cov = coverage.coverage(
                omit=["/*/tests/*", "/usr/*", "*dev-env*", "*.tox/*"])
            cov.erase()
            cov.start()

        testfiles = []
        for t in glob.glob(os.path.join(os.getcwd(), 'tests', '*.py')):
            if t.endswith("__init__.py"):
                continue

            base = os.path.basename(t)
            if (base == "ro_functional.py" and not self.ro_functional):
                continue

            if (base == "rw_functional.py" and not self.rw_functional):
                continue

            testfiles.append('.'.join(['tests', os.path.splitext(base)[0]]))

        if hasattr(unittest, "installHandler"):
            try:
                unittest.installHandler()
            except:
                print("installHandler hack failed")

        import tests as testsmodule
        testsmodule.REDHAT_URL = self.redhat_url
        if self.debug:
            import logging
            import bugzilla
            logging.getLogger(bugzilla.__name__).setLevel(logging.DEBUG)
            os.environ["__BUGZILLA_UNITTEST_DEBUG"] = "1"

        tests = unittest.TestLoader().loadTestsFromNames(testfiles)
        if self.only:
            newtests = []
            for suite1 in tests:
                for suite2 in suite1:
                    for testcase in suite2:
                        if self.only in str(testcase):
                            newtests.append(testcase)

            if not newtests:
                print("--only didn't find any tests")
                sys.exit(1)

            tests = unittest.TestSuite(newtests)
            print("Running only:")
            for test in newtests:
                print("%s" % test)
            print()

        t = unittest.TextTestRunner(verbosity=1)

        result = t.run(tests)

        if usecov:
            cov.stop()
            cov.save()

        err = int(bool(len(result.failures) > 0 or len(result.errors) > 0))
        if not err and usecov:
            cov.report(show_missing=False)
        sys.exit(err)
Esempio n. 36
0
 def setup_test_environment(self, **kwargs):
     setup_test_environment(debug=self.debug_mode)
     unittest.installHandler()
Esempio n. 37
0
        assert response.code == 200, "can't delete entry"

    def testDelWrongParamsErr(self):
        print "HTTP DELETE: wrong params"
        params = '/does_not_exist'
        try:
            response = restclient.delete(serviceUrl=serviceUrl, params=params)
            print response.read()
            assert False, "received unexpected successful response"
        except urllib2.HTTPError, e:
            print e.read()
            assert e.code == 404, "received unexpected unsuccessful response"

    def testDelNoParamsErr(self):
        print "HTTP DELETE: no params"
        try:
            response = restclient.delete(serviceUrl=serviceUrl)
            print response.read()
            assert False, "received unexpected successful response"
        except urllib2.HTTPError, e:
            print e.read()
            assert e.code == 400, "received unexpected unsuccessful response"


# driver method for all tests
if __name__ == "__main__":
    print "Running unit tests:\n"
    suite = unittest.makeSuite(ClusterConfigTestCase, 'test')
    unittest.installHandler()
    unittest.main()
Esempio n. 38
0
def main(argv):
    """
    Main function to launch the unittests as an application

    :param argv: Command line arguments
    :returns: exit status
    """
    from silx.test import utils

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("-v", "--verbose", default=0,
                        action="count", dest="verbose",
                        help="Increase verbosity. Option -v prints additional " +
                             "INFO messages. Use -vv for full verbosity, " +
                             "including debug messages and test help strings.")
    parser.add_argument("--qt-binding", dest="qt_binding", default=None,
                        help="Force using a Qt binding: 'PyQt5' or 'PySide2'")
    utils.test_options.add_parser_argument(parser)

    options = parser.parse_args(argv[1:])

    test_verbosity = 1
    use_buffer = True
    if options.verbose == 1:
        logging.root.setLevel(logging.INFO)
        _logger.info("Set log level: INFO")
        test_verbosity = 2
        use_buffer = False
    elif options.verbose > 1:
        logging.root.setLevel(logging.DEBUG)
        _logger.info("Set log level: DEBUG")
        test_verbosity = 2
        use_buffer = False

    if options.qt_binding:
        binding = options.qt_binding.lower()
        if binding == "pyqt5":
            _logger.info("Force using PyQt5")
            import PyQt5.QtCore  # noqa
        elif binding == "pyside2":
            _logger.info("Force using PySide2")
            import PySide2.QtCore  # noqa
        else:
            raise ValueError("Qt binding '%s' is unknown" % options.qt_binding)

    # Configure test options
    utils.test_options.configure(options)

    # Run the tests
    runnerArgs = {}
    runnerArgs["verbosity"] = test_verbosity
    runnerArgs["buffer"] = use_buffer
    runner = unittest.TextTestRunner(**runnerArgs)
    runner.resultclass = TextTestResultWithSkipList

    # Display the result when using CTRL-C
    unittest.installHandler()

    import silx.test
    test_suite = unittest.TestSuite()
    test_suite.addTest(silx.test.suite())
    result = runner.run(test_suite)

    if result.wasSuccessful():
        exit_status = 0
    else:
        exit_status = 1
    return exit_status
Esempio n. 39
0
 def _run_tests(self) -> None:
     unittest.installHandler()
     runner = unittest.TextTestRunner()
     runner.run(self._test_suite)  # type: ignore
Esempio n. 40
0
    def run(self):
        cov = None
        if self.coverage:
            import coverage
            omit = ["/usr/*", "/*/tests/*", "*progress.py"]
            cov = coverage.coverage(omit=omit)
            cov.erase()
            if not self._external_coverage:
                cov.start()

        import tests as testsmodule
        testsmodule.utils.clistate.regenerate_output = bool(
            self.regenerate_output)
        testsmodule.utils.clistate.use_coverage = bool(cov)
        testsmodule.utils.clistate.debug = bool(self.debug)
        for key, val in self._clistate.items():
            setattr(testsmodule.utils.clistate, key, val)
        testsmodule.setup_logging()
        if self._urlfetcher_mock:
            import tests.urlfetcher_mock
            tests.urlfetcher_mock.setup_mock()

        # This makes the test runner report results before exiting from ctrl-c
        unittest.installHandler()

        tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
        if self.only:
            newtests = []
            for suite1 in tests:
                for suite2 in suite1:
                    for testcase in suite2:
                        if self.only in str(testcase):
                            newtests.append(testcase)

            if not newtests:
                print("--only didn't find any tests")
                sys.exit(1)
            tests = unittest.TestSuite(newtests)
            print("Running only:")
            for test in newtests:
                print("%s" % test)
            print("")

        verbosity = 1
        if self.debug or self.testverbose or self._force_verbose:
            verbosity = 2
        t = unittest.TextTestRunner(verbosity=verbosity)

        try:
            result = t.run(tests)
        except KeyboardInterrupt:
            sys.exit(1)

        if cov:
            if self._external_coverage:
                cov.load()
            else:
                cov.stop()
                cov.save()

        err = int(bool(len(result.failures) > 0 or len(result.errors) > 0))
        if getattr(result, "shouldStop", False):
            # Test was aborted with ctrl-c
            err = True

        if cov and not err:
            if len(result.skipped):
                print("Skipping coverage report because tests were skipped.")
            else:
                cov.report(show_missing=False, skip_covered=True)
        sys.exit(err)
Esempio n. 41
0
 def setup_test_environment(self, **kwargs):
     setup_test_environment()
     settings.DEBUG = False
     unittest.installHandler()
Esempio n. 42
0
 def setup_test_environment(self, **kwargs):
     os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = TEST_HOST
     setup_test_environment()
     settings.DEBUG = True
     unittest.installHandler()