Example #1
0
 def run(self, test):
     """
     Run the test or suite and return a result object.
     """
     test = unittest.decorate(test, ITestCase)
     if self._forceGarbageCollection:
         test = unittest.decorate(test,
                                  unittest._ForceGarbageCollectionDecorator)
     return self._runWithoutDecoration(test)
Example #2
0
 def run(self, test):
     """
     Run the test or suite and return a result object.
     """
     test = unittest.decorate(test, ITestCase)
     if self._forceGarbageCollection:
         test = unittest.decorate(
             test, unittest._ForceGarbageCollectionDecorator)
     return self._runWithoutDecoration(test)
Example #3
0
 def test_decorateDecoratedSuite(self):
     """
     Calling L{decorate} on a test suite with already-decorated tests
     decorates all of the tests in the suite again.
     """
     test = self.TestCase()
     decoratedTest = unittest.decorate(test, unittest.TestDecorator)
     redecoratedTest = unittest.decorate(decoratedTest, unittest.TestDecorator)
     self.assertTestsEqual(redecoratedTest, unittest.TestDecorator(decoratedTest))
 def test_decorateDecoratedSuite(self):
     """
     Calling L{decorate} on a test suite with already-decorated tests
     decorates all of the tests in the suite again.
     """
     test = unittest.TestCase()
     decoratedTest = unittest.decorate(test, unittest.TestDecorator)
     redecoratedTest = unittest.decorate(decoratedTest, unittest.TestDecorator)
     self.assertTestsEqual(redecoratedTest, unittest.TestDecorator(decoratedTest))
Example #5
0
 def run(self, test):
     """
     Run the test or suite and return a result object.
     """
     result = self._makeResult()
     test = unittest.decorate(test, ITestCase)
     if self._forceGarbageCollection:
         test = unittest.decorate(test,
                                  unittest._ForceGarbageCollectionDecorator)
     # decorate the suite with reactor cleanup and log starting
     # This should move out of the runner and be presumed to be
     # present
     suite = TrialSuite([test])
     startTime = time.time()
     if self.mode == self.DRY_RUN:
         suite.visit(DryRunVisitor(result).markSuccessful)
     elif self.mode == self.DEBUG:
         # open question - should this be self.debug() instead.
         debugger = self._getDebugger()
         oldDir = self._setUpTestdir()
         try:
             self._setUpLogging()
             debugger.runcall(suite.run, result)
         finally:
             self._tearDownLogFile()
             os.chdir(oldDir)
     else:
         oldDir = self._setUpTestdir()
         try:
             self._setUpLogging()
             suite.run(result)
         finally:
             self._tearDownLogFile()
             os.chdir(oldDir)
     endTime = time.time()
     done = getattr(result, 'done', None)
     if done is None:
         warnings.warn(
             "%s should implement done() but doesn't. Falling back to "
             "printErrors() and friends." % reflect.qual(result.__class__),
             category=DeprecationWarning,
             stacklevel=2)
         result.printErrors()
         result.writeln(result.separator)
         result.writeln('Ran %d tests in %.3fs', result.testsRun,
                        endTime - startTime)
         result.write('\n')
         result.printSummary()
     else:
         result.done()
     return result
Example #6
0
 def run(self, test):
     """
     Run the test or suite and return a result object.
     """
     result = self._makeResult()
     test = unittest.decorate(test, ITestCase)
     if self._forceGarbageCollection:
         test = unittest.decorate(
             test, unittest._ForceGarbageCollectionDecorator)
     # decorate the suite with reactor cleanup and log starting
     # This should move out of the runner and be presumed to be
     # present
     suite = TrialSuite([test])
     startTime = time.time()
     if self.mode == self.DRY_RUN:
         suite.visit(DryRunVisitor(result).markSuccessful)
     elif self.mode == self.DEBUG:
         # open question - should this be self.debug() instead.
         debugger = self._getDebugger()
         oldDir = self._setUpTestdir()
         try:
             self._setUpLogging()
             debugger.runcall(suite.run, result)
         finally:
             self._tearDownLogFile()
             os.chdir(oldDir)
     else:
         oldDir = self._setUpTestdir()
         try:
             self._setUpLogging()
             suite.run(result)
         finally:
             self._tearDownLogFile()
             os.chdir(oldDir)
     endTime = time.time()
     done = getattr(result, 'done', None)
     if done is None:
         warnings.warn(
             "%s should implement done() but doesn't. Falling back to "
             "printErrors() and friends." % reflect.qual(result.__class__),
             category=DeprecationWarning, stacklevel=2)
         result.printErrors()
         result.writeln(result.separator)
         result.writeln('Ran %d tests in %.3fs', result.testsRun,
                        endTime - startTime)
         result.write('\n')
         result.printSummary()
     else:
         result.done()
     return result
 def test_decorateSingleTest(self):
     """
     Calling L{decorate} on a single test case returns the test case
     decorated with the provided decorator.
     """
     test = self.TestCase()
     decoratedTest = unittest.decorate(test, unittest.TestDecorator)
     self.assertTestsEqual(unittest.TestDecorator(test), decoratedTest)
 def test_decorateSingleTest(self):
     """
     Calling L{decorate} on a single test case returns the test case
     decorated with the provided decorator.
     """
     test = self.TestCase()
     decoratedTest = unittest.decorate(test, unittest.TestDecorator)
     self.assertTestsEqual(unittest.TestDecorator(test), decoratedTest)
Example #9
0
    def test_decorateTestSuiteReferences(self):
        """
        When decorating a test suite in-place, the number of references to the
        test objects in that test suite should stay the same.

        Previously, L{unittest.decorate} recreated a test suite, so the
        original suite kept references to the test objects. This test is here
        to ensure the problem doesn't reappear again.
        """
        getrefcount = getattr(sys, "getrefcount", None)
        if getrefcount is None:
            raise unittest.SkipTest("getrefcount not supported on this platform")
        test = self.TestCase()
        suite = unittest.TestSuite([test])
        count1 = getrefcount(test)
        unittest.decorate(suite, unittest.TestDecorator)
        count2 = getrefcount(test)
        self.assertEqual(count1, count2)
Example #10
0
 def __init__(self, tests=(), forceGarbageCollection=False):
     if forceGarbageCollection:
         newTests = []
         for test in tests:
             test = unittest.decorate(test, _ForceGarbageCollectionDecorator)
             newTests.append(test)
         tests = newTests
     suite = LoggedSuite(tests)
     super(TrialSuite, self).__init__([suite])
 def test_decoratePreservesSuite(self):
     """
     Tests can be in non-standard suites. L{decorate} preserves the
     non-standard suites when it decorates the tests.
     """
     test = unittest.TestCase()
     suite = runner.DestructiveTestSuite([test])
     decorated = unittest.decorate(suite, unittest.TestDecorator)
     self.assertSuitesEqual(decorated, runner.DestructiveTestSuite([unittest.TestDecorator(test)]))
 def test_decorateInPlaceMutatesOriginal(self):
     """
     Calling L{decorate} on a test suite will mutate the original suite.
     """
     test = unittest.TestCase()
     suite = unittest.TestSuite([test])
     decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
     self.assertSuitesEqual(decoratedTest, unittest.TestSuite([unittest.TestDecorator(test)]))
     self.assertSuitesEqual(suite, unittest.TestSuite([unittest.TestDecorator(test)]))
 def test_decorateTestSuite(self):
     """
     Calling L{decorate} on a test suite will return a test suite with
     each test decorated with the provided decorator.
     """
     test = unittest.TestCase()
     suite = unittest.TestSuite([test])
     decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
     self.assertSuitesEqual(decoratedTest, unittest.TestSuite([unittest.TestDecorator(test)]))
Example #14
0
 def __init__(self, tests=(), forceGarbageCollection=False):
     if forceGarbageCollection:
         newTests = []
         for test in tests:
             test = unittest.decorate(test, _ForceGarbageCollectionDecorator)
             newTests.append(test)
         tests = newTests
     suite = LoggedSuite(tests)
     super().__init__([suite])
    def test_decorateTestSuiteReferences(self):
        """
        When decorating a test suite in-place, the number of references to the
        test objects in that test suite should stay the same.

        Previously, L{unittest.decorate} recreated a test suite, so the
        original suite kept references to the test objects. This test is here
        to ensure the problem doesn't reappear again.
        """
        getrefcount = getattr(sys, 'getrefcount', None)
        if getrefcount is None:
            raise unittest.SkipTest(
                "getrefcount not supported on this platform")
        test = self.TestCase()
        suite = unittest.TestSuite([test])
        count1 = getrefcount(test)
        unittest.decorate(suite, unittest.TestDecorator)
        count2 = getrefcount(test)
        self.assertEqual(count1, count2)
 def test_decorateTestSuite(self):
     """
     Calling L{decorate} on a test suite will return a test suite with
     each test decorated with the provided decorator.
     """
     test = self.TestCase()
     suite = unittest.TestSuite([test])
     decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
     self.assertSuitesEqual(
         decoratedTest, unittest.TestSuite([unittest.TestDecorator(test)]))
 def test_decoratePreservesSuite(self):
     """
     Tests can be in non-standard suites. L{decorate} preserves the
     non-standard suites when it decorates the tests.
     """
     test = self.TestCase()
     suite = runner.DestructiveTestSuite([test])
     decorated = unittest.decorate(suite, unittest.TestDecorator)
     self.assertSuitesEqual(
         decorated,
         runner.DestructiveTestSuite([unittest.TestDecorator(test)]))
 def test_decorateInPlaceMutatesOriginal(self):
     """
     Calling L{decorate} on a test suite will mutate the original suite.
     """
     test = self.TestCase()
     suite = unittest.TestSuite([test])
     decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
     self.assertSuitesEqual(
         decoratedTest, unittest.TestSuite([unittest.TestDecorator(test)]))
     self.assertSuitesEqual(
         suite, unittest.TestSuite([unittest.TestDecorator(test)]))
 def test_decorateNestedTestSuite(self):
     """
     Calling L{decorate} on a test suite with nested suites will return a
     test suite that maintains the same structure, but with all tests
     decorated.
     """
     test = unittest.TestCase()
     suite = unittest.TestSuite([unittest.TestSuite([test])])
     decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
     expected = unittest.TestSuite([unittest.TestSuite([unittest.TestDecorator(test)])])
     self.assertSuitesEqual(decoratedTest, expected)
 def test_decorateNestedTestSuite(self):
     """
     Calling L{decorate} on a test suite with nested suites will return a
     test suite that maintains the same structure, but with all tests
     decorated.
     """
     test = self.TestCase()
     suite = unittest.TestSuite([unittest.TestSuite([test])])
     decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
     expected = unittest.TestSuite(
         [unittest.TestSuite([unittest.TestDecorator(test)])])
     self.assertSuitesEqual(decoratedTest, expected)
Example #21
0
 def testDoctestError(self):
     from twisted.trial.test import erroneous
     suite = unittest.decorate(
         self.loader.loadDoctests(erroneous), itrial.ITestCase)
     output = self.getOutput(suite)
     path = 'twisted.trial.test.erroneous.unexpectedException'
     for substring in ['1/0', 'ZeroDivisionError',
                       'Exception raised:', path]:
         self.assertSubstring(substring, output)
     self.failUnless(re.search('Fail(ed|ure in) example:', output),
                     "Couldn't match 'Failure in example: ' "
                     "or 'Failed example: '")
     expect = [self.doubleSeparator,
               re.compile(r'\[(ERROR|FAIL)\]: .*' + re.escape(path))]
     self.stringComparison(expect, output.splitlines())
Example #22
0
 def test_collectCalledWhenTearDownClass(self):
     """
     test gc.collect is called after tearDownClass.
     """
     test = unittest.TestSuite(
         [TestGarbageCollection.ClassTest('test_1'),
          TestGarbageCollection.ClassTest('test_2')])
     test = unittest.decorate(
         test, unittest._ForceGarbageCollectionDecorator)
     result = reporter.TestResult()
     test.run(result)
     # check that collect gets called after individual tests, and
     # after tearDownClass
     self.failUnlessEqual(
         self._collectCalled,
         ['collect', 'test1', 'collect',
          'collect', 'test2', 'tearDownClass', 'collect'])
Example #23
0
    def loadSortedPackages(self, sorter=runner.name):
        """
        Verify that packages are loaded in the correct order.
        """
        import uberpackage

        self.loader.sorter = sorter
        suite = self.loader.loadPackage(uberpackage, recurse=True)
        # XXX: Work around strange, unexplained Zope crap.
        # jml, 2007-11-15.
        suite = unittest.decorate(suite, ITestCase)
        resultingTests = list(unittest._iterateTests(suite))
        manifest = list(self._trialSortAlgorithm(sorter))
        for number, (manifestTest, actualTest) in enumerate(zip(manifest, resultingTests)):
            self.assertEqual(
                manifestTest.name, actualTest.id(), "#%d: %s != %s" % (number, manifestTest.name, actualTest.id())
            )
        self.assertEqual(len(manifest), len(resultingTests))
Example #24
0
 def loadSortedPackages(self, sorter=runner.name):
     """
     Verify that packages are loaded in the correct order.
     """
     import uberpackage
     self.loader.sorter = sorter
     suite = self.loader.loadPackage(uberpackage, recurse=True)
     # XXX: Work around strange, unexplained Zope crap.
     # jml, 2007-11-15.
     suite = unittest.decorate(suite, ITestCase)
     resultingTests = list(unittest._iterateTests(suite))
     manifest = list(self._trialSortAlgorithm(sorter))
     for number, (manifestTest,
                  actualTest) in enumerate(zip(manifest, resultingTests)):
         self.assertEqual(
             manifestTest.name, actualTest.id(),
             "#%d: %s != %s" % (number, manifestTest.name, actualTest.id()))
     self.assertEqual(len(manifest), len(resultingTests))
Example #25
0
 def test_collectCalledWhenTearDownClass(self):
     """
     test gc.collect is called after tearDownClass.
     """
     test = unittest.TestSuite([
         TestGarbageCollection.ClassTest('test_1'),
         TestGarbageCollection.ClassTest('test_2')
     ])
     test = unittest.decorate(test,
                              unittest._ForceGarbageCollectionDecorator)
     result = reporter.TestResult()
     test.run(result)
     # check that collect gets called after individual tests, and
     # after tearDownClass
     self.failUnlessEqual(self._collectCalled, [
         'collect', 'test1', 'collect', 'collect', 'test2', 'tearDownClass',
         'collect'
     ])
Example #26
0
 def testDoctestError(self):
     from twisted.trial.test import erroneous
     suite = unittest.decorate(self.loader.loadDoctests(erroneous),
                               itrial.ITestCase)
     output = self.getOutput(suite)
     path = 'twisted.trial.test.erroneous.unexpectedException'
     for substring in [
             '1/0', 'ZeroDivisionError', 'Exception raised:', path
     ]:
         self.assertSubstring(substring, output)
     self.failUnless(
         re.search('Fail(ed|ure in) example:', output),
         "Couldn't match 'Failure in example: ' "
         "or 'Failed example: '")
     expect = [
         self.doubleSeparator,
         re.compile(r'\[(ERROR|FAIL)\]: .*' + re.escape(path))
     ]
     self.stringComparison(expect, output.splitlines())