Exemple #1
0
 def test_moduleImportFailureIgnored(self):
     "toProtoTestList() does not raise errors when doing completions"
     suite = MagicMock()
     suite.__class__.__name__ = str('ModuleImportFailure')
     suite.__str__.return_value = "exception_method other_stuff"
     suite.exception_method.side_effect = AttributeError
     self.assertEqual(loader.toProtoTestList(suite, doing_completions=True), [])
Exemple #2
0
 def test_moduleImportFailureIgnored(self):
     """
     toProtoTestList() does not raise errors when doing completions
     """
     suite = MagicMock()
     suite.__class__.__name__ = str("ModuleImportFailure")
     suite.__str__.return_value = "exception_method other_stuff"
     suite.exception_method.side_effect = AttributeError
     self.assertEqual(loader.toProtoTestList(suite, doing_completions=True), [])
Exemple #3
0
    def run(self, suite):
        "Run the given test case or test suite."
        result = GreenTestResult(
                self.stream, self.descriptions, self.verbosity, html=self.html,
                termcolor=self.termcolor)
        registerResult(result)
        with warnings.catch_warnings():
            if self.warnings:
                # if self.warnings is set, use it to filter all the warnings
                warnings.simplefilter(self.warnings)
                # if the filter is 'default' or 'always', special-case the
                # warnings from the deprecated unittest methods to show them
                # no more than once per module, because they can be fairly
                # noisy.  The -Wd and -Wa flags can be used to bypass this
                # only when self.warnings is None.
                if self.warnings in ['default', 'always']:
                    warnings.filterwarnings('module',
                            category=DeprecationWarning,
                            message='Please use assert\w+ instead.')

            result.startTestRun()

            if self.subprocesses == 1:
                suite.run(result)
            else:
                tests = toProtoTestList(suite)
                pool = LoggingDaemonlessPool(processes=self.subprocesses)
                if tests:
                    async_responses = []
                    for index, test in enumerate(tests):
                        if self.run_coverage:
                            coverage_number = index + 1
                        else:
                            coverage_number = None
                        async_responses.append(pool.apply_async(
                            poolRunner,
                            (test.dotted_name, coverage_number, self.omit)))
                    pool.close()
                    for test, async_response in zip(tests, async_responses):
                        # Prints out the white 'processing...' version of the output
                        result.startTest(test)
                        # This blocks until the worker who is processing this
                        # particular test actually finishes
                        result.addProtoTestResult(async_response.get())
                pool.terminate()
                pool.join()

            result.stopTestRun()

        return result
Exemple #4
0
 def test_BigDirWithAbsoluteImports(self):
     """
     Big dir discovers tests and doesn't crash on absolute import
     """
     sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
     pkg_name = os.path.basename(sub_tmpdir)
     # Child setup
     # pkg/__init__.py
     fh = open(os.path.join(sub_tmpdir, "__init__.py"), "w")
     fh.write("\n")
     fh.close()
     # pkg/target_module.py
     fh = open(os.path.join(sub_tmpdir, "target_module.py"), "w")
     fh.write("a = 1\n")
     fh.close()
     # pkg/test/__init__.py
     os.mkdir(os.path.join(sub_tmpdir, "test"))
     fh = open(os.path.join(sub_tmpdir, "test", "__init__.py"), "w")
     fh.write("\n")
     fh.close()
     # pkg/test/test_target_module.py
     fh = open(os.path.join(sub_tmpdir, "test", "test_target_module.py"), "w")
     fh.write(
         dedent(
             """
         import unittest
         import {}.target_module
         class A(unittest.TestCase):
             def testPass(self):
                 pass
         """.format(
                 pkg_name
             )
         )
     )
     fh.close()
     # Load the tests
     os.chdir(self.tmpdir)
     test_suite = self.loader.loadTargets(pkg_name)
     self.assertEqual(test_suite.countTestCases(), 1)
     # Dotted name should start with the package!
     self.assertEqual(
         pkg_name + ".test.test_target_module.A.testPass",
         loader.toProtoTestList(test_suite)[0].dotted_name,
     )
Exemple #5
0
 def test_BigDirWithAbsoluteImports(self):
     """
     Big dir discovers tests and doesn't crash on absolute import
     """
     sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
     pkg_name = os.path.basename(sub_tmpdir)
     # Child setup
     # pkg/__init__.py
     fh = open(os.path.join(sub_tmpdir, "__init__.py"), "w")
     fh.write("\n")
     fh.close()
     # pkg/target_module.py
     fh = open(os.path.join(sub_tmpdir, "target_module.py"), "w")
     fh.write("a = 1\n")
     fh.close()
     # pkg/test/__init__.py
     os.mkdir(os.path.join(sub_tmpdir, "test"))
     fh = open(os.path.join(sub_tmpdir, "test", "__init__.py"), "w")
     fh.write("\n")
     fh.close()
     # pkg/test/test_target_module.py
     fh = open(os.path.join(sub_tmpdir, "test", "test_target_module.py"), "w")
     fh.write(
         dedent(
             """
         import unittest
         import {}.target_module
         class A(unittest.TestCase):
             def testPass(self):
                 pass
         """.format(
                 pkg_name
             )
         )
     )
     fh.close()
     # Load the tests
     os.chdir(self.tmpdir)
     test_suite = loader.loadTargets(pkg_name)
     self.assertEqual(test_suite.countTestCases(), 1)
     # Dotted name should start with the package!
     self.assertEqual(
         pkg_name + ".test.test_target_module.A.testPass", loader.toProtoTestList(test_suite)[0].dotted_name
     )
Exemple #6
0
def run(suite, stream, args):
    """
    Run the given test case or test suite with the specified arguments.

    Any args.stream passed in will be wrapped in a GreenStream
    """
    if not issubclass(GreenStream, type(stream)):
        stream = GreenStream(stream)
    result = GreenTestResult(args, stream)

    # Note: Catching SIGINT isn't supported by Python on windows (python
    # "WONTFIX" issue 18040)
    installHandler()
    registerResult(result)

    with warnings.catch_warnings():
        if args.warnings:
            # if args.warnings is set, use it to filter all the warnings
            warnings.simplefilter(args.warnings)
            # if the filter is 'default' or 'always', special-case the
            # warnings from the deprecated unittest methods to show them
            # no more than once per module, because they can be fairly
            # noisy.  The -Wd and -Wa flags can be used to bypass this
            # only when args.warnings is None.
            if args.warnings in ['default', 'always']:
                warnings.filterwarnings('module',
                        category=DeprecationWarning,
                        message='Please use assert\w+ instead.')

        result.startTestRun()

        tests = toProtoTestList(suite)
        pool = LoggingDaemonlessPool(processes=args.subprocesses or None)
        if tests:
            async_responses = []
            for index, test in enumerate(tests):
                if args.run_coverage:
                    coverage_number = index + 1
                else:
                    coverage_number = None
                async_responses.append(pool.apply_async(
                    poolRunner,
                    (test.dotted_name, coverage_number, args.omit_patterns)))
            pool.close()
            for test, async_response in zip(tests, async_responses):
                # Prints out the white 'processing...' version of the output
                result.startTest(test)
                # This blocks until the worker who is processing this
                # particular test actually finishes
                try:
                    result.addProtoTestResult(async_response.get())
                except KeyboardInterrupt: # pragma: no cover
                    result.shouldStop = True
                if result.shouldStop:
                    break
        pool.terminate()
        pool.join()

        result.stopTestRun()

    removeResult(result)

    return result