add Linux_i686

This commit is contained in:
j 2014-05-17 18:11:40 +00:00 committed by Ubuntu
commit 95cd9b11f2
1644 changed files with 564260 additions and 0 deletions

View file

@ -0,0 +1,52 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
#
# Maintainer: Jonathan Lange
"""
Asynchronous unit testing framework.
Trial extends Python's builtin C{unittest} to provide support for asynchronous
tests.
Maintainer: Jonathan Lange
Trial strives to be compatible with other Python xUnit testing frameworks.
"Compatibility" is a difficult things to define. In practice, it means that:
- L{twisted.trial.unittest.TestCase} objects should be able to be used by
other test runners without those runners requiring special support for
Trial tests.
- Tests that subclass the standard library C{TestCase} and don't do anything
"too weird" should be able to be discoverable and runnable by the Trial
test runner without the authors of those tests having to jump through
hoops.
- Tests that implement the interface provided by the standard library
C{TestCase} should be runnable by the Trial runner.
- The Trial test runner and Trial L{unittest.TestCase} objects ought to be
able to use standard library C{TestResult} objects, and third party
C{TestResult} objects based on the standard library.
This list is not necessarily exhaustive -- compatibility is hard to define.
Contributors who discover more helpful ways of defining compatibility are
encouraged to update this document.
Examples:
B{Timeouts} for tests should be implemented in the runner. If this is done,
then timeouts could work for third-party TestCase objects as well as for
L{twisted.trial.unittest.TestCase} objects. Further, Twisted C{TestCase}
objects will run in other runners without timing out.
See U{http://twistedmatrix.com/trac/ticket/2675}.
Running tests in a temporary directory should be a feature of the test case,
because often tests themselves rely on this behaviour. If the feature is
implemented in the runner, then tests will change behaviour (possibly
breaking) when run in a different test runner. Further, many tests don't even
care about the filesystem.
See U{http://twistedmatrix.com/trac/ticket/2916}.
"""

View file

@ -0,0 +1,184 @@
# -*- test-case-name: twisted.trial.test -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Infrastructure for test running and suites.
"""
import doctest
import gc
from twisted.python import components
from twisted.trial import itrial, reporter
from twisted.trial._synctest import _logObserver
pyunit = __import__('unittest')
from zope.interface import implements
class TestSuite(pyunit.TestSuite):
"""
Extend the standard library's C{TestSuite} with a consistently overrideable
C{run} method.
"""
def run(self, result):
"""
Call C{run} on every member of the suite.
"""
for test in self._tests:
if result.shouldStop:
break
test(result)
return result
class TestDecorator(components.proxyForInterface(itrial.ITestCase,
"_originalTest")):
"""
Decorator for test cases.
@param _originalTest: The wrapped instance of test.
@type _originalTest: A provider of L{itrial.ITestCase}
"""
implements(itrial.ITestCase)
def __call__(self, result):
"""
Run the unit test.
@param result: A TestResult object.
"""
return self.run(result)
def run(self, result):
"""
Run the unit test.
@param result: A TestResult object.
"""
return self._originalTest.run(
reporter._AdaptedReporter(result, self.__class__))
def _clearSuite(suite):
"""
Clear all tests from C{suite}.
This messes with the internals of C{suite}. In particular, it assumes that
the suite keeps all of its tests in a list in an instance variable called
C{_tests}.
"""
suite._tests = []
def decorate(test, decorator):
"""
Decorate all test cases in C{test} with C{decorator}.
C{test} can be a test case or a test suite. If it is a test suite, then the
structure of the suite is preserved.
L{decorate} tries to preserve the class of the test suites it finds, but
assumes the presence of the C{_tests} attribute on the suite.
@param test: The C{TestCase} or C{TestSuite} to decorate.
@param decorator: A unary callable used to decorate C{TestCase}s.
@return: A decorated C{TestCase} or a C{TestSuite} containing decorated
C{TestCase}s.
"""
try:
tests = iter(test)
except TypeError:
return decorator(test)
# At this point, we know that 'test' is a test suite.
_clearSuite(test)
for case in tests:
test.addTest(decorate(case, decorator))
return test
class _PyUnitTestCaseAdapter(TestDecorator):
"""
Adapt from pyunit.TestCase to ITestCase.
"""
class _BrokenIDTestCaseAdapter(_PyUnitTestCaseAdapter):
"""
Adapter for pyunit-style C{TestCase} subclasses that have undesirable id()
methods. That is C{unittest.FunctionTestCase} and C{unittest.DocTestCase}.
"""
def id(self):
"""
Return the fully-qualified Python name of the doctest.
"""
testID = self._originalTest.shortDescription()
if testID is not None:
return testID
return self._originalTest.id()
class _ForceGarbageCollectionDecorator(TestDecorator):
"""
Forces garbage collection to be run before and after the test. Any errors
logged during the post-test collection are added to the test result as
errors.
"""
def run(self, result):
gc.collect()
TestDecorator.run(self, result)
_logObserver._add()
gc.collect()
for error in _logObserver.getErrors():
result.addError(self, error)
_logObserver.flushErrors()
_logObserver._remove()
components.registerAdapter(
_PyUnitTestCaseAdapter, pyunit.TestCase, itrial.ITestCase)
components.registerAdapter(
_BrokenIDTestCaseAdapter, pyunit.FunctionTestCase, itrial.ITestCase)
_docTestCase = getattr(doctest, 'DocTestCase', None)
if _docTestCase:
components.registerAdapter(
_BrokenIDTestCaseAdapter, _docTestCase, itrial.ITestCase)
def _iterateTests(testSuiteOrCase):
"""
Iterate through all of the test cases in C{testSuiteOrCase}.
"""
try:
suite = iter(testSuiteOrCase)
except TypeError:
yield testSuiteOrCase
else:
for test in suite:
for subtest in _iterateTests(test):
yield subtest

View file

@ -0,0 +1,405 @@
# -*- test-case-name: twisted.trial.test -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Things likely to be used by writers of unit tests.
Maintainer: Jonathan Lange
"""
from __future__ import division, absolute_import
import inspect
import warnings
from zope.interface import implementer
# We can't import reactor at module-level because this code runs before trial
# installs a user-specified reactor, installing the default reactor and
# breaking reactor installation. See also #6047.
from twisted.internet import defer, utils
from twisted.python import failure
from twisted.trial import itrial, util
from twisted.trial._synctest import (
FailTest, SkipTest, SynchronousTestCase)
_wait_is_running = []
@implementer(itrial.ITestCase)
class TestCase(SynchronousTestCase):
"""
A unit test. The atom of the unit testing universe.
This class extends L{SynchronousTestCase} which extends C{unittest.TestCase}
from the standard library. The main feature is the ability to return
C{Deferred}s from tests and fixture methods and to have the suite wait for
those C{Deferred}s to fire. Also provides new assertions such as
L{assertFailure}.
@ivar timeout: A real number of seconds. If set, the test will
raise an error if it takes longer than C{timeout} seconds.
If not set, util.DEFAULT_TIMEOUT_DURATION is used.
"""
def __init__(self, methodName='runTest'):
"""
Construct an asynchronous test case for C{methodName}.
@param methodName: The name of a method on C{self}. This method should
be a unit test. That is, it should be a short method that calls some of
the assert* methods. If C{methodName} is unspecified,
L{SynchronousTestCase.runTest} will be used as the test method. This is
mostly useful for testing Trial.
"""
super(TestCase, self).__init__(methodName)
def assertFailure(self, deferred, *expectedFailures):
"""
Fail if C{deferred} does not errback with one of C{expectedFailures}.
Returns the original Deferred with callbacks added. You will need
to return this Deferred from your test case.
"""
def _cb(ignore):
raise self.failureException(
"did not catch an error, instead got %r" % (ignore,))
def _eb(failure):
if failure.check(*expectedFailures):
return failure.value
else:
output = ('\nExpected: %r\nGot:\n%s'
% (expectedFailures, str(failure)))
raise self.failureException(output)
return deferred.addCallbacks(_cb, _eb)
failUnlessFailure = assertFailure
def _run(self, methodName, result):
from twisted.internet import reactor
timeout = self.getTimeout()
def onTimeout(d):
e = defer.TimeoutError("%r (%s) still running at %s secs"
% (self, methodName, timeout))
f = failure.Failure(e)
# try to errback the deferred that the test returns (for no gorram
# reason) (see issue1005 and test_errorPropagation in
# test_deferred)
try:
d.errback(f)
except defer.AlreadyCalledError:
# if the deferred has been called already but the *back chain
# is still unfinished, crash the reactor and report timeout
# error ourself.
reactor.crash()
self._timedOut = True # see self._wait
todo = self.getTodo()
if todo is not None and todo.expected(f):
result.addExpectedFailure(self, f, todo)
else:
result.addError(self, f)
onTimeout = utils.suppressWarnings(
onTimeout, util.suppress(category=DeprecationWarning))
method = getattr(self, methodName)
if inspect.isgeneratorfunction(method):
exc = TypeError(
'%r is a generator function and therefore will never run' % (
method,))
return defer.fail(exc)
d = defer.maybeDeferred(
utils.runWithWarningsSuppressed, self._getSuppress(), method)
call = reactor.callLater(timeout, onTimeout, d)
d.addBoth(lambda x : call.active() and call.cancel() or x)
return d
def __call__(self, *args, **kwargs):
return self.run(*args, **kwargs)
def deferSetUp(self, ignored, result):
d = self._run('setUp', result)
d.addCallbacks(self.deferTestMethod, self._ebDeferSetUp,
callbackArgs=(result,),
errbackArgs=(result,))
return d
def _ebDeferSetUp(self, failure, result):
if failure.check(SkipTest):
result.addSkip(self, self._getSkipReason(self.setUp, failure.value))
else:
result.addError(self, failure)
if failure.check(KeyboardInterrupt):
result.stop()
return self.deferRunCleanups(None, result)
def deferTestMethod(self, ignored, result):
d = self._run(self._testMethodName, result)
d.addCallbacks(self._cbDeferTestMethod, self._ebDeferTestMethod,
callbackArgs=(result,),
errbackArgs=(result,))
d.addBoth(self.deferRunCleanups, result)
d.addBoth(self.deferTearDown, result)
return d
def _cbDeferTestMethod(self, ignored, result):
if self.getTodo() is not None:
result.addUnexpectedSuccess(self, self.getTodo())
else:
self._passed = True
return ignored
def _ebDeferTestMethod(self, f, result):
todo = self.getTodo()
if todo is not None and todo.expected(f):
result.addExpectedFailure(self, f, todo)
elif f.check(self.failureException, FailTest):
result.addFailure(self, f)
elif f.check(KeyboardInterrupt):
result.addError(self, f)
result.stop()
elif f.check(SkipTest):
result.addSkip(
self,
self._getSkipReason(getattr(self, self._testMethodName), f.value))
else:
result.addError(self, f)
def deferTearDown(self, ignored, result):
d = self._run('tearDown', result)
d.addErrback(self._ebDeferTearDown, result)
return d
def _ebDeferTearDown(self, failure, result):
result.addError(self, failure)
if failure.check(KeyboardInterrupt):
result.stop()
self._passed = False
def deferRunCleanups(self, ignored, result):
"""
Run any scheduled cleanups and report errors (if any to the result
object.
"""
d = self._runCleanups()
d.addCallback(self._cbDeferRunCleanups, result)
return d
def _cbDeferRunCleanups(self, cleanupResults, result):
for flag, failure in cleanupResults:
if flag == defer.FAILURE:
result.addError(self, failure)
if failure.check(KeyboardInterrupt):
result.stop()
self._passed = False
def _cleanUp(self, result):
try:
clean = util._Janitor(self, result).postCaseCleanup()
if not clean:
self._passed = False
except:
result.addError(self, failure.Failure())
self._passed = False
for error in self._observer.getErrors():
result.addError(self, error)
self._passed = False
self.flushLoggedErrors()
self._removeObserver()
if self._passed:
result.addSuccess(self)
def _classCleanUp(self, result):
try:
util._Janitor(self, result).postClassCleanup()
except:
result.addError(self, failure.Failure())
def _makeReactorMethod(self, name):
"""
Create a method which wraps the reactor method C{name}. The new
method issues a deprecation warning and calls the original.
"""
def _(*a, **kw):
warnings.warn("reactor.%s cannot be used inside unit tests. "
"In the future, using %s will fail the test and may "
"crash or hang the test run."
% (name, name),
stacklevel=2, category=DeprecationWarning)
return self._reactorMethods[name](*a, **kw)
return _
def _deprecateReactor(self, reactor):
"""
Deprecate C{iterate}, C{crash} and C{stop} on C{reactor}. That is,
each method is wrapped in a function that issues a deprecation
warning, then calls the original.
@param reactor: The Twisted reactor.
"""
self._reactorMethods = {}
for name in ['crash', 'iterate', 'stop']:
self._reactorMethods[name] = getattr(reactor, name)
setattr(reactor, name, self._makeReactorMethod(name))
def _undeprecateReactor(self, reactor):
"""
Restore the deprecated reactor methods. Undoes what
L{_deprecateReactor} did.
@param reactor: The Twisted reactor.
"""
for name, method in self._reactorMethods.items():
setattr(reactor, name, method)
self._reactorMethods = {}
def _runCleanups(self):
"""
Run the cleanups added with L{addCleanup} in order.
@return: A C{Deferred} that fires when all cleanups are run.
"""
def _makeFunction(f, args, kwargs):
return lambda: f(*args, **kwargs)
callables = []
while len(self._cleanups) > 0:
f, args, kwargs = self._cleanups.pop()
callables.append(_makeFunction(f, args, kwargs))
return util._runSequentially(callables)
def _runFixturesAndTest(self, result):
"""
Really run C{setUp}, the test method, and C{tearDown}. Any of these may
return L{defer.Deferred}s. After they complete, do some reactor cleanup.
@param result: A L{TestResult} object.
"""
from twisted.internet import reactor
self._deprecateReactor(reactor)
self._timedOut = False
try:
d = self.deferSetUp(None, result)
try:
self._wait(d)
finally:
self._cleanUp(result)
self._classCleanUp(result)
finally:
self._undeprecateReactor(reactor)
def addCleanup(self, f, *args, **kwargs):
"""
Extend the base cleanup feature with support for cleanup functions which
return Deferreds.
If the function C{f} returns a Deferred, C{TestCase} will wait until the
Deferred has fired before proceeding to the next function.
"""
return super(TestCase, self).addCleanup(f, *args, **kwargs)
def getSuppress(self):
return self._getSuppress()
def getTimeout(self):
"""
Returns the timeout value set on this test. Checks on the instance
first, then the class, then the module, then packages. As soon as it
finds something with a C{timeout} attribute, returns that. Returns
L{util.DEFAULT_TIMEOUT_DURATION} if it cannot find anything. See
L{TestCase} docstring for more details.
"""
timeout = util.acquireAttribute(self._parents, 'timeout',
util.DEFAULT_TIMEOUT_DURATION)
try:
return float(timeout)
except (ValueError, TypeError):
# XXX -- this is here because sometimes people will have methods
# called 'timeout', or set timeout to 'orange', or something
# Particularly, test_news.NewsTestCase and ReactorCoreTestCase
# both do this.
warnings.warn("'timeout' attribute needs to be a number.",
category=DeprecationWarning)
return util.DEFAULT_TIMEOUT_DURATION
def _wait(self, d, running=_wait_is_running):
"""Take a Deferred that only ever callbacks. Block until it happens.
"""
if running:
raise RuntimeError("_wait is not reentrant")
from twisted.internet import reactor
results = []
def append(any):
if results is not None:
results.append(any)
def crash(ign):
if results is not None:
reactor.crash()
crash = utils.suppressWarnings(
crash, util.suppress(message=r'reactor\.crash cannot be used.*',
category=DeprecationWarning))
def stop():
reactor.crash()
stop = utils.suppressWarnings(
stop, util.suppress(message=r'reactor\.crash cannot be used.*',
category=DeprecationWarning))
running.append(None)
try:
d.addBoth(append)
if results:
# d might have already been fired, in which case append is
# called synchronously. Avoid any reactor stuff.
return
d.addBoth(crash)
reactor.stop = stop
try:
reactor.run()
finally:
del reactor.stop
# If the reactor was crashed elsewhere due to a timeout, hopefully
# that crasher also reported an error. Just return.
# _timedOut is most likely to be set when d has fired but hasn't
# completed its callback chain (see self._run)
if results or self._timedOut: #defined in run() and _run()
return
# If the timeout didn't happen, and we didn't get a result or
# a failure, then the user probably aborted the test, so let's
# just raise KeyboardInterrupt.
# FIXME: imagine this:
# web/test/test_webclient.py:
# exc = self.assertRaises(error.Error, wait, method(url))
#
# wait() will raise KeyboardInterrupt, and assertRaises will
# swallow it. Therefore, wait() raising KeyboardInterrupt is
# insufficient to stop trial. A suggested solution is to have
# this code set a "stop trial" flag, or otherwise notify trial
# that it should really try to stop as soon as possible.
raise KeyboardInterrupt()
finally:
results = None
running.pop()

View file

@ -0,0 +1,47 @@
# -*- test-case-name: twisted.trial._dist.test -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
This package implements the distributed Trial test runner:
- The L{twisted.trial._dist.disttrial} module implements a test runner which
runs in a manager process and can launch additional worker processes in
which to run tests and gather up results from all of them.
- The L{twisted.trial._dist.options} module defines command line options used
to configure the distributed test runner.
- The L{twisted.trial._dist.managercommands} module defines AMP commands
which are sent from worker processes back to the manager process to report
the results of tests.
- The L{twisted.trial._dist.workercommands} module defines AMP commands which
are sent from the manager process to the worker processes to control the
execution of tests there.
- The L{twisted.trial._dist.distreporter} module defines a proxy for
L{twisted.trial.itrial.IReporter} which enforces the typical requirement
that results be passed to a reporter for only one test at a time, allowing
any reporter to be used with despite disttrial's simultaneously running
tests.
- The L{twisted.trial._dist.workerreporter} module implements a
L{twisted.trial.itrial.IReporter} which is used by worker processes and
reports results back to the manager process using AMP commands.
- The L{twisted.trial._dist.workertrial} module is a runnable script which is
the main point for worker processes.
- The L{twisted.trial._dist.worker} process defines the manager's AMP
protocol for accepting results from worker processes and a process protocol
for use running workers as local child processes (as opposed to
distributing them to another host).
@since: 12.3
"""
# File descriptors numbers used to set up pipes with the worker.
_WORKER_AMP_STDIN = 3
_WORKER_AMP_STDOUT = 4

View file

@ -0,0 +1,94 @@
# -*- test-case-name: twisted.trial._dist.test.test_distreporter -*-
#
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
The reporter is not made to support concurrent test running, so we will
hold test results in here and only send them to the reporter once the
test is over.
@since: 12.3
"""
from zope.interface import implements
from twisted.trial.itrial import IReporter
from twisted.python.components import proxyForInterface
class DistReporter(proxyForInterface(IReporter)):
"""
See module docstring.
"""
implements(IReporter)
def __init__(self, original):
super(DistReporter, self).__init__(original)
self.running = {}
def startTest(self, test):
"""
Queue test starting.
"""
self.running[test.id()] = []
self.running[test.id()].append((self.original.startTest, test))
def addFailure(self, test, fail):
"""
Queue adding a failure.
"""
self.running[test.id()].append((self.original.addFailure,
test, fail))
def addError(self, test, error):
"""
Queue error adding.
"""
self.running[test.id()].append((self.original.addError,
test, error))
def addSkip(self, test, reason):
"""
Queue adding a skip.
"""
self.running[test.id()].append((self.original.addSkip,
test, reason))
def addUnexpectedSuccess(self, test, todo):
"""
Queue adding an unexpected success.
"""
self.running[test.id()].append((self.original.addUnexpectedSuccess,
test, todo))
def addExpectedFailure(self, test, error, todo):
"""
Queue adding an unexpected failure.
"""
self.running[test.id()].append((self.original.addExpectedFailure,
test, error, todo))
def addSuccess(self, test):
"""
Queue adding a success.
"""
self.running[test.id()].append((self.original.addSuccess, test))
def stopTest(self, test):
"""
Queue stopping the test, then unroll the queue.
"""
self.running[test.id()].append((self.original.stopTest, test))
for step in self.running[test.id()]:
apply(step[0], step[1:])
del self.running[test.id()]

View file

@ -0,0 +1,258 @@
# -*- test-case-name: twisted.trial._dist.test.test_disttrial -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
This module containts the trial distributed runner, the management class
responsible for coordinating all of trial's behavior at the highest level.
@since: 12.3
"""
import os
import sys
from twisted.python.filepath import FilePath
from twisted.python.modules import theSystemPath
from twisted.internet.defer import DeferredList
from twisted.internet.task import cooperate
from twisted.trial.util import _unusedTestDirectory
from twisted.trial.unittest import _iterateTests
from twisted.trial._dist.worker import LocalWorker, LocalWorkerAMP
from twisted.trial._dist.distreporter import DistReporter
from twisted.trial.reporter import UncleanWarningsReporterWrapper
from twisted.trial._dist import _WORKER_AMP_STDIN, _WORKER_AMP_STDOUT
class DistTrialRunner(object):
"""
A specialized runner for distributed trial. The runner launches a number of
local worker processes which will run tests.
@ivar _workerNumber: the number of workers to be spawned.
@type _workerNumber: C{int}
@ivar _stream: stream which the reporter will use.
@ivar _reporterFactory: the reporter class to be used.
"""
_distReporterFactory = DistReporter
def _makeResult(self):
"""
Make reporter factory, and wrap it with a L{DistReporter}.
"""
reporter = self._reporterFactory(self._stream, self._tbformat,
realtime=self._rterrors)
if self._uncleanWarnings:
reporter = UncleanWarningsReporterWrapper(reporter)
return self._distReporterFactory(reporter)
def __init__(self, reporterFactory, workerNumber, workerArguments,
stream=None,
tracebackFormat='default',
realTimeErrors=False,
uncleanWarnings=False,
logfile='test.log',
workingDirectory='_trial_temp'):
self._workerNumber = workerNumber
self._workerArguments = workerArguments
self._reporterFactory = reporterFactory
if stream is None:
stream = sys.stdout
self._stream = stream
self._tbformat = tracebackFormat
self._rterrors = realTimeErrors
self._uncleanWarnings = uncleanWarnings
self._result = None
self._workingDirectory = workingDirectory
self._logFile = logfile
self._logFileObserver = None
self._logFileObject = None
self._logWarnings = False
def writeResults(self, result):
"""
Write test run final outcome to result.
@param result: A C{TestResult} which will print errors and the summary.
"""
result.done()
def createLocalWorkers(self, protocols, workingDirectory):
"""
Create local worker protocol instances and return them.
@param protocols: An iterable of L{LocalWorkerAMP} instances.
@param workingDirectory: The base path in which we should run the
workers.
@type workingDirectory: C{str}
@return: A list of C{quantity} C{LocalWorker} instances.
"""
return [LocalWorker(protocol,
os.path.join(workingDirectory, str(x)),
self._logFile)
for x, protocol in enumerate(protocols)]
def launchWorkerProcesses(self, spawner, protocols, arguments):
"""
Spawn processes from a list of process protocols.
@param spawner: A C{IReactorProcess.spawnProcess} implementation.
@param protocols: An iterable of C{ProcessProtocol} instances.
@param arguments: Extra arguments passed to the processes.
"""
workertrialPath = theSystemPath[
'twisted.trial._dist.workertrial'].filePath.path
childFDs = {0: 'w', 1: 'r', 2: 'r', _WORKER_AMP_STDIN: 'w',
_WORKER_AMP_STDOUT: 'r'}
environ = os.environ.copy()
# Add a environment variable containing the raw sys.path, to be used by
# subprocesses to make sure it's identical to the parent. See
# workertrial._setupPath.
environ['TRIAL_PYTHONPATH'] = os.pathsep.join(sys.path)
for worker in protocols:
args = [sys.executable, workertrialPath]
args.extend(arguments)
spawner(worker, sys.executable, args=args, childFDs=childFDs,
env=environ)
def _driveWorker(self, worker, result, testCases, cooperate):
"""
Drive a L{LocalWorkerAMP} instance, iterating the tests and calling
C{run} for every one of them.
@param worker: The L{LocalWorkerAMP} to drive.
@param result: The global L{DistReporter} instance.
@param testCases: The global list of tests to iterate.
@param cooperate: The cooperate function to use, to be customized in
tests.
@type cooperate: C{function}
@return: A C{Deferred} firing when all the tests are finished.
"""
def resultErrback(error, case):
result.original.addFailure(case, error)
return error
def task(case):
d = worker.run(case, result)
d.addErrback(resultErrback, case)
return d
return cooperate(task(case) for case in testCases).whenDone()
def run(self, suite, reactor=None, cooperate=cooperate,
untilFailure=False):
"""
Spawn local worker processes and load tests. After that, run them.
@param suite: A tests suite to be run.
@param reactor: The reactor to use, to be customized in tests.
@type reactor: A provider of
L{twisted.internet.interfaces.IReactorProcess}
@param cooperate: The cooperate function to use, to be customized in
tests.
@type cooperate: C{function}
@param untilFailure: If C{True}, continue to run the tests until they
fail.
@type untilFailure: C{bool}.
@return: The test result.
@rtype: L{DistReporter}
"""
if reactor is None:
from twisted.internet import reactor
result = self._makeResult()
count = suite.countTestCases()
self._stream.write("Running %d tests.\n" % (count,))
if not count:
# Take a shortcut if there is no test
suite.run(result.original)
self.writeResults(result)
return result
testDir, testDirLock = _unusedTestDirectory(
FilePath(self._workingDirectory))
workerNumber = min(count, self._workerNumber)
ampWorkers = [LocalWorkerAMP() for x in xrange(workerNumber)]
workers = self.createLocalWorkers(ampWorkers, testDir.path)
processEndDeferreds = [worker.endDeferred for worker in workers]
self.launchWorkerProcesses(reactor.spawnProcess, workers,
self._workerArguments)
def runTests():
testCases = iter(list(_iterateTests(suite)))
workerDeferreds = []
for worker in ampWorkers:
workerDeferreds.append(
self._driveWorker(worker, result, testCases,
cooperate=cooperate))
return DeferredList(workerDeferreds, consumeErrors=True,
fireOnOneErrback=True)
stopping = []
def nextRun(ign):
self.writeResults(result)
if not untilFailure:
return
if not result.wasSuccessful():
return
d = runTests()
return d.addCallback(nextRun)
def stop(ign):
testDirLock.unlock()
if not stopping:
stopping.append(None)
reactor.stop()
def beforeShutDown():
if not stopping:
stopping.append(None)
d = DeferredList(processEndDeferreds, consumeErrors=True)
return d.addCallback(continueShutdown)
def continueShutdown(ign):
self.writeResults(result)
return ign
d = runTests()
d.addCallback(nextRun)
d.addBoth(stop)
reactor.addSystemEventTrigger('before', 'shutdown', beforeShutDown)
reactor.run()
return result
def runUntilFailure(self, suite):
"""
Run the tests with local worker processes until they fail.
@param suite: A tests suite to be run.
"""
return self.run(suite, untilFailure=True)

View file

@ -0,0 +1,76 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Commands for reporting test success of failure to the manager.
@since: 12.3
"""
from twisted.protocols.amp import Command, String, Boolean, ListOf
class AddSuccess(Command):
"""
Add a success.
"""
arguments = [('testName', String())]
response = [('success', Boolean())]
class AddError(Command):
"""
Add an error.
"""
arguments = [('testName', String()), ('error', String()),
('errorClass', String()), ('frames', ListOf(String()))]
response = [('success', Boolean())]
class AddFailure(Command):
"""
Add a failure.
"""
arguments = [('testName', String()), ('fail', String()),
('failClass', String()), ('frames', ListOf(String()))]
response = [('success', Boolean())]
class AddSkip(Command):
"""
Add a skip.
"""
arguments = [('testName', String()), ('reason', String())]
response = [('success', Boolean())]
class AddExpectedFailure(Command):
"""
Add an expected failure.
"""
arguments = [('testName', String()), ('error', String()),
('todo', String())]
response = [('success', Boolean())]
class AddUnexpectedSuccess(Command):
"""
Add an unexpected success.
"""
arguments = [('testName', String()), ('todo', String())]
response = [('success', Boolean())]
class TestWrite(Command):
"""
Write test log.
"""
arguments = [('out', String())]
response = [('success', Boolean())]

View file

@ -0,0 +1,30 @@
# -*- test-case-name: twisted.trial._dist.test.test_options -*-
#
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Options handling specific to trial's workers.
@since: 12.3
"""
from twisted.python.filepath import FilePath
from twisted.python.usage import Options
from twisted.scripts.trial import _BasicOptions
from twisted.application.app import ReactorSelectionMixin
class WorkerOptions(_BasicOptions, Options, ReactorSelectionMixin):
"""
Options forwarded to the trial distributed worker.
"""
def coverdir(self):
"""
Return a L{FilePath} representing the directory into which coverage
results should be written.
"""
return FilePath('coverage')

View file

@ -0,0 +1,6 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Distributed trial test runner tests.
"""

View file

@ -0,0 +1,62 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for L{twisted.trial._dist.distreporter}.
"""
from cStringIO import StringIO
from twisted.trial._dist.distreporter import DistReporter
from twisted.trial.unittest import TestCase
from twisted.trial.reporter import TreeReporter
class DistReporterTestCase(TestCase):
"""
Tests for L{DistReporter}.
"""
def setUp(self):
self.stream = StringIO()
self.distReporter = DistReporter(TreeReporter(self.stream))
self.test = TestCase()
def test_startSuccessStop(self):
"""
Success output only gets sent to the stream after the test has stopped.
"""
self.distReporter.startTest(self.test)
self.assertEqual(self.stream.getvalue(), "")
self.distReporter.addSuccess(self.test)
self.assertEqual(self.stream.getvalue(), "")
self.distReporter.stopTest(self.test)
self.assertNotEqual(self.stream.getvalue(), "")
def test_startErrorStop(self):
"""
Error output only gets sent to the stream after the test has stopped.
"""
self.distReporter.startTest(self.test)
self.assertEqual(self.stream.getvalue(), "")
self.distReporter.addError(self.test, "error")
self.assertEqual(self.stream.getvalue(), "")
self.distReporter.stopTest(self.test)
self.assertNotEqual(self.stream.getvalue(), "")
def test_forwardedMethods(self):
"""
Calling methods of L{DistReporter} add calls to the running queue of
the test.
"""
self.distReporter.startTest(self.test)
self.distReporter.addFailure(self.test, "foo")
self.distReporter.addError(self.test, "bar")
self.distReporter.addSkip(self.test, "egg")
self.distReporter.addUnexpectedSuccess(self.test, "spam")
self.distReporter.addExpectedFailure(self.test, "err", "foo")
self.assertEqual(len(self.distReporter.running[self.test.id()]), 6)

View file

@ -0,0 +1,375 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for L{twisted.trial._dist.disttrial}.
"""
import os
import sys
from cStringIO import StringIO
from twisted.internet.protocol import ProcessProtocol
from twisted.internet.defer import fail, succeed
from twisted.internet.task import Cooperator, deferLater
from twisted.internet.main import CONNECTION_DONE
from twisted.internet import reactor
from twisted.python.failure import Failure
from twisted.python.lockfile import FilesystemLock
from twisted.test.test_cooperator import FakeScheduler
from twisted.trial.unittest import TestCase
from twisted.trial.reporter import Reporter, TreeReporter
from twisted.trial.reporter import UncleanWarningsReporterWrapper
from twisted.trial.runner import TrialSuite, ErrorHolder
from twisted.trial._dist.disttrial import DistTrialRunner
from twisted.trial._dist.distreporter import DistReporter
from twisted.trial._dist.worker import LocalWorker
class FakeTransport(object):
"""
A simple fake process transport.
"""
def writeToChild(self, fd, data):
"""
Ignore write calls.
"""
class FakeReactor(object):
"""
A simple fake reactor for testing purposes.
"""
spawnCount = 0
stopCount = 0
runCount = 0
def spawnProcess(self, worker, *args, **kwargs):
worker.makeConnection(FakeTransport())
self.spawnCount += 1
def stop(self):
self.stopCount += 1
def run(self):
self.runCount += 1
def addSystemEventTrigger(self, *args, **kw):
pass
class DistTrialRunnerTestCase(TestCase):
"""
Tests for L{DistTrialRunner}.
"""
def setUp(self):
"""
Create a runner for testing.
"""
self.runner = DistTrialRunner(TreeReporter, 4, [],
workingDirectory=self.mktemp())
self.runner._stream = StringIO()
def test_writeResults(self):
"""
L{DistTrialRunner.writeResults} writes to the stream specified in the
init.
"""
stringIO = StringIO()
result = DistReporter(Reporter(stringIO))
self.runner.writeResults(result)
self.assertTrue(stringIO.tell() > 0)
def test_createLocalWorkers(self):
"""
C{createLocalWorkers} iterates the list of protocols and create one
L{LocalWorker} for each.
"""
protocols = [object() for x in xrange(4)]
workers = self.runner.createLocalWorkers(protocols, "path")
for s in workers:
self.assertIsInstance(s, LocalWorker)
self.assertEqual(4, len(workers))
def test_launchWorkerProcesses(self):
"""
Given a C{spawnProcess} function, C{launchWorkerProcess} launches a
python process with a existing path as its argument.
"""
protocols = [ProcessProtocol() for i in range(4)]
arguments = []
environment = {}
def fakeSpawnProcess(processProtocol, executable, args=(), env={},
path=None, uid=None, gid=None, usePTY=0,
childFDs=None):
arguments.append(executable)
arguments.extend(args)
environment.update(env)
self.runner.launchWorkerProcesses(
fakeSpawnProcess, protocols, ["foo"])
self.assertEqual(arguments[0], arguments[1])
self.assertTrue(os.path.exists(arguments[2]))
self.assertEqual("foo", arguments[3])
self.assertEqual(os.pathsep.join(sys.path),
environment["TRIAL_PYTHONPATH"])
def test_run(self):
"""
C{run} starts the reactor exactly once and spawns each of the workers
exactly once.
"""
fakeReactor = FakeReactor()
suite = TrialSuite()
for i in xrange(10):
suite.addTest(TestCase())
self.runner.run(suite, fakeReactor)
self.assertEqual(fakeReactor.runCount, 1)
self.assertEqual(fakeReactor.spawnCount, self.runner._workerNumber)
def test_runUsedDirectory(self):
"""
L{DistTrialRunner} checks if the test directory is already locked, and
if it is generates a name based on it.
"""
class FakeReactorWithLock(FakeReactor):
def spawnProcess(oself, worker, *args, **kwargs):
self.assertEqual(os.path.abspath(worker._logDirectory),
os.path.abspath(
os.path.join(workingDirectory + "-1",
str(oself.spawnCount))))
localLock = FilesystemLock(workingDirectory + "-1.lock")
self.assertFalse(localLock.lock())
oself.spawnCount += 1
worker.makeConnection(FakeTransport())
worker._ampProtocol.run = lambda *args: succeed(None)
newDirectory = self.mktemp()
os.mkdir(newDirectory)
workingDirectory = os.path.join(newDirectory, "_trial_temp")
lock = FilesystemLock(workingDirectory + ".lock")
lock.lock()
self.addCleanup(lock.unlock)
self.runner._workingDirectory = workingDirectory
fakeReactor = FakeReactorWithLock()
suite = TrialSuite()
for i in xrange(10):
suite.addTest(TestCase())
self.runner.run(suite, fakeReactor)
def test_minimalWorker(self):
"""
L{DistTrialRunner} doesn't try to start more workers than the number of
tests.
"""
fakeReactor = FakeReactor()
self.runner.run(TestCase(), fakeReactor)
self.assertEqual(fakeReactor.runCount, 1)
self.assertEqual(fakeReactor.spawnCount, 1)
def test_runUncleanWarnings(self):
"""
Running with the C{unclean-warnings} option makes L{DistTrialRunner}
uses the L{UncleanWarningsReporterWrapper}.
"""
fakeReactor = FakeReactor()
self.runner._uncleanWarnings = True
result = self.runner.run(TestCase(), fakeReactor)
self.assertIsInstance(result, DistReporter)
self.assertIsInstance(result.original,
UncleanWarningsReporterWrapper)
def test_runWithoutTest(self):
"""
When the suite contains no test, L{DistTrialRunner} takes a shortcut
path without launching any process or starting the reactor.
"""
fakeReactor = object()
suite = TrialSuite()
result = self.runner.run(suite, fakeReactor)
self.assertIsInstance(result, DistReporter)
output = self.runner._stream.getvalue()
self.assertIn("Running 0 test", output)
self.assertIn("PASSED", output)
def test_runWithoutTestButWithAnError(self):
"""
Even if there is no test, the suite can contain an error (most likely,
an import error): this should make the run fail, and the error should
be printed.
"""
fakeReactor = object()
error = ErrorHolder("an error", Failure(RuntimeError("foo bar")))
result = self.runner.run(error, fakeReactor)
self.assertIsInstance(result, DistReporter)
output = self.runner._stream.getvalue()
self.assertIn("Running 0 test", output)
self.assertIn("foo bar", output)
self.assertIn("an error", output)
self.assertIn("errors=1", output)
self.assertIn("FAILED", output)
def test_runUnexpectedError(self):
"""
If for some reasons we can't connect to the worker process, the test
suite catches and fails.
"""
class FakeReactorWithFail(FakeReactor):
def spawnProcess(self, worker, *args, **kwargs):
worker.makeConnection(FakeTransport())
self.spawnCount += 1
worker._ampProtocol.run = self.failingRun
def failingRun(self, case, result):
return fail(RuntimeError("oops"))
scheduler = FakeScheduler()
cooperator = Cooperator(scheduler=scheduler)
fakeReactor = FakeReactorWithFail()
result = self.runner.run(TestCase(), fakeReactor,
cooperator.cooperate)
self.assertEqual(fakeReactor.runCount, 1)
self.assertEqual(fakeReactor.spawnCount, 1)
scheduler.pump()
self.assertEqual(1, len(result.original.failures))
def test_runStopAfterTests(self):
"""
L{DistTrialRunner} calls C{reactor.stop} and unlocks the test directory
once the tests have run.
"""
functions = []
class FakeReactorWithSuccess(FakeReactor):
def spawnProcess(self, worker, *args, **kwargs):
worker.makeConnection(FakeTransport())
self.spawnCount += 1
worker._ampProtocol.run = self.succeedingRun
def succeedingRun(self, case, result):
return succeed(None)
def addSystemEventTrigger(oself, phase, event, function):
self.assertEqual('before', phase)
self.assertEqual('shutdown', event)
functions.append(function)
workingDirectory = self.runner._workingDirectory
fakeReactor = FakeReactorWithSuccess()
self.runner.run(TestCase(), fakeReactor)
def check():
localLock = FilesystemLock(workingDirectory + ".lock")
self.assertTrue(localLock.lock())
self.assertEqual(1, fakeReactor.stopCount)
# We don't wait for the process deferreds here, so nothign is
# returned by the function before shutdown
self.assertIdentical(None, functions[0]())
return deferLater(reactor, 0, check)
def test_runWaitForProcessesDeferreds(self):
"""
L{DistTrialRunner} waits for the worker processes to stop when the
reactor is stopping, and then unlocks the test directory, not trying to
stop the reactor again.
"""
functions = []
workers = []
class FakeReactorWithEvent(FakeReactor):
def spawnProcess(self, worker, *args, **kwargs):
worker.makeConnection(FakeTransport())
workers.append(worker)
def addSystemEventTrigger(oself, phase, event, function):
self.assertEqual('before', phase)
self.assertEqual('shutdown', event)
functions.append(function)
workingDirectory = self.runner._workingDirectory
fakeReactor = FakeReactorWithEvent()
self.runner.run(TestCase(), fakeReactor)
def check(ign):
# Let the AMP deferreds fire
return deferLater(reactor, 0, realCheck)
def realCheck():
localLock = FilesystemLock(workingDirectory + ".lock")
self.assertTrue(localLock.lock())
# Stop is not called, as it ought to have been called before
self.assertEqual(0, fakeReactor.stopCount)
workers[0].processEnded(Failure(CONNECTION_DONE))
return functions[0]().addCallback(check)
def test_runUntilFailure(self):
"""
L{DistTrialRunner} can run in C{untilFailure} mode where it will run
the given tests until they fail.
"""
called = []
class FakeReactorWithSuccess(FakeReactor):
def spawnProcess(self, worker, *args, **kwargs):
worker.makeConnection(FakeTransport())
self.spawnCount += 1
worker._ampProtocol.run = self.succeedingRun
def succeedingRun(self, case, result):
called.append(None)
if len(called) == 5:
return fail(RuntimeError("oops"))
return succeed(None)
fakeReactor = FakeReactorWithSuccess()
scheduler = FakeScheduler()
cooperator = Cooperator(scheduler=scheduler)
result = self.runner.run(
TestCase(), fakeReactor, cooperate=cooperator.cooperate,
untilFailure=True)
scheduler.pump()
self.assertEqual(5, len(called))
self.assertFalse(result.wasSuccessful())
output = self.runner._stream.getvalue()
self.assertIn("PASSED", output)
self.assertIn("FAIL", output)

View file

@ -0,0 +1,48 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for distributed trial's options management.
"""
import os, sys, gc
from twisted.trial.unittest import TestCase
from twisted.trial._dist.options import WorkerOptions
class WorkerOptionsTestCase(TestCase):
"""
Tests for L{WorkerOptions}.
"""
def setUp(self):
"""
Build an L{WorkerOptions} object to be used in the tests.
"""
self.options = WorkerOptions()
def test_standardOptions(self):
"""
L{WorkerOptions} supports a subset of standard options supported by
trial.
"""
self.addCleanup(sys.setrecursionlimit, sys.getrecursionlimit())
if gc.isenabled():
self.addCleanup(gc.enable)
gc.enable()
self.options.parseOptions(["--recursionlimit", "2000", "--disablegc"])
self.assertEqual(2000, sys.getrecursionlimit())
self.assertFalse(gc.isenabled())
def test_coverage(self):
"""
L{WorkerOptions.coverdir} returns the C{coverage} child directory of
the current directory to be used for storing coverage data.
"""
self.assertEqual(
os.path.realpath(os.path.join(os.getcwd(), "coverage")),
self.options.coverdir().path)

View file

@ -0,0 +1,473 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Test for distributed trial worker side.
"""
import os
from cStringIO import StringIO
from zope.interface.verify import verifyObject
from twisted.trial.reporter import TestResult
from twisted.trial.unittest import TestCase
from twisted.trial._dist.worker import (
LocalWorker, LocalWorkerAMP, LocalWorkerTransport, WorkerProtocol)
from twisted.trial._dist import managercommands, workercommands
from twisted.scripts import trial
from twisted.test.proto_helpers import StringTransport
from twisted.internet.interfaces import ITransport, IAddress
from twisted.internet.defer import fail, succeed
from twisted.internet.main import CONNECTION_DONE
from twisted.internet.error import ConnectionDone
from twisted.python.failure import Failure
from twisted.protocols.amp import AMP
class FakeAMP(AMP):
"""
A fake amp protocol.
"""
class WorkerProtocolTestCase(TestCase):
"""
Tests for L{WorkerProtocol}.
"""
def setUp(self):
"""
Set up a transport, a result stream and a protocol instance.
"""
self.serverTransport = StringTransport()
self.clientTransport = StringTransport()
self.server = WorkerProtocol()
self.server.makeConnection(self.serverTransport)
self.client = FakeAMP()
self.client.makeConnection(self.clientTransport)
def test_run(self):
"""
Calling the L{workercommands.Run} command on the client returns a
response with C{success} sets to C{True}.
"""
d = self.client.callRemote(workercommands.Run, testCase="doesntexist")
def check(result):
self.assertTrue(result['success'])
d.addCallback(check)
self.server.dataReceived(self.clientTransport.value())
self.clientTransport.clear()
self.client.dataReceived(self.serverTransport.value())
self.serverTransport.clear()
return d
def test_start(self):
"""
The C{start} command changes the current path.
"""
curdir = os.path.realpath(os.path.curdir)
self.addCleanup(os.chdir, curdir)
self.server.start('..')
self.assertNotEqual(os.path.realpath(os.path.curdir), curdir)
class LocalWorkerAMPTestCase(TestCase):
"""
Test case for distributed trial's manager-side local worker AMP protocol
"""
def setUp(self):
self.managerTransport = StringTransport()
self.managerAMP = LocalWorkerAMP()
self.managerAMP.makeConnection(self.managerTransport)
self.result = TestResult()
self.workerTransport = StringTransport()
self.worker = AMP()
self.worker.makeConnection(self.workerTransport)
config = trial.Options()
self.testName = "twisted.doesnexist"
config['tests'].append(self.testName)
self.testCase = trial._getSuite(config)._tests.pop()
self.managerAMP.run(self.testCase, self.result)
self.managerTransport.clear()
def pumpTransports(self):
"""
Sends data from C{self.workerTransport} to C{self.managerAMP}, and then
data from C{self.managerTransport} back to C{self.worker}.
"""
self.managerAMP.dataReceived(self.workerTransport.value())
self.workerTransport.clear()
self.worker.dataReceived(self.managerTransport.value())
def test_runSuccess(self):
"""
Run a test, and succeed.
"""
results = []
d = self.worker.callRemote(managercommands.AddSuccess,
testName=self.testName)
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertTrue(results)
def test_runExpectedFailure(self):
"""
Run a test, and fail expectedly.
"""
results = []
d = self.worker.callRemote(managercommands.AddExpectedFailure,
testName=self.testName, error='error',
todo='todoReason')
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertEqual(self.testCase, self.result.expectedFailures[0][0])
self.assertTrue(results)
def test_runError(self):
"""
Run a test, and encounter an error.
"""
results = []
d = self.worker.callRemote(managercommands.AddError,
testName=self.testName, error='error',
errorClass='exceptions.ValueError',
frames=[])
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertEqual(self.testCase, self.result.errors[0][0])
self.assertTrue(results)
def test_runErrorWithFrames(self):
"""
L{LocalWorkerAMP._buildFailure} recreates the C{Failure.frames} from
the C{frames} argument passed to C{AddError}.
"""
results = []
d = self.worker.callRemote(managercommands.AddError,
testName=self.testName, error='error',
errorClass='exceptions.ValueError',
frames=["file.py", "invalid code", "3"])
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertEqual(self.testCase, self.result.errors[0][0])
self.assertEqual(
[('file.py', 'invalid code', 3, [], [])],
self.result.errors[0][1].frames)
self.assertTrue(results)
def test_runFailure(self):
"""
Run a test, and fail.
"""
results = []
d = self.worker.callRemote(managercommands.AddFailure,
testName=self.testName, fail='fail',
failClass='exceptions.RuntimeError',
frames=[])
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertEqual(self.testCase, self.result.failures[0][0])
self.assertTrue(results)
def test_runSkip(self):
"""
Run a test, but skip it.
"""
results = []
d = self.worker.callRemote(managercommands.AddSkip,
testName=self.testName, reason='reason')
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertEqual(self.testCase, self.result.skips[0][0])
self.assertTrue(results)
def test_runUnexpectedSuccesses(self):
"""
Run a test, and succeed unexpectedly.
"""
results = []
d = self.worker.callRemote(managercommands.AddUnexpectedSuccess,
testName=self.testName,
todo='todo')
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertEqual(self.testCase, self.result.unexpectedSuccesses[0][0])
self.assertTrue(results)
def test_testWrite(self):
"""
L{LocalWorkerAMP.testWrite} writes the data received to its test
stream.
"""
results = []
stream = StringIO()
self.managerAMP.setTestStream(stream)
d = self.worker.callRemote(managercommands.TestWrite,
out="Some output")
d.addCallback(lambda result: results.append(result['success']))
self.pumpTransports()
self.assertEqual("Some output\n", stream.getvalue())
self.assertTrue(results)
def test_stopAfterRun(self):
"""
L{LocalWorkerAMP.run} calls C{stopTest} on its test result once the
C{Run} commands has succeeded.
"""
result = object()
stopped = []
def fakeCallRemote(command, testCase):
return succeed(result)
self.managerAMP.callRemote = fakeCallRemote
class StopTestResult(TestResult):
def stopTest(self, test):
stopped.append(test)
d = self.managerAMP.run(self.testCase, StopTestResult())
self.assertEqual([self.testCase], stopped)
return d.addCallback(self.assertIdentical, result)
class FakeAMProtocol(AMP):
"""
A fake implementation of L{AMP} for testing.
"""
id = 0
dataString = ""
def dataReceived(self, data):
self.dataString += data
def setTestStream(self, stream):
self.testStream = stream
class FakeTransport(object):
"""
A fake process transport implementation for testing.
"""
dataString = ""
calls = 0
def writeToChild(self, fd, data):
self.dataString += data
def loseConnection(self):
self.calls += 1
class LocalWorkerTestCase(TestCase):
"""
Tests for L{LocalWorker} and L{LocalWorkerTransport}.
"""
def test_childDataReceived(self):
"""
L{LocalWorker.childDataReceived} forwards the received data to linked
L{AMP} protocol if the right file descriptor, otherwise forwards to
C{ProcessProtocol.childDataReceived}.
"""
fakeTransport = FakeTransport()
localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log')
localWorker.makeConnection(fakeTransport)
localWorker._outLog = StringIO()
localWorker.childDataReceived(4, "foo")
localWorker.childDataReceived(1, "bar")
self.assertEqual("foo", localWorker._ampProtocol.dataString)
self.assertEqual("bar", localWorker._outLog.getvalue())
def test_outReceived(self):
"""
L{LocalWorker.outReceived} logs the output into its C{_outLog} log
file.
"""
fakeTransport = FakeTransport()
localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log')
localWorker.makeConnection(fakeTransport)
localWorker._outLog = StringIO()
data = "The quick brown fox jumps over the lazy dog"
localWorker.outReceived(data)
self.assertEqual(data, localWorker._outLog.getvalue())
def test_errReceived(self):
"""
L{LocalWorker.errReceived} logs the errors into its C{_errLog} log
file.
"""
fakeTransport = FakeTransport()
localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log')
localWorker.makeConnection(fakeTransport)
localWorker._errLog = StringIO()
data = "The quick brown fox jumps over the lazy dog"
localWorker.errReceived(data)
self.assertEqual(data, localWorker._errLog.getvalue())
def test_write(self):
"""
L{LocalWorkerTransport.write} forwards the written data to the given
transport.
"""
transport = FakeTransport()
localTransport = LocalWorkerTransport(transport)
data = "The quick brown fox jumps over the lazy dog"
localTransport.write(data)
self.assertEqual(data, transport.dataString)
def test_writeSequence(self):
"""
L{LocalWorkerTransport.writeSequence} forwards the written data to the
given transport.
"""
transport = FakeTransport()
localTransport = LocalWorkerTransport(transport)
data = ("The quick ", "brown fox jumps ", "over the lazy dog")
localTransport.writeSequence(data)
self.assertEqual("".join(data), transport.dataString)
def test_loseConnection(self):
"""
L{LocalWorkerTransport.loseConnection} forwards the call to the given
transport.
"""
transport = FakeTransport()
localTransport = LocalWorkerTransport(transport)
localTransport.loseConnection()
self.assertEqual(transport.calls, 1)
def test_connectionLost(self):
"""
L{LocalWorker.connectionLost} closes the log streams.
"""
class FakeStream(object):
callNumber = 0
def close(self):
self.callNumber += 1
transport = FakeTransport()
localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log')
localWorker.makeConnection(transport)
localWorker._outLog = FakeStream()
localWorker._errLog = FakeStream()
localWorker.connectionLost(None)
self.assertEqual(localWorker._outLog.callNumber, 1)
self.assertEqual(localWorker._errLog.callNumber, 1)
def test_processEnded(self):
"""
L{LocalWorker.processEnded} calls C{connectionLost} on itself and on
the L{AMP} protocol.
"""
class FakeStream(object):
callNumber = 0
def close(self):
self.callNumber += 1
transport = FakeTransport()
protocol = FakeAMProtocol()
localWorker = LocalWorker(protocol, '.', 'test.log')
localWorker.makeConnection(transport)
localWorker._outLog = FakeStream()
localWorker.processEnded(Failure(CONNECTION_DONE))
self.assertEqual(localWorker._outLog.callNumber, 1)
self.assertIdentical(None, protocol.transport)
return self.assertFailure(localWorker.endDeferred, ConnectionDone)
def test_addresses(self):
"""
L{LocalWorkerTransport.getPeer} and L{LocalWorkerTransport.getHost}
return L{IAddress} objects.
"""
localTransport = LocalWorkerTransport(None)
self.assertTrue(verifyObject(IAddress, localTransport.getPeer()))
self.assertTrue(verifyObject(IAddress, localTransport.getHost()))
def test_transport(self):
"""
L{LocalWorkerTransport} implements L{ITransport} to be able to be used
by L{AMP}.
"""
localTransport = LocalWorkerTransport(None)
self.assertTrue(verifyObject(ITransport, localTransport))
def test_startError(self):
"""
L{LocalWorker} swallows the exceptions returned by the L{AMP} protocol
start method, as it generates unnecessary errors.
"""
def failCallRemote(command, directory):
return fail(RuntimeError("oops"))
transport = FakeTransport()
protocol = FakeAMProtocol()
protocol.callRemote = failCallRemote
localWorker = LocalWorker(protocol, '.', 'test.log')
localWorker.makeConnection(transport)
self.assertEqual([], self.flushLoggedErrors(RuntimeError))

View file

@ -0,0 +1,119 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for L{twisted.trial._dist.workerreporter}.
"""
from twisted.python.failure import Failure
from twisted.trial.unittest import TestCase, Todo
from twisted.trial._dist.workerreporter import WorkerReporter
from twisted.trial._dist import managercommands
class FakeAMProtocol(object):
"""
A fake C{AMP} implementations to track C{callRemote} calls.
"""
id = 0
lastCall = None
def callRemote(self, command, **kwargs):
self.lastCall = command
class WorkerReporterTestCase(TestCase):
"""
Tests for L{WorkerReporter}.
"""
def setUp(self):
self.fakeAMProtocol = FakeAMProtocol()
self.workerReporter = WorkerReporter(self.fakeAMProtocol)
self.test = TestCase()
def test_addSuccess(self):
"""
L{WorkerReporter.addSuccess} sends a L{managercommands.AddSuccess}
command.
"""
self.workerReporter.addSuccess(self.test)
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddSuccess)
def test_addError(self):
"""
L{WorkerReporter.addError} sends a L{managercommands.AddError} command.
"""
self.workerReporter.addError(self.test, Failure(RuntimeError('error')))
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddError)
def test_addErrorTuple(self):
"""
Adding an error using L{WorkerReporter.addError} as a
C{sys.exc_info}-style tuple sends an L{managercommands.AddError}
command.
"""
self.workerReporter.addError(
self.test, (RuntimeError, RuntimeError('error'), None))
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddError)
def test_addFailure(self):
"""
L{WorkerReporter.addFailure} sends a L{managercommands.AddFailure}
command.
"""
self.workerReporter.addFailure(self.test,
Failure(RuntimeError('fail')))
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddFailure)
def test_addFailureTuple(self):
"""
Adding a failure using L{WorkerReporter.addFailure} as a
C{sys.exc_info}-style tuple sends an L{managercommands.AddFailure}
message.
"""
self.workerReporter.addFailure(
self.test, (RuntimeError, RuntimeError('fail'), None))
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddFailure)
def test_addSkip(self):
"""
L{WorkerReporter.addSkip} sends a L{managercommands.AddSkip} command.
"""
self.workerReporter.addSkip(self.test, 'reason')
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddSkip)
def test_addExpectedFailure(self):
"""
L{WorkerReporter.addExpectedFailure} sends a
L{managercommands.AddExpectedFailure} command.
protocol.
"""
self.workerReporter.addExpectedFailure(
self.test, Failure(RuntimeError('error')), Todo('todo'))
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddExpectedFailure)
def test_addUnexpectedSuccess(self):
"""
L{WorkerReporter.addUnexpectedSuccess} sends a
L{managercommands.AddUnexpectedSuccess} command.
"""
self.workerReporter.addUnexpectedSuccess(self.test, Todo('todo'))
self.assertEqual(self.fakeAMProtocol.lastCall,
managercommands.AddUnexpectedSuccess)

View file

@ -0,0 +1,183 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for L{twisted.trial._dist.workertrial}.
"""
import errno
import sys
import os
from cStringIO import StringIO
from twisted.protocols.amp import AMP
from twisted.test.proto_helpers import StringTransport
from twisted.trial.unittest import TestCase
from twisted.trial._dist.workertrial import WorkerLogObserver, main, _setupPath
from twisted.trial._dist import (
workertrial, _WORKER_AMP_STDIN, _WORKER_AMP_STDOUT, workercommands,
managercommands)
class FakeAMP(AMP):
"""
A fake amp protocol.
"""
class WorkerLogObserverTestCase(TestCase):
"""
Tests for L{WorkerLogObserver}.
"""
def test_emit(self):
"""
L{WorkerLogObserver} forwards data to L{managercommands.TestWrite}.
"""
calls = []
class FakeClient(object):
def callRemote(self, method, **kwargs):
calls.append((method, kwargs))
observer = WorkerLogObserver(FakeClient())
observer.emit({'message': ['Some log']})
self.assertEqual(
calls, [(managercommands.TestWrite, {'out': 'Some log'})])
class MainTestCase(TestCase):
"""
Tests for L{main}.
"""
def setUp(self):
self.readStream = StringIO()
self.writeStream = StringIO()
self.patch(workertrial, 'startLoggingWithObserver',
self.startLoggingWithObserver)
self.addCleanup(setattr, sys, "argv", sys.argv)
sys.argv = ["trial"]
def fdopen(self, fd, mode=None):
"""
Fake C{os.fdopen} implementation which returns C{self.readStream} for
the stdin fd and C{self.writeStream} for the stdout fd.
"""
if fd == _WORKER_AMP_STDIN:
self.assertIdentical(None, mode)
return self.readStream
elif fd == _WORKER_AMP_STDOUT:
self.assertEqual('w', mode)
return self.writeStream
else:
raise AssertionError("Unexpected fd %r" % (fd,))
def startLoggingWithObserver(self, emit, setStdout):
"""
Override C{startLoggingWithObserver} for not starting logging.
"""
self.assertFalse(setStdout)
def test_empty(self):
"""
If no data is ever written, L{main} exits without writing data out.
"""
main(self.fdopen)
self.assertEqual('', self.writeStream.getvalue())
def test_forwardCommand(self):
"""
L{main} forwards data from its input stream to a L{WorkerProtocol}
instance which writes data to the output stream.
"""
client = FakeAMP()
clientTransport = StringTransport()
client.makeConnection(clientTransport)
client.callRemote(workercommands.Run, testCase="doesntexist")
self.readStream = clientTransport.io
self.readStream.seek(0, 0)
main(self.fdopen)
self.assertIn(
"No module named 'doesntexist'", self.writeStream.getvalue())
def test_readInterrupted(self):
"""
If reading the input stream fails with a C{IOError} with errno
C{EINTR}, L{main} ignores it and continues reading.
"""
excInfos = []
class FakeStream(object):
count = 0
def read(oself, size):
oself.count += 1
if oself.count == 1:
raise IOError(errno.EINTR)
else:
excInfos.append(sys.exc_info())
return ''
self.readStream = FakeStream()
main(self.fdopen)
self.assertEqual('', self.writeStream.getvalue())
self.assertEqual([(None, None, None)], excInfos)
def test_otherReadError(self):
"""
L{main} only ignores C{IOError} with C{EINTR} errno: otherwise, the
error pops out.
"""
class FakeStream(object):
count = 0
def read(oself, size):
oself.count += 1
if oself.count == 1:
raise IOError("Something else")
return ''
self.readStream = FakeStream()
self.assertRaises(IOError, main, self.fdopen)
class SetupPathTestCase(TestCase):
"""
Tests for L{_setupPath} C{sys.path} manipulation.
"""
def setUp(self):
self.addCleanup(setattr, sys, "path", sys.path[:])
def test_overridePath(self):
"""
L{_setupPath} overrides C{sys.path} if B{TRIAL_PYTHONPATH} is specified
in the environment.
"""
environ = {"TRIAL_PYTHONPATH": os.pathsep.join(["foo", "bar"])}
_setupPath(environ)
self.assertEqual(["foo", "bar"], sys.path)
def test_noVariable(self):
"""
L{_setupPath} doesn't change C{sys.path} if B{TRIAL_PYTHONPATH} is not
present in the environment.
"""
originalPath = sys.path[:]
_setupPath({})
self.assertEqual(originalPath, sys.path)

View file

@ -0,0 +1,328 @@
# -*- test-case-name: twisted.trial._dist.test.test_worker -*-
#
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
This module implements the worker classes.
@since: 12.3
"""
import os
from zope.interface import implements
from twisted.internet.protocol import ProcessProtocol
from twisted.internet.interfaces import ITransport, IAddress
from twisted.internet.defer import Deferred
from twisted.protocols.amp import AMP
from twisted.python.failure import Failure
from twisted.python.reflect import namedObject
from twisted.trial.unittest import Todo
from twisted.trial.runner import TrialSuite, TestLoader
from twisted.trial._dist import workercommands, managercommands
from twisted.trial._dist import _WORKER_AMP_STDIN, _WORKER_AMP_STDOUT
from twisted.trial._dist.workerreporter import WorkerReporter
class WorkerProtocol(AMP):
"""
The worker-side trial distributed protocol.
"""
def __init__(self, forceGarbageCollection=False):
self._loader = TestLoader()
self._result = WorkerReporter(self)
self._forceGarbageCollection = forceGarbageCollection
def run(self, testCase):
"""
Run a test case by name.
"""
case = self._loader.loadByName(testCase)
suite = TrialSuite([case], self._forceGarbageCollection)
suite.run(self._result)
return {'success': True}
workercommands.Run.responder(run)
def start(self, directory):
"""
Set up the worker, moving into given directory for tests to run in
them.
"""
os.chdir(directory)
return {'success': True}
workercommands.Start.responder(start)
class LocalWorkerAMP(AMP):
"""
Local implementation of the manager commands.
"""
def addSuccess(self, testName):
"""
Add a success to the reporter.
"""
self._result.addSuccess(self._testCase)
return {'success': True}
managercommands.AddSuccess.responder(addSuccess)
def _buildFailure(self, error, errorClass, frames):
"""
Helper to build a C{Failure} with some traceback.
@param error: An C{Exception} instance.
@param error: The class name of the C{error} class.
@param frames: A flat list of strings representing the information need
to approximatively rebuild C{Failure} frames.
@return: A L{Failure} instance with enough information about a test
error.
"""
errorType = namedObject(errorClass)
failure = Failure(error, errorType)
for i in range(0, len(frames), 3):
failure.frames.append(
(frames[i], frames[i + 1], int(frames[i + 2]), [], []))
return failure
def addError(self, testName, error, errorClass, frames):
"""
Add an error to the reporter.
"""
failure = self._buildFailure(error, errorClass, frames)
self._result.addError(self._testCase, failure)
return {'success': True}
managercommands.AddError.responder(addError)
def addFailure(self, testName, fail, failClass, frames):
"""
Add a failure to the reporter.
"""
failure = self._buildFailure(fail, failClass, frames)
self._result.addFailure(self._testCase, failure)
return {'success': True}
managercommands.AddFailure.responder(addFailure)
def addSkip(self, testName, reason):
"""
Add a skip to the reporter.
"""
self._result.addSkip(self._testCase, reason)
return {'success': True}
managercommands.AddSkip.responder(addSkip)
def addExpectedFailure(self, testName, error, todo):
"""
Add an expected failure to the reporter.
"""
_todo = Todo(todo)
self._result.addExpectedFailure(self._testCase, error, _todo)
return {'success': True}
managercommands.AddExpectedFailure.responder(addExpectedFailure)
def addUnexpectedSuccess(self, testName, todo):
"""
Add an unexpected success to the reporter.
"""
self._result.addUnexpectedSuccess(self._testCase, todo)
return {'success': True}
managercommands.AddUnexpectedSuccess.responder(addUnexpectedSuccess)
def testWrite(self, out):
"""
Print test output from the worker.
"""
self._testStream.write(out + '\n')
self._testStream.flush()
return {'success': True}
managercommands.TestWrite.responder(testWrite)
def _stopTest(self, result):
"""
Stop the current running test case, forwarding the result.
"""
self._result.stopTest(self._testCase)
return result
def run(self, testCase, result):
"""
Run a test.
"""
self._testCase = testCase
self._result = result
self._result.startTest(testCase)
d = self.callRemote(workercommands.Run, testCase=testCase.id())
return d.addCallback(self._stopTest)
def setTestStream(self, stream):
"""
Set the stream used to log output from tests.
"""
self._testStream = stream
class LocalWorkerAddress(object):
"""
A L{IAddress} implementation meant to provide stub addresses for
L{ITransport.getPeer} and L{ITransport.getHost}.
"""
implements(IAddress)
class LocalWorkerTransport(object):
"""
A stub transport implementation used to support L{AMP} over a
L{ProcessProtocol} transport.
"""
implements(ITransport)
def __init__(self, transport):
self._transport = transport
def write(self, data):
"""
Forward data to transport.
"""
self._transport.writeToChild(_WORKER_AMP_STDIN, data)
def writeSequence(self, sequence):
"""
Emulate C{writeSequence} by iterating data in the C{sequence}.
"""
for data in sequence:
self._transport.writeToChild(_WORKER_AMP_STDIN, data)
def loseConnection(self):
"""
Closes the transport.
"""
self._transport.loseConnection()
def getHost(self):
"""
Return a L{LocalWorkerAddress} instance.
"""
return LocalWorkerAddress()
def getPeer(self):
"""
Return a L{LocalWorkerAddress} instance.
"""
return LocalWorkerAddress()
class LocalWorker(ProcessProtocol):
"""
Local process worker protocol. This worker runs as a local process and
communicates via stdin/out.
@ivar _ampProtocol: The L{AMP} protocol instance used to communicate with
the worker.
@ivar _logDirectory: The directory where logs will reside.
@ivar _logFile: The name of the main log file for tests output.
"""
def __init__(self, ampProtocol, logDirectory, logFile):
self._ampProtocol = ampProtocol
self._logDirectory = logDirectory
self._logFile = logFile
self.endDeferred = Deferred()
def connectionMade(self):
"""
When connection is made, create the AMP protocol instance.
"""
self._ampProtocol.makeConnection(LocalWorkerTransport(self.transport))
if not os.path.exists(self._logDirectory):
os.makedirs(self._logDirectory)
self._outLog = file(os.path.join(self._logDirectory, 'out.log'), 'w')
self._errLog = file(os.path.join(self._logDirectory, 'err.log'), 'w')
testLog = file(os.path.join(self._logDirectory, self._logFile), 'w')
self._ampProtocol.setTestStream(testLog)
d = self._ampProtocol.callRemote(workercommands.Start,
directory=self._logDirectory)
# Ignore the potential errors, the test suite will fail properly and it
# would just print garbage.
d.addErrback(lambda x: None)
def connectionLost(self, reason):
"""
On connection lost, close the log files that we're managing for stdin
and stdout.
"""
self._outLog.close()
self._errLog.close()
def processEnded(self, reason):
"""
When the process closes, call C{connectionLost} for cleanup purposes
and forward the information to the C{_ampProtocol}.
"""
self.connectionLost(reason)
self._ampProtocol.connectionLost(reason)
self.endDeferred.callback(reason)
def outReceived(self, data):
"""
Send data received from stdout to log.
"""
self._outLog.write(data)
def errReceived(self, data):
"""
Write error data to log.
"""
self._errLog.write(data)
def childDataReceived(self, childFD, data):
"""
Handle data received on the specific pipe for the C{_ampProtocol}.
"""
if childFD == _WORKER_AMP_STDOUT:
self._ampProtocol.dataReceived(data)
else:
ProcessProtocol.childDataReceived(self, childFD, data)

View file

@ -0,0 +1,28 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Commands for telling a worker to load tests or run tests.
@since: 12.3
"""
from twisted.protocols.amp import Command, String, Boolean
class Run(Command):
"""
Run a test.
"""
arguments = [('testCase', String())]
response = [('success', Boolean())]
class Start(Command):
"""
Set up the worker process, giving the running directory.
"""
arguments = [('directory', String())]
response = [('success', Boolean())]

View file

@ -0,0 +1,123 @@
# -*- test-case-name: twisted.trial._dist.test.test_workerreporter -*-
#
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Test reporter forwarding test results over trial distributed AMP commands.
@since: 12.3
"""
from twisted.python.failure import Failure
from twisted.python.reflect import qual
from twisted.trial.reporter import TestResult
from twisted.trial._dist import managercommands
class WorkerReporter(TestResult):
"""
Reporter for trial's distributed workers. We send things not through a
stream, but through an C{AMP} protocol's C{callRemote} method.
"""
def __init__(self, ampProtocol):
"""
@param ampProtocol: The communication channel with the trial
distributed manager which collects all test results.
@type ampProtocol: C{AMP}
"""
super(WorkerReporter, self).__init__()
self.ampProtocol = ampProtocol
def _getFailure(self, error):
"""
Convert a C{sys.exc_info()}-style tuple to a L{Failure}, if necessary.
"""
if isinstance(error, tuple):
return Failure(error[1], error[0], error[2])
return error
def _getFrames(self, failure):
"""
Extract frames from a C{Failure} instance.
"""
frames = []
for frame in failure.frames:
frames.extend([frame[0], frame[1], str(frame[2])])
return frames
def addSuccess(self, test):
"""
Send a success over.
"""
super(WorkerReporter, self).addSuccess(test)
self.ampProtocol.callRemote(managercommands.AddSuccess,
testName=test.id())
def addError(self, test, error):
"""
Send an error over.
"""
super(WorkerReporter, self).addError(test, error)
failure = self._getFailure(error)
frames = self._getFrames(failure)
self.ampProtocol.callRemote(managercommands.AddError,
testName=test.id(),
error=failure.getErrorMessage(),
errorClass=qual(failure.type),
frames=frames)
def addFailure(self, test, fail):
"""
Send a Failure over.
"""
super(WorkerReporter, self).addFailure(test, fail)
failure = self._getFailure(fail)
frames = self._getFrames(failure)
self.ampProtocol.callRemote(managercommands.AddFailure,
testName=test.id(),
fail=failure.getErrorMessage(),
failClass=qual(failure.type),
frames=frames)
def addSkip(self, test, reason):
"""
Send a skip over.
"""
super(WorkerReporter, self).addSkip(test, reason)
self.ampProtocol.callRemote(managercommands.AddSkip,
testName=test.id(), reason=str(reason))
def addExpectedFailure(self, test, error, todo):
"""
Send an expected failure over.
"""
super(WorkerReporter, self).addExpectedFailure(test, error, todo)
self.ampProtocol.callRemote(managercommands.AddExpectedFailure,
testName=test.id(),
error=error.getErrorMessage(),
todo=todo.reason)
def addUnexpectedSuccess(self, test, todo):
"""
Send an unexpected success over.
"""
super(WorkerReporter, self).addUnexpectedSuccess(test, todo)
self.ampProtocol.callRemote(managercommands.AddUnexpectedSuccess,
testName=test.id(), todo=todo.reason)
def printSummary(self):
"""
I{Don't} print a summary
"""

View file

@ -0,0 +1,109 @@
# -*- test-case-name: twisted.trial._dist.test.test_workertrial -*-
#
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Implementation of C{AMP} worker commands, and main executable entry point for
the workers.
@since: 12.3
"""
import sys
import os
import errno
def _setupPath(environ):
"""
Override C{sys.path} with what the parent passed in B{TRIAL_PYTHONPATH}.
@see: twisted.trial._dist.disttrial.DistTrialRunner.launchWorkerProcesses
"""
if 'TRIAL_PYTHONPATH' in environ:
sys.path[:] = environ['TRIAL_PYTHONPATH'].split(os.pathsep)
_setupPath(os.environ)
from twisted.internet.protocol import FileWrapper
from twisted.python.log import startLoggingWithObserver, textFromEventDict
from twisted.trial._dist.options import WorkerOptions
from twisted.trial._dist import _WORKER_AMP_STDIN, _WORKER_AMP_STDOUT
class WorkerLogObserver(object):
"""
A log observer that forward its output to a C{AMP} protocol.
"""
def __init__(self, protocol):
"""
@param protocol: a connected C{AMP} protocol instance.
@type protocol: C{AMP}
"""
self.protocol = protocol
def emit(self, eventDict):
"""
Produce a log output.
"""
from twisted.trial._dist import managercommands
text = textFromEventDict(eventDict)
if text is None:
return
self.protocol.callRemote(managercommands.TestWrite, out=text)
def main(_fdopen=os.fdopen):
"""
Main function to be run if __name__ == "__main__".
@param _fdopen: If specified, the function to use in place of C{os.fdopen}.
@param _fdopen: C{callable}
"""
config = WorkerOptions()
config.parseOptions()
from twisted.trial._dist.worker import WorkerProtocol
workerProtocol = WorkerProtocol(config['force-gc'])
protocolIn = _fdopen(_WORKER_AMP_STDIN)
protocolOut = _fdopen(_WORKER_AMP_STDOUT, 'w')
workerProtocol.makeConnection(FileWrapper(protocolOut))
observer = WorkerLogObserver(workerProtocol)
startLoggingWithObserver(observer.emit, False)
while True:
try:
r = protocolIn.read(1)
except IOError, e:
if e.args[0] == errno.EINTR:
sys.exc_clear()
continue
else:
raise
if r == '':
break
else:
workerProtocol.dataReceived(r)
protocolOut.flush()
sys.stdout.flush()
sys.stderr.flush()
if config.tracer:
sys.settrace(None)
results = config.tracer.results()
results.write_results(show_missing=True, summary=False,
coverdir=config.coverdir().path)
if __name__ == '__main__':
main()

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,253 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Interfaces for Trial.
Maintainer: Jonathan Lange
"""
from __future__ import division, absolute_import
import zope.interface as zi
from zope.interface import Attribute
class ITestCase(zi.Interface):
"""
The interface that a test case must implement in order to be used in Trial.
"""
failureException = zi.Attribute(
"The exception class that is raised by failed assertions")
def __call__(result):
"""
Run the test. Should always do exactly the same thing as run().
"""
def countTestCases():
"""
Return the number of tests in this test case. Usually 1.
"""
def id():
"""
Return a unique identifier for the test, usually the fully-qualified
Python name.
"""
def run(result):
"""
Run the test, storing the results in C{result}.
@param result: A L{TestResult}.
"""
def shortDescription():
"""
Return a short description of the test.
"""
class IReporter(zi.Interface):
"""
I report results from a run of a test suite.
"""
stream = zi.Attribute(
"Deprecated in Twisted 8.0. "
"The io-stream that this reporter will write to")
tbformat = zi.Attribute("Either 'default', 'brief', or 'verbose'")
args = zi.Attribute(
"Additional string argument passed from the command line")
shouldStop = zi.Attribute(
"""
A boolean indicating that this reporter would like the test run to stop.
""")
separator = Attribute(
"Deprecated in Twisted 8.0. "
"A value which will occasionally be passed to the L{write} method.")
testsRun = Attribute(
"""
The number of tests that seem to have been run according to this
reporter.
""")
def startTest(method):
"""
Report the beginning of a run of a single test method.
@param method: an object that is adaptable to ITestMethod
"""
def stopTest(method):
"""
Report the status of a single test method
@param method: an object that is adaptable to ITestMethod
"""
def startSuite(name):
"""
Deprecated in Twisted 8.0.
Suites which wish to appear in reporter output should call this
before running their tests.
"""
def endSuite(name):
"""
Deprecated in Twisted 8.0.
Called at the end of a suite, if and only if that suite has called
C{startSuite}.
"""
def cleanupErrors(errs):
"""
Deprecated in Twisted 8.0.
Called when the reactor has been left in a 'dirty' state
@param errs: a list of L{twisted.python.failure.Failure}s
"""
def upDownError(userMeth, warn=True, printStatus=True):
"""
Deprecated in Twisted 8.0.
Called when an error occurs in a setUp* or tearDown* method
@param warn: indicates whether or not the reporter should emit a
warning about the error
@type warn: Boolean
@param printStatus: indicates whether or not the reporter should
print the name of the method and the status
message appropriate for the type of error
@type printStatus: Boolean
"""
def addSuccess(test):
"""
Record that test passed.
"""
def addError(test, error):
"""
Record that a test has raised an unexpected exception.
@param test: The test that has raised an error.
@param error: The error that the test raised. It will either be a
three-tuple in the style of C{sys.exc_info()} or a
L{Failure<twisted.python.failure.Failure>} object.
"""
def addFailure(test, failure):
"""
Record that a test has failed with the given failure.
@param test: The test that has failed.
@param failure: The failure that the test failed with. It will
either be a three-tuple in the style of C{sys.exc_info()}
or a L{Failure<twisted.python.failure.Failure>} object.
"""
def addExpectedFailure(test, failure, todo):
"""
Record that the given test failed, and was expected to do so.
@type test: L{pyunit.TestCase}
@param test: The test which this is about.
@type error: L{failure.Failure}
@param error: The error which this test failed with.
@type todo: L{unittest.Todo}
@param todo: The reason for the test's TODO status.
"""
def addUnexpectedSuccess(test, todo):
"""
Record that the given test failed, and was expected to do so.
@type test: L{pyunit.TestCase}
@param test: The test which this is about.
@type todo: L{unittest.Todo}
@param todo: The reason for the test's TODO status.
"""
def addSkip(test, reason):
"""
Record that a test has been skipped for the given reason.
@param test: The test that has been skipped.
@param reason: An object that the test case has specified as the reason
for skipping the test.
"""
def printSummary():
"""
Deprecated in Twisted 8.0, use L{done} instead.
Present a summary of the test results.
"""
def printErrors():
"""
Deprecated in Twisted 8.0, use L{done} instead.
Present the errors that have occured during the test run. This method
will be called after all tests have been run.
"""
def write(string):
"""
Deprecated in Twisted 8.0, use L{done} instead.
Display a string to the user, without appending a new line.
"""
def writeln(string):
"""
Deprecated in Twisted 8.0, use L{done} instead.
Display a string to the user, appending a new line.
"""
def wasSuccessful():
"""
Return a boolean indicating whether all test results that were reported
to this reporter were successful or not.
"""
def done():
"""
Called when the test run is complete.
This gives the result object an opportunity to display a summary of
information to the user. Once you have called C{done} on an
L{IReporter} object, you should assume that the L{IReporter} object is
no longer usable.
"""

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,795 @@
# -*- test-case-name: twisted.trial.test.test_runner -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
A miscellany of code used to run Trial tests.
Maintainer: Jonathan Lange
"""
__all__ = [
'TestSuite',
'DestructiveTestSuite', 'DryRunVisitor', 'ErrorHolder', 'LoggedSuite',
'TestHolder', 'TestLoader', 'TrialRunner', 'TrialSuite',
'filenameToModule', 'isPackage', 'isPackageDirectory', 'isTestCase',
'name', 'samefile', 'NOT_IN_TEST',
]
import os, types, warnings, sys, inspect, imp
import doctest, time
from twisted.python import reflect, log, failure, modules, filepath
from twisted.python.deprecate import deprecatedModuleAttribute
from twisted.python.versions import Version
from twisted.internet import defer
from twisted.trial import util, unittest
from twisted.trial.itrial import ITestCase
from twisted.trial.reporter import _ExitWrapper, UncleanWarningsReporterWrapper
# These are imported so that they remain in the public API for t.trial.runner
from twisted.trial.unittest import TestSuite
from zope.interface import implements
pyunit = __import__('unittest')
def isPackage(module):
"""Given an object return True if the object looks like a package"""
if not isinstance(module, types.ModuleType):
return False
basename = os.path.splitext(os.path.basename(module.__file__))[0]
return basename == '__init__'
def isPackageDirectory(dirname):
"""Is the directory at path 'dirname' a Python package directory?
Returns the name of the __init__ file (it may have a weird extension)
if dirname is a package directory. Otherwise, returns False"""
for ext in zip(*imp.get_suffixes())[0]:
initFile = '__init__' + ext
if os.path.exists(os.path.join(dirname, initFile)):
return initFile
return False
def samefile(filename1, filename2):
"""
A hacky implementation of C{os.path.samefile}. Used by L{filenameToModule}
when the platform doesn't provide C{os.path.samefile}. Do not use this.
"""
return os.path.abspath(filename1) == os.path.abspath(filename2)
def filenameToModule(fn):
"""
Given a filename, do whatever possible to return a module object matching
that file.
If the file in question is a module in Python path, properly import and
return that module. Otherwise, load the source manually.
@param fn: A filename.
@return: A module object.
@raise ValueError: If C{fn} does not exist.
"""
if not os.path.exists(fn):
raise ValueError("%r doesn't exist" % (fn,))
try:
ret = reflect.namedAny(reflect.filenameToModuleName(fn))
except (ValueError, AttributeError):
# Couldn't find module. The file 'fn' is not in PYTHONPATH
return _importFromFile(fn)
# ensure that the loaded module matches the file
retFile = os.path.splitext(ret.__file__)[0] + '.py'
# not all platforms (e.g. win32) have os.path.samefile
same = getattr(os.path, 'samefile', samefile)
if os.path.isfile(fn) and not same(fn, retFile):
del sys.modules[ret.__name__]
ret = _importFromFile(fn)
return ret
def _importFromFile(fn, moduleName=None):
fn = _resolveDirectory(fn)
if not moduleName:
moduleName = os.path.splitext(os.path.split(fn)[-1])[0]
if moduleName in sys.modules:
return sys.modules[moduleName]
fd = open(fn, 'r')
try:
module = imp.load_source(moduleName, fn, fd)
finally:
fd.close()
return module
def _resolveDirectory(fn):
if os.path.isdir(fn):
initFile = isPackageDirectory(fn)
if initFile:
fn = os.path.join(fn, initFile)
else:
raise ValueError('%r is not a package directory' % (fn,))
return fn
def _getMethodNameInClass(method):
"""
Find the attribute name on the method's class which refers to the method.
For some methods, notably decorators which have not had __name__ set correctly:
getattr(method.im_class, method.__name__) != method
"""
if getattr(method.im_class, method.__name__, object()) != method:
for alias in dir(method.im_class):
if getattr(method.im_class, alias, object()) == method:
return alias
return method.__name__
class DestructiveTestSuite(TestSuite):
"""
A test suite which remove the tests once run, to minimize memory usage.
"""
def run(self, result):
"""
Almost the same as L{TestSuite.run}, but with C{self._tests} being
empty at the end.
"""
while self._tests:
if result.shouldStop:
break
test = self._tests.pop(0)
test(result)
return result
# When an error occurs outside of any test, the user will see this string
# in place of a test's name.
NOT_IN_TEST = "<not in test>"
class LoggedSuite(TestSuite):
"""
Any errors logged in this suite will be reported to the L{TestResult}
object.
"""
def run(self, result):
"""
Run the suite, storing all errors in C{result}. If an error is logged
while no tests are running, then it will be added as an error to
C{result}.
@param result: A L{TestResult} object.
"""
observer = unittest._logObserver
observer._add()
super(LoggedSuite, self).run(result)
observer._remove()
for error in observer.getErrors():
result.addError(TestHolder(NOT_IN_TEST), error)
observer.flushErrors()
class TrialSuite(TestSuite):
"""
Suite to wrap around every single test in a C{trial} run. Used internally
by Trial to set up things necessary for Trial tests to work, regardless of
what context they are run in.
"""
def __init__(self, tests=(), forceGarbageCollection=False):
if forceGarbageCollection:
newTests = []
for test in tests:
test = unittest.decorate(
test, unittest._ForceGarbageCollectionDecorator)
newTests.append(test)
tests = newTests
suite = LoggedSuite(tests)
super(TrialSuite, self).__init__([suite])
def _bail(self):
from twisted.internet import reactor
d = defer.Deferred()
reactor.addSystemEventTrigger('after', 'shutdown',
lambda: d.callback(None))
reactor.fireSystemEvent('shutdown') # radix's suggestion
# As long as TestCase does crap stuff with the reactor we need to
# manually shutdown the reactor here, and that requires util.wait
# :(
# so that the shutdown event completes
unittest.TestCase('mktemp')._wait(d)
def run(self, result):
try:
TestSuite.run(self, result)
finally:
self._bail()
def name(thing):
"""
@param thing: an object from modules (instance of PythonModule,
PythonAttribute), a TestCase subclass, or an instance of a TestCase.
"""
if isTestCase(thing):
# TestCase subclass
theName = reflect.qual(thing)
else:
# thing from trial, or thing from modules.
# this monstrosity exists so that modules' objects do not have to
# implement id(). -jml
try:
theName = thing.id()
except AttributeError:
theName = thing.name
return theName
def isTestCase(obj):
"""
@return: C{True} if C{obj} is a class that contains test cases, C{False}
otherwise. Used to find all the tests in a module.
"""
try:
return issubclass(obj, pyunit.TestCase)
except TypeError:
return False
class TestHolder(object):
"""
Placeholder for a L{TestCase} inside a reporter. As far as a L{TestResult}
is concerned, this looks exactly like a unit test.
"""
implements(ITestCase)
failureException = None
def __init__(self, description):
"""
@param description: A string to be displayed L{TestResult}.
"""
self.description = description
def __call__(self, result):
return self.run(result)
def id(self):
return self.description
def countTestCases(self):
return 0
def run(self, result):
"""
This test is just a placeholder. Run the test successfully.
@param result: The C{TestResult} to store the results in.
@type result: L{twisted.trial.itrial.IReporter}.
"""
result.startTest(self)
result.addSuccess(self)
result.stopTest(self)
def shortDescription(self):
return self.description
class ErrorHolder(TestHolder):
"""
Used to insert arbitrary errors into a test suite run. Provides enough
methods to look like a C{TestCase}, however, when it is run, it simply adds
an error to the C{TestResult}. The most common use-case is for when a
module fails to import.
"""
def __init__(self, description, error):
"""
@param description: A string used by C{TestResult}s to identify this
error. Generally, this is the name of a module that failed to import.
@param error: The error to be added to the result. Can be an `exc_info`
tuple or a L{twisted.python.failure.Failure}.
"""
super(ErrorHolder, self).__init__(description)
self.error = util.excInfoOrFailureToExcInfo(error)
def __repr__(self):
return "<ErrorHolder description=%r error=%r>" % (
self.description, self.error[1])
def run(self, result):
"""
Run the test, reporting the error.
@param result: The C{TestResult} to store the results in.
@type result: L{twisted.trial.itrial.IReporter}.
"""
result.startTest(self)
result.addError(self, self.error)
result.stopTest(self)
class TestLoader(object):
"""
I find tests inside function, modules, files -- whatever -- then return
them wrapped inside a Test (either a L{TestSuite} or a L{TestCase}).
@ivar methodPrefix: A string prefix. C{TestLoader} will assume that all the
methods in a class that begin with C{methodPrefix} are test cases.
@ivar modulePrefix: A string prefix. Every module in a package that begins
with C{modulePrefix} is considered a module full of tests.
@ivar forceGarbageCollection: A flag applied to each C{TestCase} loaded.
See L{unittest.TestCase} for more information.
@ivar sorter: A key function used to sort C{TestCase}s, test classes,
modules and packages.
@ivar suiteFactory: A callable which is passed a list of tests (which
themselves may be suites of tests). Must return a test suite.
"""
methodPrefix = 'test'
modulePrefix = 'test_'
def __init__(self):
self.suiteFactory = TestSuite
self.sorter = name
self._importErrors = []
def sort(self, xs):
"""
Sort the given things using L{sorter}.
@param xs: A list of test cases, class or modules.
"""
return sorted(xs, key=self.sorter)
def findTestClasses(self, module):
"""Given a module, return all Trial test classes"""
classes = []
for name, val in inspect.getmembers(module):
if isTestCase(val):
classes.append(val)
return self.sort(classes)
def findByName(self, name):
"""
Return a Python object given a string describing it.
@param name: a string which may be either a filename or a
fully-qualified Python name.
@return: If C{name} is a filename, return the module. If C{name} is a
fully-qualified Python name, return the object it refers to.
"""
if os.path.exists(name):
return filenameToModule(name)
return reflect.namedAny(name)
def loadModule(self, module):
"""
Return a test suite with all the tests from a module.
Included are TestCase subclasses and doctests listed in the module's
__doctests__ module. If that's not good for you, put a function named
either C{testSuite} or C{test_suite} in your module that returns a
TestSuite, and I'll use the results of that instead.
If C{testSuite} and C{test_suite} are both present, then I'll use
C{testSuite}.
"""
## XXX - should I add an optional parameter to disable the check for
## a custom suite.
## OR, should I add another method
if not isinstance(module, types.ModuleType):
raise TypeError("%r is not a module" % (module,))
if hasattr(module, 'testSuite'):
return module.testSuite()
elif hasattr(module, 'test_suite'):
return module.test_suite()
suite = self.suiteFactory()
for testClass in self.findTestClasses(module):
suite.addTest(self.loadClass(testClass))
if not hasattr(module, '__doctests__'):
return suite
docSuite = self.suiteFactory()
for doctest in module.__doctests__:
docSuite.addTest(self.loadDoctests(doctest))
return self.suiteFactory([suite, docSuite])
loadTestsFromModule = loadModule
def loadClass(self, klass):
"""
Given a class which contains test cases, return a sorted list of
C{TestCase} instances.
"""
if not (isinstance(klass, type) or isinstance(klass, types.ClassType)):
raise TypeError("%r is not a class" % (klass,))
if not isTestCase(klass):
raise ValueError("%r is not a test case" % (klass,))
names = self.getTestCaseNames(klass)
tests = self.sort([self._makeCase(klass, self.methodPrefix+name)
for name in names])
return self.suiteFactory(tests)
loadTestsFromTestCase = loadClass
def getTestCaseNames(self, klass):
"""
Given a class that contains C{TestCase}s, return a list of names of
methods that probably contain tests.
"""
return reflect.prefixedMethodNames(klass, self.methodPrefix)
def loadMethod(self, method):
"""
Given a method of a C{TestCase} that represents a test, return a
C{TestCase} instance for that test.
"""
if not isinstance(method, types.MethodType):
raise TypeError("%r not a method" % (method,))
return self._makeCase(method.im_class, _getMethodNameInClass(method))
def _makeCase(self, klass, methodName):
return klass(methodName)
def loadPackage(self, package, recurse=False):
"""
Load tests from a module object representing a package, and return a
TestSuite containing those tests.
Tests are only loaded from modules whose name begins with 'test_'
(or whatever C{modulePrefix} is set to).
@param package: a types.ModuleType object (or reasonable facsimilie
obtained by importing) which may contain tests.
@param recurse: A boolean. If True, inspect modules within packages
within the given package (and so on), otherwise, only inspect modules
in the package itself.
@raise: TypeError if 'package' is not a package.
@return: a TestSuite created with my suiteFactory, containing all the
tests.
"""
if not isPackage(package):
raise TypeError("%r is not a package" % (package,))
pkgobj = modules.getModule(package.__name__)
if recurse:
discovery = pkgobj.walkModules()
else:
discovery = pkgobj.iterModules()
discovered = []
for disco in discovery:
if disco.name.split(".")[-1].startswith(self.modulePrefix):
discovered.append(disco)
suite = self.suiteFactory()
for modinfo in self.sort(discovered):
try:
module = modinfo.load()
except:
thingToAdd = ErrorHolder(modinfo.name, failure.Failure())
else:
thingToAdd = self.loadModule(module)
suite.addTest(thingToAdd)
return suite
def loadDoctests(self, module):
"""
Return a suite of tests for all the doctests defined in C{module}.
@param module: A module object or a module name.
"""
if isinstance(module, str):
try:
module = reflect.namedAny(module)
except:
return ErrorHolder(module, failure.Failure())
if not inspect.ismodule(module):
warnings.warn("trial only supports doctesting modules")
return
extraArgs = {}
if sys.version_info > (2, 4):
# Work around Python issue2604: DocTestCase.tearDown clobbers globs
def saveGlobals(test):
"""
Save C{test.globs} and replace it with a copy so that if
necessary, the original will be available for the next test
run.
"""
test._savedGlobals = getattr(test, '_savedGlobals', test.globs)
test.globs = test._savedGlobals.copy()
extraArgs['setUp'] = saveGlobals
return doctest.DocTestSuite(module, **extraArgs)
def loadAnything(self, thing, recurse=False):
"""
Given a Python object, return whatever tests that are in it. Whatever
'in' might mean.
@param thing: A Python object. A module, method, class or package.
@param recurse: Whether or not to look in subpackages of packages.
Defaults to False.
@return: A C{TestCase} or C{TestSuite}.
"""
if isinstance(thing, types.ModuleType):
if isPackage(thing):
return self.loadPackage(thing, recurse)
return self.loadModule(thing)
elif isinstance(thing, types.ClassType):
return self.loadClass(thing)
elif isinstance(thing, type):
return self.loadClass(thing)
elif isinstance(thing, types.MethodType):
return self.loadMethod(thing)
raise TypeError("No loader for %r. Unrecognized type" % (thing,))
def loadByName(self, name, recurse=False):
"""
Given a string representing a Python object, return whatever tests
are in that object.
If C{name} is somehow inaccessible (e.g. the module can't be imported,
there is no Python object with that name etc) then return an
L{ErrorHolder}.
@param name: The fully-qualified name of a Python object.
"""
try:
thing = self.findByName(name)
except:
return ErrorHolder(name, failure.Failure())
return self.loadAnything(thing, recurse)
loadTestsFromName = loadByName
def loadByNames(self, names, recurse=False):
"""
Construct a TestSuite containing all the tests found in 'names', where
names is a list of fully qualified python names and/or filenames. The
suite returned will have no duplicate tests, even if the same object
is named twice.
"""
things = []
errors = []
for name in names:
try:
things.append(self.findByName(name))
except:
errors.append(ErrorHolder(name, failure.Failure()))
suites = [self.loadAnything(thing, recurse)
for thing in self._uniqueTests(things)]
suites.extend(errors)
return self.suiteFactory(suites)
def _uniqueTests(self, things):
"""
Gather unique suite objects from loaded things. This will guarantee
uniqueness of inherited methods on TestCases which would otherwise hash
to same value and collapse to one test unexpectedly if using simpler
means: e.g. set().
"""
seen = set()
for thing in things:
if isinstance(thing, types.MethodType):
thing = (thing, thing.im_class)
else:
thing = (thing,)
if thing not in seen:
yield thing[0]
seen.add(thing)
class DryRunVisitor(object):
"""
A visitor that makes a reporter think that every test visited has run
successfully.
"""
deprecatedModuleAttribute(
Version("Twisted", 13, 0, 0),
"Trial no longer has support for visitors",
"twisted.trial.runner", "DryRunVisitor")
def __init__(self, reporter):
"""
@param reporter: A C{TestResult} object.
"""
self.reporter = reporter
def markSuccessful(self, testCase):
"""
Convince the reporter that this test has been run successfully.
"""
self.reporter.startTest(testCase)
self.reporter.addSuccess(testCase)
self.reporter.stopTest(testCase)
class TrialRunner(object):
"""
A specialised runner that the trial front end uses.
"""
DEBUG = 'debug'
DRY_RUN = 'dry-run'
def _setUpTestdir(self):
self._tearDownLogFile()
currentDir = os.getcwd()
base = filepath.FilePath(self.workingDirectory)
testdir, self._testDirLock = util._unusedTestDirectory(base)
os.chdir(testdir.path)
return currentDir
def _tearDownTestdir(self, oldDir):
os.chdir(oldDir)
self._testDirLock.unlock()
_log = log
def _makeResult(self):
reporter = self.reporterFactory(self.stream, self.tbformat,
self.rterrors, self._log)
if self._exitFirst:
reporter = _ExitWrapper(reporter)
if self.uncleanWarnings:
reporter = UncleanWarningsReporterWrapper(reporter)
return reporter
def __init__(self, reporterFactory,
mode=None,
logfile='test.log',
stream=sys.stdout,
profile=False,
tracebackFormat='default',
realTimeErrors=False,
uncleanWarnings=False,
workingDirectory=None,
forceGarbageCollection=False,
debugger=None,
exitFirst=False):
self.reporterFactory = reporterFactory
self.logfile = logfile
self.mode = mode
self.stream = stream
self.tbformat = tracebackFormat
self.rterrors = realTimeErrors
self.uncleanWarnings = uncleanWarnings
self._result = None
self.workingDirectory = workingDirectory or '_trial_temp'
self._logFileObserver = None
self._logFileObject = None
self._forceGarbageCollection = forceGarbageCollection
self.debugger = debugger
self._exitFirst = exitFirst
if profile:
self.run = util.profiled(self.run, 'profile.data')
def _tearDownLogFile(self):
if self._logFileObserver is not None:
log.removeObserver(self._logFileObserver.emit)
self._logFileObserver = None
if self._logFileObject is not None:
self._logFileObject.close()
self._logFileObject = None
def _setUpLogFile(self):
self._tearDownLogFile()
if self.logfile == '-':
logFile = sys.stdout
else:
logFile = file(self.logfile, 'a')
self._logFileObject = logFile
self._logFileObserver = log.FileLogObserver(logFile)
log.startLoggingWithObserver(self._logFileObserver.emit, 0)
def run(self, test):
"""
Run the test or suite and return a result object.
"""
test = unittest.decorate(test, ITestCase)
return self._runWithoutDecoration(test, self._forceGarbageCollection)
def _runWithoutDecoration(self, test, forceGarbageCollection=False):
"""
Private helper that runs the given test but doesn't decorate it.
"""
result = self._makeResult()
# 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], forceGarbageCollection)
startTime = time.time()
if self.mode == self.DRY_RUN:
for single in unittest._iterateTests(suite):
result.startTest(single)
result.addSuccess(single)
result.stopTest(single)
else:
if self.mode == self.DEBUG:
run = lambda: self.debugger.runcall(suite.run, result)
else:
run = lambda: suite.run(result)
oldDir = self._setUpTestdir()
try:
self._setUpLogFile()
run()
finally:
self._tearDownLogFile()
self._tearDownTestdir(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=3)
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 runUntilFailure(self, test):
"""
Repeatedly run C{test} until it fails.
"""
count = 0
while True:
count += 1
self.stream.write("Test Pass %d\n" % (count,))
if count == 1:
result = self.run(test)
else:
result = self._runWithoutDecoration(test)
if result.testsRun == 0:
break
if not result.wasSuccessful():
break
return result

View file

@ -0,0 +1,6 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Unit tests for the Trial unit-testing framework.
"""

View file

@ -0,0 +1,203 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for Deferred handling by L{twisted.trial.unittest.TestCase}.
"""
from __future__ import division, absolute_import
from twisted.trial import unittest
from twisted.internet import defer, threads, reactor
class DeferredSetUpOK(unittest.TestCase):
def setUp(self):
d = defer.succeed('value')
d.addCallback(self._cb_setUpCalled)
return d
def _cb_setUpCalled(self, ignored):
self._setUpCalled = True
def test_ok(self):
self.failUnless(self._setUpCalled)
class DeferredSetUpFail(unittest.TestCase):
testCalled = False
def setUp(self):
return defer.fail(unittest.FailTest('i fail'))
def test_ok(self):
DeferredSetUpFail.testCalled = True
self.fail("I should not get called")
class DeferredSetUpCallbackFail(unittest.TestCase):
testCalled = False
def setUp(self):
d = defer.succeed('value')
d.addCallback(self._cb_setUpCalled)
return d
def _cb_setUpCalled(self, ignored):
self.fail('deliberate failure')
def test_ok(self):
DeferredSetUpCallbackFail.testCalled = True
class DeferredSetUpError(unittest.TestCase):
testCalled = False
def setUp(self):
return defer.fail(RuntimeError('deliberate error'))
def test_ok(self):
DeferredSetUpError.testCalled = True
class DeferredSetUpNeverFire(unittest.TestCase):
testCalled = False
def setUp(self):
return defer.Deferred()
def test_ok(self):
DeferredSetUpNeverFire.testCalled = True
class DeferredSetUpSkip(unittest.TestCase):
testCalled = False
def setUp(self):
d = defer.succeed('value')
d.addCallback(self._cb1)
return d
def _cb1(self, ignored):
raise unittest.SkipTest("skip me")
def test_ok(self):
DeferredSetUpSkip.testCalled = True
class DeferredTests(unittest.TestCase):
touched = False
def _cb_fail(self, reason):
self.fail(reason)
def _cb_error(self, reason):
raise RuntimeError(reason)
def _cb_skip(self, reason):
raise unittest.SkipTest(reason)
def _touchClass(self, ignored):
self.__class__.touched = True
def setUp(self):
self.__class__.touched = False
def test_pass(self):
return defer.succeed('success')
def test_passGenerated(self):
self._touchClass(None)
yield None
test_passGenerated = defer.deferredGenerator(test_passGenerated)
def test_fail(self):
return defer.fail(self.failureException('I fail'))
def test_failureInCallback(self):
d = defer.succeed('fail')
d.addCallback(self._cb_fail)
return d
def test_errorInCallback(self):
d = defer.succeed('error')
d.addCallback(self._cb_error)
return d
def test_skip(self):
d = defer.succeed('skip')
d.addCallback(self._cb_skip)
d.addCallback(self._touchClass)
return d
def test_thread(self):
return threads.deferToThread(lambda : None)
def test_expectedFailure(self):
d = defer.succeed('todo')
d.addCallback(self._cb_error)
return d
test_expectedFailure.todo = "Expected failure"
class TimeoutTests(unittest.TestCase):
timedOut = None
def test_pass(self):
d = defer.Deferred()
reactor.callLater(0, d.callback, 'hoorj!')
return d
test_pass.timeout = 2
def test_passDefault(self):
# test default timeout
d = defer.Deferred()
reactor.callLater(0, d.callback, 'hoorj!')
return d
def test_timeout(self):
return defer.Deferred()
test_timeout.timeout = 0.1
def test_timeoutZero(self):
return defer.Deferred()
test_timeoutZero.timeout = 0
def test_expectedFailure(self):
return defer.Deferred()
test_expectedFailure.timeout = 0.1
test_expectedFailure.todo = "i will get it right, eventually"
def test_skip(self):
return defer.Deferred()
test_skip.timeout = 0.1
test_skip.skip = "i will get it right, eventually"
def test_errorPropagation(self):
def timedOut(err):
self.__class__.timedOut = err
return err
d = defer.Deferred()
d.addErrback(timedOut)
return d
test_errorPropagation.timeout = 0.1
def test_calledButNeverCallback(self):
d = defer.Deferred()
def neverFire(r):
return defer.Deferred()
d.addCallback(neverFire)
d.callback(1)
return d
test_calledButNeverCallback.timeout = 0.1
class TestClassTimeoutAttribute(unittest.TestCase):
timeout = 0.2
def setUp(self):
self.d = defer.Deferred()
def testMethod(self):
self.methodCalled = True
return self.d

View file

@ -0,0 +1,188 @@
# -*- test-case-name: twisted.trial.test.test_tests -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Definitions of test cases with various interesting error-related behaviors, to
be used by test modules to exercise different features of trial's test runner.
See the L{twisted.trial.test.test_tests} module docstring for details about how
this code is arranged.
"""
from __future__ import division, absolute_import
from twisted.trial import unittest, util
from twisted.internet import reactor, protocol, defer
class FoolishError(Exception):
pass
class FailureInSetUpMixin(object):
def setUp(self):
raise FoolishError("I am a broken setUp method")
def test_noop(self):
pass
class SynchronousTestFailureInSetUp(
FailureInSetUpMixin, unittest.SynchronousTestCase):
pass
class AsynchronousTestFailureInSetUp(
FailureInSetUpMixin, unittest.TestCase):
pass
class FailureInTearDownMixin(object):
def tearDown(self):
raise FoolishError("I am a broken tearDown method")
def test_noop(self):
pass
class SynchronousTestFailureInTearDown(
FailureInTearDownMixin, unittest.SynchronousTestCase):
pass
class AsynchronousTestFailureInTearDown(
FailureInTearDownMixin, unittest.TestCase):
pass
class TestRegularFail(unittest.SynchronousTestCase):
def test_fail(self):
self.fail("I fail")
def test_subfail(self):
self.subroutine()
def subroutine(self):
self.fail("I fail inside")
class TestAsynchronousFail(unittest.TestCase):
"""
Test failures for L{unittest.TestCase} based classes.
"""
def test_fail(self):
"""
A test which fails in the callback of the returned L{defer.Deferred}.
"""
d = defer.Deferred()
d.addCallback(self._later)
reactor.callLater(0, d.callback, None)
return d
def _later(self, res):
self.fail("I fail later")
def test_exception(self):
"""
A test which raises an exception synchronously.
"""
raise Exception("I fail")
class ErrorTest(unittest.SynchronousTestCase):
"""
A test case which has a L{test_foo} which will raise an error.
@ivar ran: boolean indicating whether L{test_foo} has been run.
"""
ran = False
def test_foo(self):
"""
Set C{self.ran} to True and raise a C{ZeroDivisionError}
"""
self.ran = True
1/0
class TestSkipTestCase(unittest.SynchronousTestCase):
pass
TestSkipTestCase.skip = "skipping this test"
class DelayedCall(unittest.TestCase):
hiddenExceptionMsg = "something blew up"
def go(self):
raise RuntimeError(self.hiddenExceptionMsg)
def testHiddenException(self):
"""
What happens if an error is raised in a DelayedCall and an error is
also raised in the test?
L{test_reporter.TestErrorReporting.testHiddenException} checks that
both errors get reported.
Note that this behaviour is deprecated. A B{real} test would return a
Deferred that got triggered by the callLater. This would guarantee the
delayed call error gets reported.
"""
reactor.callLater(0, self.go)
reactor.iterate(0.01)
self.fail("Deliberate failure to mask the hidden exception")
testHiddenException.suppress = [util.suppress(
message=r'reactor\.iterate cannot be used.*',
category=DeprecationWarning)]
class ReactorCleanupTests(unittest.TestCase):
def test_leftoverPendingCalls(self):
def _():
print('foo!')
reactor.callLater(10000.0, _)
class SocketOpenTest(unittest.TestCase):
def test_socketsLeftOpen(self):
f = protocol.Factory()
f.protocol = protocol.Protocol
reactor.listenTCP(0, f)
class TimingOutDeferred(unittest.TestCase):
def test_alpha(self):
pass
def test_deferredThatNeverFires(self):
self.methodCalled = True
d = defer.Deferred()
return d
def test_omega(self):
pass
def unexpectedException(self):
"""i will raise an unexpected exception...
... *CAUSE THAT'S THE KINDA GUY I AM*
>>> 1/0
"""

View file

@ -0,0 +1,21 @@
# Copyright (c) 2006 Twisted Matrix Laboratories. See LICENSE for details
"""
Mock test module that contains a C{test_suite} method. L{runner.TestLoader}
should load the tests from the C{test_suite}, not from the C{Foo} C{TestCase}.
See {twisted.trial.test.test_loader.LoaderTest.test_loadModuleWith_test_suite}.
"""
from twisted.trial import unittest, runner
class Foo(unittest.SynchronousTestCase):
def test_foo(self):
pass
def test_suite():
ts = runner.TestSuite()
ts.name = "MyCustomSuite"
return ts

View file

@ -0,0 +1,21 @@
# Copyright (c) 2006 Twisted Matrix Laboratories. See LICENSE for details
"""
Mock test module that contains a C{testSuite} method. L{runner.TestLoader}
should load the tests from the C{testSuite}, not from the C{Foo} C{TestCase}.
See L{twisted.trial.test.test_loader.LoaderTest.test_loadModuleWith_testSuite}.
"""
from twisted.trial import unittest, runner
class Foo(unittest.SynchronousTestCase):
def test_foo(self):
pass
def testSuite():
ts = runner.TestSuite()
ts.name = "MyCustomSuite"
return ts

View file

@ -0,0 +1,28 @@
# Copyright (c) 2006 Twisted Matrix Laboratories. See LICENSE for details
"""
Mock test module that contains both a C{test_suite} and a C{testSuite} method.
L{runner.TestLoader} should load the tests from the C{testSuite}, not from the
C{Foo} C{TestCase} nor from the C{test_suite} method.
See {twisted.trial.test.test_loader.LoaderTest.test_loadModuleWithBothCustom}.
"""
from twisted.trial import unittest, runner
class Foo(unittest.SynchronousTestCase):
def test_foo(self):
pass
def test_suite():
ts = runner.TestSuite()
ts.name = "test_suite"
return ts
def testSuite():
ts = runner.TestSuite()
ts.name = "testSuite"
return ts

View file

@ -0,0 +1,104 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
# this module is a trivial class with doctests to test trial's doctest
# support.
from __future__ import division, absolute_import
class Counter(object):
"""a simple counter object for testing trial's doctest support
>>> c = Counter()
>>> c.value()
0
>>> c += 3
>>> c.value()
3
>>> c.incr()
>>> c.value() == 4
True
>>> c == 4
True
>>> c != 9
True
"""
_count = 0
def __init__(self, initialValue=0, maxval=None):
self._count = initialValue
self.maxval = maxval
def __iadd__(self, other):
"""add other to my value and return self
>>> c = Counter(100)
>>> c += 333
>>> c == 433
True
"""
if self.maxval is not None and ((self._count + other) > self.maxval):
raise ValueError, "sorry, counter got too big"
else:
self._count += other
return self
def __eq__(self, other):
"""equality operator, compare other to my value()
>>> c = Counter()
>>> c == 0
True
>>> c += 10
>>> c.incr()
>>> c == 10 # fail this test on purpose
True
"""
return self._count == other
def __ne__(self, other):
"""inequality operator
>>> c = Counter()
>>> c != 10
True
"""
return not self.__eq__(other)
def incr(self):
"""increment my value by 1
>>> from twisted.trial.test.mockdoctest import Counter
>>> c = Counter(10, 11)
>>> c.incr()
>>> c.value() == 11
True
>>> c.incr()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "twisted/trial/test/mockdoctest.py", line 51, in incr
self.__iadd__(1)
File "twisted/trial/test/mockdoctest.py", line 39, in __iadd__
raise ValueError, "sorry, counter got too big"
ValueError: sorry, counter got too big
"""
self.__iadd__(1)
def value(self):
"""return this counter's value
>>> c = Counter(555)
>>> c.value() == 555
True
"""
return self._count
def unexpectedException(self):
"""i will raise an unexpected exception...
... *CAUSE THAT'S THE KINDA GUY I AM*
>>> 1/0
"""

View file

@ -0,0 +1,7 @@
# -*- test-case-name: twisted.trial.test.moduleself -*-
from twisted.trial import unittest
class Foo(unittest.SynchronousTestCase):
def testFoo(self):
pass

View file

@ -0,0 +1,11 @@
# -*- test-case-name: twisted.trial.test.test_log -*-
# fodder for test_script, which parses files for emacs local variable
# declarations. This one is supposed to have:
# test-case-name: twisted.trial.test.test_log.
# in the first line
# The class declaration is irrelevant
class Foo(object):
pass

View file

@ -0,0 +1,6 @@
# fodder for test_script, which parses files for emacs local variable
# declarations. This one is supposed to have none.
# The class declaration is irrelevant
class Bar(object):
pass

View file

@ -0,0 +1,50 @@
# -*- test-case-name: twisted.trial.test.test_script -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for handling of trial's --order option.
"""
from twisted.trial import unittest
class FooTest(unittest.TestCase):
"""
Used to make assertions about the order its tests will be run in.
"""
def test_first(self):
pass
def test_second(self):
pass
def test_third(self):
pass
def test_fourth(self):
pass
class BazTest(unittest.TestCase):
"""
Used to make assertions about the order the test cases in this module are
run in.
"""
def test_baz(self):
pass
class BarTest(unittest.TestCase):
"""
Used to make assertions about the order the test cases in this module are
run in.
"""
def test_bar(self):
pass

View file

@ -0,0 +1,181 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
#
"""
Classes and functions used by L{twisted.trial.test.test_util}
and L{twisted.trial.test.test_loader}.
"""
from __future__ import division, absolute_import
import sys, os
from twisted.trial import unittest
testModule = """
from twisted.trial import unittest
class FooTest(unittest.SynchronousTestCase):
def testFoo(self):
pass
"""
dosModule = testModule.replace('\n', '\r\n')
testSample = """
'''This module is used by test_loader to test the Trial test loading
functionality. Do NOT change the number of tests in this module.
Do NOT change the names the tests in this module.
'''
import unittest as pyunit
from twisted.trial import unittest
class FooTest(unittest.SynchronousTestCase):
def test_foo(self):
pass
def test_bar(self):
pass
class PyunitTest(pyunit.TestCase):
def test_foo(self):
pass
def test_bar(self):
pass
class NotATest(object):
def test_foo(self):
pass
class AlphabetTest(unittest.SynchronousTestCase):
def test_a(self):
pass
def test_b(self):
pass
def test_c(self):
pass
"""
testInheritanceSample = """
'''This module is used by test_loader to test the Trial test loading
functionality. Do NOT change the number of tests in this module.
Do NOT change the names the tests in this module.
'''
from twisted.trial import unittest
class X(object):
def test_foo(self):
pass
class A(unittest.SynchronousTestCase, X):
pass
class B(unittest.SynchronousTestCase, X):
pass
"""
class PackageTest(unittest.SynchronousTestCase):
files = [
('badpackage/__init__.py', 'frotz\n'),
('badpackage/test_module.py', ''),
('package2/__init__.py', ''),
('package2/test_module.py', 'import frotz\n'),
('package/__init__.py', ''),
('package/frotz.py', 'frotz\n'),
('package/test_bad_module.py',
'raise ZeroDivisionError("fake error")'),
('package/test_dos_module.py', dosModule),
('package/test_import_module.py', 'import frotz'),
('package/test_module.py', testModule),
('goodpackage/__init__.py', ''),
('goodpackage/test_sample.py', testSample),
('goodpackage/sub/__init__.py', ''),
('goodpackage/sub/test_sample.py', testSample),
('inheritancepackage/__init__.py', ''),
('inheritancepackage/test_x.py', testInheritanceSample),
]
def _toModuleName(self, filename):
name = os.path.splitext(filename)[0]
segs = name.split('/')
if segs[-1] == '__init__':
segs = segs[:-1]
return '.'.join(segs)
def getModules(self):
"""
Return matching module names for files listed in C{self.files}.
"""
return [self._toModuleName(filename) for (filename, code) in self.files]
def cleanUpModules(self):
modules = self.getModules()
modules.sort()
modules.reverse()
for module in modules:
try:
del sys.modules[module]
except KeyError:
pass
def createFiles(self, files, parentDir='.'):
for filename, contents in self.files:
filename = os.path.join(parentDir, filename)
self._createDirectory(filename)
fd = open(filename, 'w')
fd.write(contents)
fd.close()
def _createDirectory(self, filename):
directory = os.path.dirname(filename)
if not os.path.exists(directory):
os.makedirs(directory)
def setUp(self, parentDir=None):
if parentDir is None:
parentDir = self.mktemp()
self.parent = parentDir
self.createFiles(self.files, parentDir)
def tearDown(self):
self.cleanUpModules()
class SysPathManglingTest(PackageTest):
def setUp(self, parent=None):
self.oldPath = sys.path[:]
self.newPath = sys.path[:]
if parent is None:
parent = self.mktemp()
PackageTest.setUp(self, parent)
self.newPath.append(self.parent)
self.mangleSysPath(self.newPath)
def tearDown(self):
PackageTest.tearDown(self)
self.mangleSysPath(self.oldPath)
def mangleSysPath(self, pathVar):
sys.path[:] = pathVar

View file

@ -0,0 +1,108 @@
"""This module is used by test_loader to test the Trial test loading
functionality. Do NOT change the number of tests in this module. Do NOT change
the names the tests in this module.
"""
import unittest as pyunit
from twisted.trial import unittest
from twisted.python.util import mergeFunctionMetadata
class FooTest(unittest.SynchronousTestCase):
def test_foo(self):
pass
def test_bar(self):
pass
def badDecorator(fn):
"""
Decorate a function without preserving the name of the original function.
Always return a function with the same name.
"""
def nameCollision(*args, **kwargs):
return fn(*args, **kwargs)
return nameCollision
def goodDecorator(fn):
"""
Decorate a function and preserve the original name.
"""
def nameCollision(*args, **kwargs):
return fn(*args, **kwargs)
return mergeFunctionMetadata(fn, nameCollision)
class DecorationTest(unittest.SynchronousTestCase):
def test_badDecorator(self):
"""
This test method is decorated in a way that gives it a confusing name
that collides with another method.
"""
test_badDecorator = badDecorator(test_badDecorator)
def test_goodDecorator(self):
"""
This test method is decorated in a way that preserves its name.
"""
test_goodDecorator = goodDecorator(test_goodDecorator)
def renamedDecorator(self):
"""
This is secretly a test method and will be decorated and then renamed so
test discovery can find it.
"""
test_renamedDecorator = goodDecorator(renamedDecorator)
def nameCollision(self):
"""
This isn't a test, it's just here to collide with tests.
"""
class PyunitTest(pyunit.TestCase):
def test_foo(self):
pass
def test_bar(self):
pass
class NotATest(object):
def test_foo(self):
pass
class AlphabetTest(unittest.SynchronousTestCase):
def test_a(self):
pass
def test_b(self):
pass
def test_c(self):
pass

View file

@ -0,0 +1,14 @@
#!/usr/bin/env python
# -*- test-case-name: twisted.trial.test.test_log,twisted.trial.test.test_class -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
# fodder for test_script, which parses files for emacs local variable
# declarations. This one is supposed to have:
# test-case-name: twisted.trial.test.test_log,twisted.trial.test.test_class
# in the second line
# The class declaration is irrelevant
class Foo(object):
pass

View file

@ -0,0 +1,270 @@
# -*- test-case-name: twisted.trial.test.test_tests -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Definitions of test cases with various interesting behaviors, to be used by
L{twisted.trial.test.test_tests} and other test modules to exercise different
features of trial's test runner.
See the L{twisted.trial.test.test_tests} module docstring for details about how
this code is arranged.
"""
from __future__ import division, absolute_import
from twisted.trial.unittest import (
SynchronousTestCase, TestCase, SkipTest, FailTest)
class SkippingMixin(object):
def test_skip1(self):
raise SkipTest('skip1')
def test_skip2(self):
raise RuntimeError("I should not get raised")
test_skip2.skip = 'skip2'
def test_skip3(self):
self.fail('I should not fail')
test_skip3.skip = 'skip3'
class SynchronousSkipping(SkippingMixin, SynchronousTestCase):
pass
class AsynchronousSkipping(SkippingMixin, TestCase):
pass
class SkippingSetUpMixin(object):
def setUp(self):
raise SkipTest('skipSetUp')
def test_1(self):
pass
def test_2(self):
pass
class SynchronousSkippingSetUp(SkippingSetUpMixin, SynchronousTestCase):
pass
class AsynchronousSkippingSetUp(SkippingSetUpMixin, TestCase):
pass
class DeprecatedReasonlessSkipMixin(object):
def test_1(self):
raise SkipTest()
class SynchronousDeprecatedReasonlessSkip(
DeprecatedReasonlessSkipMixin, SynchronousTestCase):
pass
class AsynchronousDeprecatedReasonlessSkip(
DeprecatedReasonlessSkipMixin, TestCase):
pass
class SkippedClassMixin(object):
skip = 'class'
def setUp(self):
self.__class__._setUpRan = True
def test_skip1(self):
raise SkipTest('skip1')
def test_skip2(self):
raise RuntimeError("Ought to skip me")
test_skip2.skip = 'skip2'
def test_skip3(self):
pass
def test_skip4(self):
raise RuntimeError("Skip me too")
class SynchronousSkippedClass(SkippedClassMixin, SynchronousTestCase):
pass
class AsynchronousSkippedClass(SkippedClassMixin, TestCase):
pass
class TodoMixin(object):
def test_todo1(self):
self.fail("deliberate failure")
test_todo1.todo = "todo1"
def test_todo2(self):
raise RuntimeError("deliberate error")
test_todo2.todo = "todo2"
def test_todo3(self):
"""unexpected success"""
test_todo3.todo = 'todo3'
class SynchronousTodo(TodoMixin, SynchronousTestCase):
pass
class AsynchronousTodo(TodoMixin, TestCase):
pass
class SetUpTodoMixin(object):
def setUp(self):
raise RuntimeError("deliberate error")
def test_todo1(self):
pass
test_todo1.todo = "setUp todo1"
class SynchronousSetUpTodo(SetUpTodoMixin, SynchronousTestCase):
pass
class AsynchronousSetUpTodo(SetUpTodoMixin, TestCase):
pass
class TearDownTodoMixin(object):
def tearDown(self):
raise RuntimeError("deliberate error")
def test_todo1(self):
pass
test_todo1.todo = "tearDown todo1"
class SynchronousTearDownTodo(TearDownTodoMixin, SynchronousTestCase):
pass
class AsynchronousTearDownTodo(TearDownTodoMixin, TestCase):
pass
class TodoClassMixin(object):
todo = "class"
def test_todo1(self):
pass
test_todo1.todo = "method"
def test_todo2(self):
pass
def test_todo3(self):
self.fail("Deliberate Failure")
test_todo3.todo = "method"
def test_todo4(self):
self.fail("Deliberate Failure")
class SynchronousTodoClass(TodoClassMixin, SynchronousTestCase):
pass
class AsynchronousTodoClass(TodoClassMixin, TestCase):
pass
class StrictTodoMixin(object):
def test_todo1(self):
raise RuntimeError("expected failure")
test_todo1.todo = (RuntimeError, "todo1")
def test_todo2(self):
raise RuntimeError("expected failure")
test_todo2.todo = ((RuntimeError, OSError), "todo2")
def test_todo3(self):
raise RuntimeError("we had no idea!")
test_todo3.todo = (OSError, "todo3")
def test_todo4(self):
raise RuntimeError("we had no idea!")
test_todo4.todo = ((OSError, SyntaxError), "todo4")
def test_todo5(self):
self.fail("deliberate failure")
test_todo5.todo = (FailTest, "todo5")
def test_todo6(self):
self.fail("deliberate failure")
test_todo6.todo = (RuntimeError, "todo6")
def test_todo7(self):
pass
test_todo7.todo = (RuntimeError, "todo7")
class SynchronousStrictTodo(StrictTodoMixin, SynchronousTestCase):
pass
class AsynchronousStrictTodo(StrictTodoMixin, TestCase):
pass
class AddCleanupMixin(object):
def setUp(self):
self.log = ['setUp']
def brokenSetUp(self):
self.log = ['setUp']
raise RuntimeError("Deliberate failure")
def skippingSetUp(self):
self.log = ['setUp']
raise SkipTest("Don't do this")
def append(self, thing):
self.log.append(thing)
def tearDown(self):
self.log.append('tearDown')
def runTest(self):
self.log.append('runTest')
class SynchronousAddCleanup(AddCleanupMixin, SynchronousTestCase):
pass
class AsynchronousAddCleanup(AddCleanupMixin, TestCase):
pass

View file

@ -0,0 +1,115 @@
# -*- test-case-name: twisted.trial.test.test_tests -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Test cases used to make sure that warning supression works at the module,
method, and class levels.
See the L{twisted.trial.test.test_tests} module docstring for details about how
this code is arranged.
"""
from __future__ import division, absolute_import
import warnings
from twisted.python.compat import _PY3
from twisted.trial import unittest, util
METHOD_WARNING_MSG = "method warning message"
CLASS_WARNING_MSG = "class warning message"
MODULE_WARNING_MSG = "module warning message"
class MethodWarning(Warning):
pass
class ClassWarning(Warning):
pass
class ModuleWarning(Warning):
pass
class EmitMixin:
def _emit(self):
warnings.warn(METHOD_WARNING_MSG, MethodWarning)
warnings.warn(CLASS_WARNING_MSG, ClassWarning)
warnings.warn(MODULE_WARNING_MSG, ModuleWarning)
class SuppressionMixin(EmitMixin):
suppress = [util.suppress(message=CLASS_WARNING_MSG)]
def testSuppressMethod(self):
self._emit()
testSuppressMethod.suppress = [util.suppress(message=METHOD_WARNING_MSG)]
def testSuppressClass(self):
self._emit()
def testOverrideSuppressClass(self):
self._emit()
testOverrideSuppressClass.suppress = []
class SetUpSuppressionMixin(object):
def setUp(self):
self._emit()
class TearDownSuppressionMixin(object):
def tearDown(self):
self._emit()
class TestSuppression2Mixin(EmitMixin):
def testSuppressModule(self):
self._emit()
suppress = [util.suppress(message=MODULE_WARNING_MSG)]
class SynchronousTestSuppression(SuppressionMixin, unittest.SynchronousTestCase):
pass
class SynchronousTestSetUpSuppression(SetUpSuppressionMixin, SynchronousTestSuppression):
pass
class SynchronousTestTearDownSuppression(TearDownSuppressionMixin, SynchronousTestSuppression):
pass
class SynchronousTestSuppression2(TestSuppression2Mixin, unittest.SynchronousTestCase):
pass
class AsynchronousTestSuppression(SuppressionMixin, unittest.TestCase):
pass
class AsynchronousTestSetUpSuppression(SetUpSuppressionMixin, AsynchronousTestSuppression):
pass
class AsynchronousTestTearDownSuppression(TearDownSuppressionMixin, AsynchronousTestSuppression):
pass
class AsynchronousTestSuppression2(TestSuppression2Mixin, unittest.TestCase):
pass

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,83 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for async assertions provided by C{twisted.trial.unittest.TestCase}.
"""
from __future__ import division, absolute_import
import unittest as pyunit
from twisted.python import failure
from twisted.internet import defer
from twisted.trial import unittest
class TestAsynchronousAssertions(unittest.TestCase):
"""
Tests for L{TestCase}'s asynchronous extensions to L{SynchronousTestCase}.
That is, assertFailure.
"""
def test_assertFailure(self):
d = defer.maybeDeferred(lambda: 1/0)
return self.assertFailure(d, ZeroDivisionError)
def test_assertFailure_wrongException(self):
d = defer.maybeDeferred(lambda: 1/0)
self.assertFailure(d, OverflowError)
d.addCallbacks(lambda x: self.fail('Should have failed'),
lambda x: x.trap(self.failureException))
return d
def test_assertFailure_noException(self):
d = defer.succeed(None)
self.assertFailure(d, ZeroDivisionError)
d.addCallbacks(lambda x: self.fail('Should have failed'),
lambda x: x.trap(self.failureException))
return d
def test_assertFailure_moreInfo(self):
"""
In the case of assertFailure failing, check that we get lots of
information about the exception that was raised.
"""
try:
1/0
except ZeroDivisionError:
f = failure.Failure()
d = defer.fail(f)
d = self.assertFailure(d, RuntimeError)
d.addErrback(self._checkInfo, f)
return d
def _checkInfo(self, assertionFailure, f):
assert assertionFailure.check(self.failureException)
output = assertionFailure.getErrorMessage()
self.assertIn(f.getErrorMessage(), output)
self.assertIn(f.getBriefTraceback(), output)
def test_assertFailure_masked(self):
"""
A single wrong assertFailure should fail the whole test.
"""
class ExampleFailure(Exception):
pass
class TC(unittest.TestCase):
failureException = ExampleFailure
def test_assertFailure(self):
d = defer.maybeDeferred(lambda: 1/0)
self.assertFailure(d, OverflowError)
self.assertFailure(d, ZeroDivisionError)
return d
test = TC('test_assertFailure')
result = pyunit.TestResult()
test.run(result)
self.assertEqual(1, len(result.failures))

View file

@ -0,0 +1,236 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for returning Deferreds from a TestCase.
"""
from __future__ import division, absolute_import
import unittest as pyunit
from twisted.internet import defer
from twisted.trial import unittest, reporter
from twisted.trial import util
from twisted.trial.test import detests
class TestSetUp(unittest.TestCase):
def _loadSuite(self, klass):
loader = pyunit.TestLoader()
r = reporter.TestResult()
s = loader.loadTestsFromTestCase(klass)
return r, s
def test_success(self):
result, suite = self._loadSuite(detests.DeferredSetUpOK)
suite(result)
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
def test_fail(self):
self.failIf(detests.DeferredSetUpFail.testCalled)
result, suite = self._loadSuite(detests.DeferredSetUpFail)
suite(result)
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 0)
self.assertEqual(len(result.errors), 1)
self.failIf(detests.DeferredSetUpFail.testCalled)
def test_callbackFail(self):
self.failIf(detests.DeferredSetUpCallbackFail.testCalled)
result, suite = self._loadSuite(detests.DeferredSetUpCallbackFail)
suite(result)
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 0)
self.assertEqual(len(result.errors), 1)
self.failIf(detests.DeferredSetUpCallbackFail.testCalled)
def test_error(self):
self.failIf(detests.DeferredSetUpError.testCalled)
result, suite = self._loadSuite(detests.DeferredSetUpError)
suite(result)
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 0)
self.assertEqual(len(result.errors), 1)
self.failIf(detests.DeferredSetUpError.testCalled)
def test_skip(self):
self.failIf(detests.DeferredSetUpSkip.testCalled)
result, suite = self._loadSuite(detests.DeferredSetUpSkip)
suite(result)
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 0)
self.assertEqual(len(result.errors), 0)
self.assertEqual(len(result.skips), 1)
self.failIf(detests.DeferredSetUpSkip.testCalled)
class TestNeverFire(unittest.TestCase):
def setUp(self):
self._oldTimeout = util.DEFAULT_TIMEOUT_DURATION
util.DEFAULT_TIMEOUT_DURATION = 0.1
def tearDown(self):
util.DEFAULT_TIMEOUT_DURATION = self._oldTimeout
def _loadSuite(self, klass):
loader = pyunit.TestLoader()
r = reporter.TestResult()
s = loader.loadTestsFromTestCase(klass)
return r, s
def test_setUp(self):
self.failIf(detests.DeferredSetUpNeverFire.testCalled)
result, suite = self._loadSuite(detests.DeferredSetUpNeverFire)
suite(result)
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 0)
self.assertEqual(len(result.errors), 1)
self.failIf(detests.DeferredSetUpNeverFire.testCalled)
self.failUnless(result.errors[0][1].check(defer.TimeoutError))
class TestTester(unittest.TestCase):
def getTest(self, name):
raise NotImplementedError("must override me")
def runTest(self, name):
result = reporter.TestResult()
self.getTest(name).run(result)
return result
class TestDeferred(TestTester):
def getTest(self, name):
return detests.DeferredTests(name)
def test_pass(self):
result = self.runTest('test_pass')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
def test_passGenerated(self):
result = self.runTest('test_passGenerated')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.failUnless(detests.DeferredTests.touched)
def test_fail(self):
result = self.runTest('test_fail')
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 1)
def test_failureInCallback(self):
result = self.runTest('test_failureInCallback')
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 1)
def test_errorInCallback(self):
result = self.runTest('test_errorInCallback')
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.errors), 1)
def test_skip(self):
result = self.runTest('test_skip')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.skips), 1)
self.failIf(detests.DeferredTests.touched)
def test_todo(self):
result = self.runTest('test_expectedFailure')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.errors), 0)
self.assertEqual(len(result.failures), 0)
self.assertEqual(len(result.expectedFailures), 1)
def test_thread(self):
result = self.runTest('test_thread')
self.assertEqual(result.testsRun, 1)
self.failUnless(result.wasSuccessful(), result.errors)
class TestTimeout(TestTester):
def getTest(self, name):
return detests.TimeoutTests(name)
def _wasTimeout(self, error):
self.assertEqual(error.check(defer.TimeoutError),
defer.TimeoutError)
def test_pass(self):
result = self.runTest('test_pass')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
def test_passDefault(self):
result = self.runTest('test_passDefault')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
def test_timeout(self):
result = self.runTest('test_timeout')
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.errors), 1)
self._wasTimeout(result.errors[0][1])
def test_timeoutZero(self):
result = self.runTest('test_timeoutZero')
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.errors), 1)
self._wasTimeout(result.errors[0][1])
def test_skip(self):
result = self.runTest('test_skip')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.skips), 1)
def test_todo(self):
result = self.runTest('test_expectedFailure')
self.failUnless(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.expectedFailures), 1)
self._wasTimeout(result.expectedFailures[0][1])
def test_errorPropagation(self):
result = self.runTest('test_errorPropagation')
self.failIf(result.wasSuccessful())
self.assertEqual(result.testsRun, 1)
self._wasTimeout(detests.TimeoutTests.timedOut)
def test_classTimeout(self):
loader = pyunit.TestLoader()
suite = loader.loadTestsFromTestCase(detests.TestClassTimeoutAttribute)
result = reporter.TestResult()
suite.run(result)
self.assertEqual(len(result.errors), 1)
self._wasTimeout(result.errors[0][1])
def test_callbackReturnsNonCallingDeferred(self):
#hacky timeout
# raises KeyboardInterrupt because Trial sucks
from twisted.internet import reactor
call = reactor.callLater(2, reactor.crash)
result = self.runTest('test_calledButNeverCallback')
if call.active():
call.cancel()
self.failIf(result.wasSuccessful())
self._wasTimeout(result.errors[0][1])
# The test loader erroneously attempts to run this:
del TestTester

View file

@ -0,0 +1,62 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Test Twisted's doctest support.
"""
from twisted.trial import itrial, runner, unittest, reporter
from twisted.trial.test import mockdoctest
class TestRunners(unittest.SynchronousTestCase):
"""
Tests for Twisted's doctest support.
"""
def test_id(self):
"""
Check that the id() of the doctests' case object contains the FQPN of
the actual tests.
"""
loader = runner.TestLoader()
suite = loader.loadDoctests(mockdoctest)
idPrefix = 'twisted.trial.test.mockdoctest.Counter'
for test in suite._tests:
self.assertIn(idPrefix, itrial.ITestCase(test).id())
def test_basicTrialIntegration(self):
"""
L{loadDoctests} loads all of the doctests in the given module.
"""
loader = runner.TestLoader()
suite = loader.loadDoctests(mockdoctest)
self.assertEqual(7, suite.countTestCases())
def _testRun(self, suite):
"""
Run C{suite} and check the result.
"""
result = reporter.TestResult()
suite.run(result)
self.assertEqual(5, result.successes)
self.assertEqual(2, len(result.failures))
def test_expectedResults(self, count=1):
"""
Trial can correctly run doctests with its xUnit test APIs.
"""
suite = runner.TestLoader().loadDoctests(mockdoctest)
self._testRun(suite)
def test_repeatable(self):
"""
Doctests should be runnable repeatably.
"""
suite = runner.TestLoader().loadDoctests(mockdoctest)
self._testRun(suite)
self._testRun(suite)

View file

@ -0,0 +1,119 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for interrupting tests with Control-C.
"""
import StringIO
from twisted.trial import unittest
from twisted.trial import reporter, runner
class TrialTest(unittest.SynchronousTestCase):
def setUp(self):
self.output = StringIO.StringIO()
self.reporter = reporter.TestResult()
self.loader = runner.TestLoader()
class TestInterruptInTest(TrialTest):
class InterruptedTest(unittest.TestCase):
def test_02_raiseInterrupt(self):
raise KeyboardInterrupt
def test_01_doNothing(self):
pass
def test_03_doNothing(self):
TestInterruptInTest.test_03_doNothing_run = True
def setUp(self):
super(TestInterruptInTest, self).setUp()
self.suite = self.loader.loadClass(TestInterruptInTest.InterruptedTest)
TestInterruptInTest.test_03_doNothing_run = None
def test_setUpOK(self):
self.assertEqual(3, self.suite.countTestCases())
self.assertEqual(0, self.reporter.testsRun)
self.failIf(self.reporter.shouldStop)
def test_interruptInTest(self):
runner.TrialSuite([self.suite]).run(self.reporter)
self.failUnless(self.reporter.shouldStop)
self.assertEqual(2, self.reporter.testsRun)
self.failIf(TestInterruptInTest.test_03_doNothing_run,
"test_03_doNothing ran.")
class TestInterruptInSetUp(TrialTest):
testsRun = 0
class InterruptedTest(unittest.TestCase):
def setUp(self):
if TestInterruptInSetUp.testsRun > 0:
raise KeyboardInterrupt
def test_01(self):
TestInterruptInSetUp.testsRun += 1
def test_02(self):
TestInterruptInSetUp.testsRun += 1
TestInterruptInSetUp.test_02_run = True
def setUp(self):
super(TestInterruptInSetUp, self).setUp()
self.suite = self.loader.loadClass(
TestInterruptInSetUp.InterruptedTest)
TestInterruptInSetUp.test_02_run = False
TestInterruptInSetUp.testsRun = 0
def test_setUpOK(self):
self.assertEqual(0, TestInterruptInSetUp.testsRun)
self.assertEqual(2, self.suite.countTestCases())
self.assertEqual(0, self.reporter.testsRun)
self.failIf(self.reporter.shouldStop)
def test_interruptInSetUp(self):
runner.TrialSuite([self.suite]).run(self.reporter)
self.failUnless(self.reporter.shouldStop)
self.assertEqual(2, self.reporter.testsRun)
self.failIf(TestInterruptInSetUp.test_02_run,
"test_02 ran")
class TestInterruptInTearDown(TrialTest):
testsRun = 0
class InterruptedTest(unittest.TestCase):
def tearDown(self):
if TestInterruptInTearDown.testsRun > 0:
raise KeyboardInterrupt
def test_01(self):
TestInterruptInTearDown.testsRun += 1
def test_02(self):
TestInterruptInTearDown.testsRun += 1
TestInterruptInTearDown.test_02_run = True
def setUp(self):
super(TestInterruptInTearDown, self).setUp()
self.suite = self.loader.loadClass(
TestInterruptInTearDown.InterruptedTest)
TestInterruptInTearDown.testsRun = 0
TestInterruptInTearDown.test_02_run = False
def test_setUpOK(self):
self.assertEqual(0, TestInterruptInTearDown.testsRun)
self.assertEqual(2, self.suite.countTestCases())
self.assertEqual(0, self.reporter.testsRun)
self.failIf(self.reporter.shouldStop)
def test_interruptInTearDown(self):
runner.TrialSuite([self.suite]).run(self.reporter)
self.assertEqual(1, self.reporter.testsRun)
self.failUnless(self.reporter.shouldStop)
self.failIf(TestInterruptInTearDown.test_02_run,
"test_02 ran")

View file

@ -0,0 +1,656 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for loading tests by name.
"""
import os
import sys
import unittest as pyunit
from hashlib import md5
from twisted.python import util, filepath
from twisted.trial.test import packages
from twisted.trial import runner, reporter, unittest
from twisted.trial.itrial import ITestCase
from twisted.python.modules import getModule
def testNames(tests):
"""
Return the id of each test within the given test suite or case.
"""
names = []
for test in unittest._iterateTests(tests):
names.append(test.id())
return names
class FinderTest(packages.PackageTest):
"""
Tests for L{runner.TestLoader.findByName}.
"""
def setUp(self):
packages.PackageTest.setUp(self)
self.loader = runner.TestLoader()
def tearDown(self):
packages.PackageTest.tearDown(self)
def test_findPackage(self):
sample1 = self.loader.findByName('twisted')
import twisted as sample2
self.assertEqual(sample1, sample2)
def test_findModule(self):
sample1 = self.loader.findByName('twisted.trial.test.sample')
import sample as sample2
self.assertEqual(sample1, sample2)
def test_findFile(self):
path = util.sibpath(__file__, 'sample.py')
sample1 = self.loader.findByName(path)
import sample as sample2
self.assertEqual(sample1, sample2)
def test_findObject(self):
sample1 = self.loader.findByName('twisted.trial.test.sample.FooTest')
import sample
self.assertEqual(sample.FooTest, sample1)
def test_findNonModule(self):
self.failUnlessRaises(AttributeError,
self.loader.findByName,
'twisted.trial.test.nonexistent')
def test_findNonPackage(self):
self.failUnlessRaises(ValueError,
self.loader.findByName,
'nonextant')
def test_findNonFile(self):
path = util.sibpath(__file__, 'nonexistent.py')
self.failUnlessRaises(ValueError, self.loader.findByName, path)
class FileTest(packages.SysPathManglingTest):
"""
Tests for L{runner.filenameToModule}.
"""
def test_notFile(self):
"""
L{runner.filenameToModule} raises a C{ValueError} when a non-existing
file is passed.
"""
err = self.assertRaises(ValueError, runner.filenameToModule, 'it')
self.assertEqual(str(err), "'it' doesn't exist")
def test_moduleInPath(self):
"""
If the file in question is a module on the Python path, then it should
properly import and return that module.
"""
sample1 = runner.filenameToModule(util.sibpath(__file__, 'sample.py'))
import sample as sample2
self.assertEqual(sample2, sample1)
def test_moduleNotInPath(self):
"""
If passed the path to a file containing the implementation of a
module within a package which is not on the import path,
L{runner.filenameToModule} returns a module object loosely
resembling the module defined by that file anyway.
"""
# "test_sample" isn't actually the name of this module. However,
# filenameToModule can't seem to figure that out. So clean up this
# mis-named module. It would be better if this weren't necessary
# and filenameToModule either didn't exist or added a correctly
# named module to sys.modules.
self.addCleanup(sys.modules.pop, 'test_sample', None)
self.mangleSysPath(self.oldPath)
sample1 = runner.filenameToModule(
os.path.join(self.parent, 'goodpackage', 'test_sample.py'))
self.mangleSysPath(self.newPath)
from goodpackage import test_sample as sample2
self.assertEqual(os.path.splitext(sample2.__file__)[0],
os.path.splitext(sample1.__file__)[0])
def test_packageInPath(self):
"""
If the file in question is a package on the Python path, then it should
properly import and return that package.
"""
package1 = runner.filenameToModule(os.path.join(self.parent,
'goodpackage'))
import goodpackage
self.assertEqual(goodpackage, package1)
def test_packageNotInPath(self):
"""
If passed the path to a directory which represents a package which
is not on the import path, L{runner.filenameToModule} returns a
module object loosely resembling the package defined by that
directory anyway.
"""
# "__init__" isn't actually the name of the package! However,
# filenameToModule is pretty stupid and decides that is its name
# after all. Make sure it gets cleaned up. See the comment in
# test_moduleNotInPath for possible courses of action related to
# this.
self.addCleanup(sys.modules.pop, "__init__")
self.mangleSysPath(self.oldPath)
package1 = runner.filenameToModule(
os.path.join(self.parent, 'goodpackage'))
self.mangleSysPath(self.newPath)
import goodpackage
self.assertEqual(os.path.splitext(goodpackage.__file__)[0],
os.path.splitext(package1.__file__)[0])
def test_directoryNotPackage(self):
"""
L{runner.filenameToModule} raises a C{ValueError} when the name of an
empty directory is passed that isn't considered a valid Python package
because it doesn't contain a C{__init__.py} file.
"""
emptyDir = filepath.FilePath(self.parent).child("emptyDirectory")
emptyDir.createDirectory()
err = self.assertRaises(ValueError, runner.filenameToModule,
emptyDir.path)
self.assertEqual(str(err), "%r is not a package directory" % (
emptyDir.path,))
def test_filenameNotPython(self):
"""
L{runner.filenameToModule} raises a C{SyntaxError} when a non-Python
file is passed.
"""
filename = filepath.FilePath(self.parent).child('notpython')
filename.setContent("This isn't python")
self.failUnlessRaises(
SyntaxError, runner.filenameToModule, filename.path)
def test_filenameMatchesPackage(self):
"""
The C{__file__} attribute of the module should match the package name.
"""
filename = filepath.FilePath(self.parent).child('goodpackage.py')
filename.setContent(packages.testModule)
try:
module = runner.filenameToModule(filename.path)
self.assertEqual(filename.path, module.__file__)
finally:
filename.remove()
def test_directory(self):
"""
Test loader against a filesystem directory containing an empty
C{__init__.py} file. It should handle 'path' and 'path/' the same way.
"""
goodDir = filepath.FilePath(self.parent).child('goodDirectory')
goodDir.createDirectory()
goodDir.child('__init__.py').setContent('')
try:
module = runner.filenameToModule(goodDir.path)
self.assert_(module.__name__.endswith('goodDirectory'))
module = runner.filenameToModule(goodDir.path + os.path.sep)
self.assert_(module.__name__.endswith('goodDirectory'))
finally:
goodDir.remove()
class LoaderTest(packages.SysPathManglingTest):
"""
Tests for L{trial.TestLoader}.
"""
def setUp(self):
self.loader = runner.TestLoader()
packages.SysPathManglingTest.setUp(self)
def test_sortCases(self):
import sample
suite = self.loader.loadClass(sample.AlphabetTest)
self.assertEqual(['test_a', 'test_b', 'test_c'],
[test._testMethodName for test in suite._tests])
newOrder = ['test_b', 'test_c', 'test_a']
sortDict = dict(zip(newOrder, range(3)))
self.loader.sorter = lambda x : sortDict.get(x.shortDescription(), -1)
suite = self.loader.loadClass(sample.AlphabetTest)
self.assertEqual(newOrder,
[test._testMethodName for test in suite._tests])
def test_loadMethod(self):
import sample
suite = self.loader.loadMethod(sample.FooTest.test_foo)
self.assertEqual(1, suite.countTestCases())
self.assertEqual('test_foo', suite._testMethodName)
def test_loadFailingMethod(self):
# test added for issue1353
import erroneous
suite = self.loader.loadMethod(erroneous.TestRegularFail.test_fail)
result = reporter.TestResult()
suite.run(result)
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 1)
def test_loadNonMethod(self):
import sample
self.failUnlessRaises(TypeError, self.loader.loadMethod, sample)
self.failUnlessRaises(TypeError,
self.loader.loadMethod, sample.FooTest)
self.failUnlessRaises(TypeError, self.loader.loadMethod, "string")
self.failUnlessRaises(TypeError,
self.loader.loadMethod, ('foo', 'bar'))
def test_loadBadDecorator(self):
"""
A decorated test method for which the decorator has failed to set the
method's __name__ correctly is loaded and its name in the class scope
discovered.
"""
import sample
suite = self.loader.loadMethod(sample.DecorationTest.test_badDecorator)
self.assertEqual(1, suite.countTestCases())
self.assertEqual('test_badDecorator', suite._testMethodName)
def test_loadGoodDecorator(self):
"""
A decorated test method for which the decorator has set the method's
__name__ correctly is loaded and the only name by which it goes is used.
"""
import sample
suite = self.loader.loadMethod(
sample.DecorationTest.test_goodDecorator)
self.assertEqual(1, suite.countTestCases())
self.assertEqual('test_goodDecorator', suite._testMethodName)
def test_loadRenamedDecorator(self):
"""
Load a decorated method which has been copied to a new name inside the
class. Thus its __name__ and its key in the class's __dict__ no
longer match.
"""
import sample
suite = self.loader.loadMethod(
sample.DecorationTest.test_renamedDecorator)
self.assertEqual(1, suite.countTestCases())
self.assertEqual('test_renamedDecorator', suite._testMethodName)
def test_loadClass(self):
import sample
suite = self.loader.loadClass(sample.FooTest)
self.assertEqual(2, suite.countTestCases())
self.assertEqual(['test_bar', 'test_foo'],
[test._testMethodName for test in suite._tests])
def test_loadNonClass(self):
import sample
self.failUnlessRaises(TypeError, self.loader.loadClass, sample)
self.failUnlessRaises(TypeError,
self.loader.loadClass, sample.FooTest.test_foo)
self.failUnlessRaises(TypeError, self.loader.loadClass, "string")
self.failUnlessRaises(TypeError,
self.loader.loadClass, ('foo', 'bar'))
def test_loadNonTestCase(self):
import sample
self.failUnlessRaises(ValueError, self.loader.loadClass,
sample.NotATest)
def test_loadModule(self):
import sample
suite = self.loader.loadModule(sample)
self.assertEqual(10, suite.countTestCases())
def test_loadNonModule(self):
import sample
self.failUnlessRaises(TypeError,
self.loader.loadModule, sample.FooTest)
self.failUnlessRaises(TypeError,
self.loader.loadModule, sample.FooTest.test_foo)
self.failUnlessRaises(TypeError, self.loader.loadModule, "string")
self.failUnlessRaises(TypeError,
self.loader.loadModule, ('foo', 'bar'))
def test_loadPackage(self):
import goodpackage
suite = self.loader.loadPackage(goodpackage)
self.assertEqual(7, suite.countTestCases())
def test_loadNonPackage(self):
import sample
self.failUnlessRaises(TypeError,
self.loader.loadPackage, sample.FooTest)
self.failUnlessRaises(TypeError,
self.loader.loadPackage, sample.FooTest.test_foo)
self.failUnlessRaises(TypeError, self.loader.loadPackage, "string")
self.failUnlessRaises(TypeError,
self.loader.loadPackage, ('foo', 'bar'))
def test_loadModuleAsPackage(self):
import sample
## XXX -- should this instead raise a ValueError? -- jml
self.failUnlessRaises(TypeError, self.loader.loadPackage, sample)
def test_loadPackageRecursive(self):
import goodpackage
suite = self.loader.loadPackage(goodpackage, recurse=True)
self.assertEqual(14, suite.countTestCases())
def test_loadAnythingOnModule(self):
import sample
suite = self.loader.loadAnything(sample)
self.assertEqual(sample.__name__,
suite._tests[0]._tests[0].__class__.__module__)
def test_loadAnythingOnClass(self):
import sample
suite = self.loader.loadAnything(sample.FooTest)
self.assertEqual(2, suite.countTestCases())
def test_loadAnythingOnMethod(self):
import sample
suite = self.loader.loadAnything(sample.FooTest.test_foo)
self.assertEqual(1, suite.countTestCases())
def test_loadAnythingOnPackage(self):
import goodpackage
suite = self.loader.loadAnything(goodpackage)
self.failUnless(isinstance(suite, self.loader.suiteFactory))
self.assertEqual(7, suite.countTestCases())
def test_loadAnythingOnPackageRecursive(self):
import goodpackage
suite = self.loader.loadAnything(goodpackage, recurse=True)
self.failUnless(isinstance(suite, self.loader.suiteFactory))
self.assertEqual(14, suite.countTestCases())
def test_loadAnythingOnString(self):
# the important thing about this test is not the string-iness
# but the non-handledness.
self.failUnlessRaises(TypeError,
self.loader.loadAnything, "goodpackage")
def test_importErrors(self):
import package
suite = self.loader.loadPackage(package, recurse=True)
result = reporter.Reporter()
suite.run(result)
self.assertEqual(False, result.wasSuccessful())
self.assertEqual(2, len(result.errors))
errors = [test.id() for test, error in result.errors]
errors.sort()
self.assertEqual(errors, ['package.test_bad_module',
'package.test_import_module'])
def test_differentInstances(self):
"""
L{TestLoader.loadClass} returns a suite with each test method
represented by a different instances of the L{TestCase} they are
defined on.
"""
class DistinctInstances(pyunit.TestCase):
def test_1(self):
self.first = 'test1Run'
def test_2(self):
self.assertFalse(hasattr(self, 'first'))
suite = self.loader.loadClass(DistinctInstances)
result = reporter.Reporter()
suite.run(result)
self.assertTrue(result.wasSuccessful())
def test_loadModuleWith_test_suite(self):
"""
Check that C{test_suite} is used when present and other L{TestCase}s are
not included.
"""
from twisted.trial.test import mockcustomsuite
suite = self.loader.loadModule(mockcustomsuite)
self.assertEqual(0, suite.countTestCases())
self.assertEqual("MyCustomSuite", getattr(suite, 'name', None))
def test_loadModuleWith_testSuite(self):
"""
Check that C{testSuite} is used when present and other L{TestCase}s are
not included.
"""
from twisted.trial.test import mockcustomsuite2
suite = self.loader.loadModule(mockcustomsuite2)
self.assertEqual(0, suite.countTestCases())
self.assertEqual("MyCustomSuite", getattr(suite, 'name', None))
def test_loadModuleWithBothCustom(self):
"""
Check that if C{testSuite} and C{test_suite} are both present in a
module then C{testSuite} gets priority.
"""
from twisted.trial.test import mockcustomsuite3
suite = self.loader.loadModule(mockcustomsuite3)
self.assertEqual('testSuite', getattr(suite, 'name', None))
def test_customLoadRaisesAttributeError(self):
"""
Make sure that any C{AttributeError}s raised by C{testSuite} are not
swallowed by L{TestLoader}.
"""
def testSuite():
raise AttributeError('should be reraised')
from twisted.trial.test import mockcustomsuite2
mockcustomsuite2.testSuite, original = (testSuite,
mockcustomsuite2.testSuite)
try:
self.assertRaises(AttributeError, self.loader.loadModule,
mockcustomsuite2)
finally:
mockcustomsuite2.testSuite = original
# XXX - duplicated and modified from test_script
def assertSuitesEqual(self, test1, test2):
names1 = testNames(test1)
names2 = testNames(test2)
names1.sort()
names2.sort()
self.assertEqual(names1, names2)
def test_loadByNamesDuplicate(self):
"""
Check that loadByNames ignores duplicate names
"""
module = 'twisted.trial.test.test_log'
suite1 = self.loader.loadByNames([module, module], True)
suite2 = self.loader.loadByName(module, True)
self.assertSuitesEqual(suite1, suite2)
def test_loadByNamesPreservesOrder(self):
"""
L{TestLoader.loadByNames} preserves the order of tests provided to it.
"""
modules = [
"inheritancepackage.test_x.A.test_foo",
"twisted.trial.test.sample",
"goodpackage",
"twisted.trial.test.test_log",
"twisted.trial.test.sample.FooTest",
"package.test_module"]
suite1 = self.loader.loadByNames(modules)
suite2 = runner.TestSuite(map(self.loader.loadByName, modules))
self.assertEqual(testNames(suite1), testNames(suite2))
def test_loadDifferentNames(self):
"""
Check that loadByNames loads all the names that it is given
"""
modules = ['goodpackage', 'package.test_module']
suite1 = self.loader.loadByNames(modules)
suite2 = runner.TestSuite(map(self.loader.loadByName, modules))
self.assertSuitesEqual(suite1, suite2)
def test_loadInheritedMethods(self):
"""
Check that test methods names which are inherited from are all
loaded rather than just one.
"""
methods = ['inheritancepackage.test_x.A.test_foo',
'inheritancepackage.test_x.B.test_foo']
suite1 = self.loader.loadByNames(methods)
suite2 = runner.TestSuite(map(self.loader.loadByName, methods))
self.assertSuitesEqual(suite1, suite2)
class ZipLoadingTest(LoaderTest):
def setUp(self):
from twisted.python.test.test_zippath import zipit
LoaderTest.setUp(self)
zipit(self.parent, self.parent+'.zip')
self.parent += '.zip'
self.mangleSysPath(self.oldPath+[self.parent])
class PackageOrderingTest(packages.SysPathManglingTest):
def setUp(self):
self.loader = runner.TestLoader()
self.topDir = self.mktemp()
parent = os.path.join(self.topDir, "uberpackage")
os.makedirs(parent)
open(os.path.join(parent, "__init__.py"), "wb").close()
packages.SysPathManglingTest.setUp(self, parent)
self.mangleSysPath(self.oldPath + [self.topDir])
def _trialSortAlgorithm(self, sorter):
"""
Right now, halfway by accident, trial sorts like this:
1. all modules are grouped together in one list and sorted.
2. within each module, the classes are grouped together in one list
and sorted.
3. finally within each class, each test method is grouped together
in a list and sorted.
This attempts to return a sorted list of testable thingies following
those rules, so that we can compare the behavior of loadPackage.
The things that show as 'cases' are errors from modules which failed to
import, and test methods. Let's gather all those together.
"""
pkg = getModule('uberpackage')
testModules = []
for testModule in pkg.walkModules():
if testModule.name.split(".")[-1].startswith("test_"):
testModules.append(testModule)
sortedModules = sorted(testModules, key=sorter) # ONE
for modinfo in sortedModules:
# Now let's find all the classes.
module = modinfo.load(None)
if module is None:
yield modinfo
else:
testClasses = []
for attrib in modinfo.iterAttributes():
if runner.isTestCase(attrib.load()):
testClasses.append(attrib)
sortedClasses = sorted(testClasses, key=sorter) # TWO
for clsinfo in sortedClasses:
testMethods = []
for attr in clsinfo.iterAttributes():
if attr.name.split(".")[-1].startswith('test'):
testMethods.append(attr)
sortedMethods = sorted(testMethods, key=sorter) # THREE
for methinfo in sortedMethods:
yield methinfo
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))
def test_sortPackagesDefaultOrder(self):
self.loadSortedPackages()
def test_sortPackagesSillyOrder(self):
def sillySorter(s):
# This has to work on fully-qualified class names and class
# objects, which is silly, but it's the "spec", such as it is.
# if isinstance(s, type) or isinstance(s, types.ClassType):
# return s.__module__+'.'+s.__name__
n = runner.name(s)
d = md5(n).hexdigest()
return d
self.loadSortedPackages(sillySorter)

View file

@ -0,0 +1,235 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Test the interaction between trial and errors logged during test run.
"""
from __future__ import division
import time
from twisted.internet import reactor, task
from twisted.python import failure, log
from twisted.trial import unittest, reporter
def makeFailure():
"""
Return a new, realistic failure.
"""
try:
1/0
except ZeroDivisionError:
f = failure.Failure()
return f
class Mask(object):
"""
Hide C{MockTest}s from Trial's automatic test finder.
"""
class FailureLoggingMixin(object):
def test_silent(self):
"""
Don't log any errors.
"""
def test_single(self):
"""
Log a single error.
"""
log.err(makeFailure())
def test_double(self):
"""
Log two errors.
"""
log.err(makeFailure())
log.err(makeFailure())
class SynchronousFailureLogging(FailureLoggingMixin, unittest.SynchronousTestCase):
pass
class AsynchronousFailureLogging(FailureLoggingMixin, unittest.TestCase):
def test_inCallback(self):
"""
Log an error in an asynchronous callback.
"""
return task.deferLater(reactor, 0, lambda: log.err(makeFailure()))
class TestObserver(unittest.SynchronousTestCase):
"""
Tests for L{unittest._LogObserver}, a helper for the implementation of
L{SynchronousTestCase.flushLoggedErrors}.
"""
def setUp(self):
self.result = reporter.TestResult()
self.observer = unittest._LogObserver()
def test_msg(self):
"""
Test that a standard log message doesn't go anywhere near the result.
"""
self.observer.gotEvent({'message': ('some message',),
'time': time.time(), 'isError': 0,
'system': '-'})
self.assertEqual(self.observer.getErrors(), [])
def test_error(self):
"""
Test that an observed error gets added to the result
"""
f = makeFailure()
self.observer.gotEvent({'message': (),
'time': time.time(), 'isError': 1,
'system': '-', 'failure': f,
'why': None})
self.assertEqual(self.observer.getErrors(), [f])
def test_flush(self):
"""
Check that flushing the observer with no args removes all errors.
"""
self.test_error()
flushed = self.observer.flushErrors()
self.assertEqual(self.observer.getErrors(), [])
self.assertEqual(len(flushed), 1)
self.assertTrue(flushed[0].check(ZeroDivisionError))
def _makeRuntimeFailure(self):
return failure.Failure(RuntimeError('test error'))
def test_flushByType(self):
"""
Check that flushing the observer remove all failures of the given type.
"""
self.test_error() # log a ZeroDivisionError to the observer
f = self._makeRuntimeFailure()
self.observer.gotEvent(dict(message=(), time=time.time(), isError=1,
system='-', failure=f, why=None))
flushed = self.observer.flushErrors(ZeroDivisionError)
self.assertEqual(self.observer.getErrors(), [f])
self.assertEqual(len(flushed), 1)
self.assertTrue(flushed[0].check(ZeroDivisionError))
def test_ignoreErrors(self):
"""
Check that C{_ignoreErrors} actually causes errors to be ignored.
"""
self.observer._ignoreErrors(ZeroDivisionError)
f = makeFailure()
self.observer.gotEvent({'message': (),
'time': time.time(), 'isError': 1,
'system': '-', 'failure': f,
'why': None})
self.assertEqual(self.observer.getErrors(), [])
def test_clearIgnores(self):
"""
Check that C{_clearIgnores} ensures that previously ignored errors
get captured.
"""
self.observer._ignoreErrors(ZeroDivisionError)
self.observer._clearIgnores()
f = makeFailure()
self.observer.gotEvent({'message': (),
'time': time.time(), 'isError': 1,
'system': '-', 'failure': f,
'why': None})
self.assertEqual(self.observer.getErrors(), [f])
class LogErrorsMixin(object):
"""
High-level tests demonstrating the expected behaviour of logged errors
during tests.
"""
def setUp(self):
self.result = reporter.TestResult()
def tearDown(self):
self.flushLoggedErrors(ZeroDivisionError)
def test_singleError(self):
"""
Test that a logged error gets reported as a test error.
"""
test = self.MockTest('test_single')
test(self.result)
self.assertEqual(len(self.result.errors), 1)
self.assertTrue(self.result.errors[0][1].check(ZeroDivisionError),
self.result.errors[0][1])
self.assertEqual(0, self.result.successes)
def test_twoErrors(self):
"""
Test that when two errors get logged, they both get reported as test
errors.
"""
test = self.MockTest('test_double')
test(self.result)
self.assertEqual(len(self.result.errors), 2)
self.assertEqual(0, self.result.successes)
def test_errorsIsolated(self):
"""
Check that an error logged in one test doesn't fail the next test.
"""
t1 = self.MockTest('test_single')
t2 = self.MockTest('test_silent')
t1(self.result)
t2(self.result)
self.assertEqual(len(self.result.errors), 1)
self.assertEqual(self.result.errors[0][0], t1)
self.assertEqual(1, self.result.successes)
def test_boundedObservers(self):
"""
There are no extra log observers after a test runs.
"""
# XXX trial is *all about* global log state. It should really be fixed.
observer = unittest._LogObserver()
self.patch(unittest, '_logObserver', observer)
observers = log.theLogPublisher.observers[:]
test = self.MockTest()
test(self.result)
self.assertEqual(observers, log.theLogPublisher.observers)
class SynchronousLogErrorsTests(LogErrorsMixin, unittest.SynchronousTestCase):
MockTest = Mask.SynchronousFailureLogging
class AsynchronousLogErrorsTests(LogErrorsMixin, unittest.TestCase):
MockTest = Mask.AsynchronousFailureLogging
def test_inCallback(self):
"""
Test that errors logged in callbacks get reported as test errors.
"""
test = self.MockTest('test_inCallback')
test(self.result)
self.assertEqual(len(self.result.errors), 1)
self.assertTrue(self.result.errors[0][1].check(ZeroDivisionError),
self.result.errors[0][1])

View file

@ -0,0 +1,179 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for the output generated by trial.
"""
import os, StringIO
from twisted.scripts import trial
from twisted.trial import runner
from twisted.trial.test import packages
def runTrial(*args):
from twisted.trial import reporter
config = trial.Options()
config.parseOptions(args)
output = StringIO.StringIO()
myRunner = runner.TrialRunner(
reporter.VerboseTextReporter,
stream=output,
workingDirectory=config['temp-directory'])
suite = trial._getSuite(config)
result = myRunner.run(suite)
return output.getvalue()
class TestImportErrors(packages.SysPathManglingTest):
"""Actually run trial as if on the command line and check that the output
is what we expect.
"""
debug = False
parent = "_testImportErrors"
def runTrial(self, *args):
return runTrial('--temp-directory', self.mktemp(), *args)
def _print(self, stuff):
print stuff
return stuff
def assertIn(self, container, containee, *args, **kwargs):
# redefined to be useful in callbacks
super(TestImportErrors, self).assertIn(
containee, container, *args, **kwargs)
return container
def assertNotIn(self, container, containee, *args, **kwargs):
# redefined to be useful in callbacks
super(TestImportErrors, self).assertNotIn(
containee, container, *args, **kwargs)
return container
def test_trialRun(self):
self.runTrial()
def test_nonexistentModule(self):
d = self.runTrial('twisted.doesntexist')
self.assertIn(d, '[ERROR]')
self.assertIn(d, 'twisted.doesntexist')
return d
def test_nonexistentPackage(self):
d = self.runTrial('doesntexist')
self.assertIn(d, 'doesntexist')
self.assertIn(d, 'ModuleNotFound')
self.assertIn(d, '[ERROR]')
return d
def test_nonexistentPackageWithModule(self):
d = self.runTrial('doesntexist.barney')
self.assertIn(d, 'doesntexist.barney')
self.assertIn(d, 'ObjectNotFound')
self.assertIn(d, '[ERROR]')
return d
def test_badpackage(self):
d = self.runTrial('badpackage')
self.assertIn(d, '[ERROR]')
self.assertIn(d, 'badpackage')
self.assertNotIn(d, 'IOError')
return d
def test_moduleInBadpackage(self):
d = self.runTrial('badpackage.test_module')
self.assertIn(d, "[ERROR]")
self.assertIn(d, "badpackage.test_module")
self.assertNotIn(d, 'IOError')
return d
def test_badmodule(self):
d = self.runTrial('package.test_bad_module')
self.assertIn(d, '[ERROR]')
self.assertIn(d, 'package.test_bad_module')
self.assertNotIn(d, 'IOError')
self.assertNotIn(d, '<module ')
return d
def test_badimport(self):
d = self.runTrial('package.test_import_module')
self.assertIn(d, '[ERROR]')
self.assertIn(d, 'package.test_import_module')
self.assertNotIn(d, 'IOError')
self.assertNotIn(d, '<module ')
return d
def test_recurseImport(self):
d = self.runTrial('package')
self.assertIn(d, '[ERROR]')
self.assertIn(d, 'test_bad_module')
self.assertIn(d, 'test_import_module')
self.assertNotIn(d, '<module ')
self.assertNotIn(d, 'IOError')
return d
def test_recurseImportErrors(self):
d = self.runTrial('package2')
self.assertIn(d, '[ERROR]')
self.assertIn(d, 'package2')
self.assertIn(d, 'test_module')
self.assertIn(d, "No module named frotz")
self.assertNotIn(d, '<module ')
self.assertNotIn(d, 'IOError')
return d
def test_nonRecurseImportErrors(self):
d = self.runTrial('-N', 'package2')
self.assertIn(d, '[ERROR]')
self.assertIn(d, "No module named frotz")
self.assertNotIn(d, '<module ')
return d
def test_regularRun(self):
d = self.runTrial('package.test_module')
self.assertNotIn(d, '[ERROR]')
self.assertNotIn(d, 'IOError')
self.assertIn(d, 'OK')
self.assertIn(d, 'PASSED (successes=1)')
return d
def test_filename(self):
self.mangleSysPath(self.oldPath)
d = self.runTrial(
os.path.join(self.parent, 'package', 'test_module.py'))
self.assertNotIn(d, '[ERROR]')
self.assertNotIn(d, 'IOError')
self.assertIn(d, 'OK')
self.assertIn(d, 'PASSED (successes=1)')
return d
def test_dosFile(self):
## XXX -- not really an output test, more of a script test
self.mangleSysPath(self.oldPath)
d = self.runTrial(
os.path.join(self.parent,
'package', 'test_dos_module.py'))
self.assertNotIn(d, '[ERROR]')
self.assertNotIn(d, 'IOError')
self.assertIn(d, 'OK')
self.assertIn(d, 'PASSED (successes=1)')
return d

View file

@ -0,0 +1,46 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
#
# Maintainer: Jonathan Lange
"""
Tests for L{twisted.plugins.twisted_trial}.
"""
from twisted.plugin import getPlugins
from twisted.trial import unittest
from twisted.trial.itrial import IReporter
class TestPlugins(unittest.SynchronousTestCase):
"""
Tests for Trial's reporter plugins.
"""
def getPluginsByLongOption(self, longOption):
"""
Return the Trial reporter plugin with the given long option.
If more than one is found, raise ValueError. If none are found, raise
IndexError.
"""
plugins = [
plugin for plugin in getPlugins(IReporter)
if plugin.longOpt == longOption]
if len(plugins) > 1:
raise ValueError(
"More than one plugin found with long option %r: %r"
% (longOption, plugins))
return plugins[0]
def test_subunitPlugin(self):
"""
One of the reporter plugins is the subunit reporter plugin.
"""
subunitPlugin = self.getPluginsByLongOption('subunit')
self.assertEqual('Subunit Reporter', subunitPlugin.name)
self.assertEqual('twisted.trial.reporter', subunitPlugin.module)
self.assertEqual('subunit', subunitPlugin.longOpt)
self.assertIdentical(None, subunitPlugin.shortOpt)
self.assertEqual('SubunitReporter', subunitPlugin.klass)

View file

@ -0,0 +1,287 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
from __future__ import division, absolute_import
import sys
import traceback
from zope.interface import implementer
from twisted.python.compat import _PY3
from twisted.python.failure import Failure
from twisted.trial.unittest import SynchronousTestCase, PyUnitResultAdapter
from twisted.trial.itrial import IReporter, ITestCase
import unittest as pyunit
class TestPyUnitTestCase(SynchronousTestCase):
class PyUnitTest(pyunit.TestCase):
def test_pass(self):
pass
def setUp(self):
self.original = self.PyUnitTest('test_pass')
self.test = ITestCase(self.original)
def test_callable(self):
"""
Tests must be callable in order to be used with Python's unittest.py.
"""
self.assertTrue(callable(self.test),
"%r is not callable." % (self.test,))
# Remove this when we port twisted.trial._synctest to Python 3:
if _PY3:
del TestPyUnitTestCase
class TestPyUnitResult(SynchronousTestCase):
"""
Tests to show that PyUnitResultAdapter wraps TestResult objects from the
standard library 'unittest' module in such a way as to make them usable and
useful from Trial.
"""
# Once erroneous is ported to Python 3 this can be replaced with
# erroneous.ErrorTest:
class ErrorTest(SynchronousTestCase):
"""
A test case which has a L{test_foo} which will raise an error.
@ivar ran: boolean indicating whether L{test_foo} has been run.
"""
ran = False
def test_foo(self):
"""
Set C{self.ran} to True and raise a C{ZeroDivisionError}
"""
self.ran = True
1/0
def test_dontUseAdapterWhenReporterProvidesIReporter(self):
"""
The L{PyUnitResultAdapter} is only used when the result passed to
C{run} does *not* provide L{IReporter}.
"""
@implementer(IReporter)
class StubReporter(object):
"""
A reporter which records data about calls made to it.
@ivar errors: Errors passed to L{addError}.
@ivar failures: Failures passed to L{addFailure}.
"""
def __init__(self):
self.errors = []
self.failures = []
def startTest(self, test):
"""
Do nothing.
"""
def stopTest(self, test):
"""
Do nothing.
"""
def addError(self, test, error):
"""
Record the error.
"""
self.errors.append(error)
test = self.ErrorTest("test_foo")
result = StubReporter()
test.run(result)
self.assertIsInstance(result.errors[0], Failure)
def test_success(self):
class SuccessTest(SynchronousTestCase):
ran = False
def test_foo(s):
s.ran = True
test = SuccessTest('test_foo')
result = pyunit.TestResult()
test.run(result)
self.failUnless(test.ran)
self.assertEqual(1, result.testsRun)
self.failUnless(result.wasSuccessful())
def test_failure(self):
class FailureTest(SynchronousTestCase):
ran = False
def test_foo(s):
s.ran = True
s.fail('boom!')
test = FailureTest('test_foo')
result = pyunit.TestResult()
test.run(result)
self.failUnless(test.ran)
self.assertEqual(1, result.testsRun)
self.assertEqual(1, len(result.failures))
self.failIf(result.wasSuccessful())
def test_error(self):
test = self.ErrorTest('test_foo')
result = pyunit.TestResult()
test.run(result)
self.failUnless(test.ran)
self.assertEqual(1, result.testsRun)
self.assertEqual(1, len(result.errors))
self.failIf(result.wasSuccessful())
def test_setUpError(self):
class ErrorTest(SynchronousTestCase):
ran = False
def setUp(self):
1/0
def test_foo(s):
s.ran = True
test = ErrorTest('test_foo')
result = pyunit.TestResult()
test.run(result)
self.failIf(test.ran)
self.assertEqual(1, result.testsRun)
self.assertEqual(1, len(result.errors))
self.failIf(result.wasSuccessful())
def test_tracebackFromFailure(self):
"""
Errors added through the L{PyUnitResultAdapter} have the same traceback
information as if there were no adapter at all.
"""
try:
1/0
except ZeroDivisionError:
exc_info = sys.exc_info()
f = Failure()
pyresult = pyunit.TestResult()
result = PyUnitResultAdapter(pyresult)
result.addError(self, f)
self.assertEqual(pyresult.errors[0][1],
''.join(traceback.format_exception(*exc_info)))
def test_traceback(self):
"""
As test_tracebackFromFailure, but covering more code.
"""
class ErrorTest(SynchronousTestCase):
exc_info = None
def test_foo(self):
try:
1/0
except ZeroDivisionError:
self.exc_info = sys.exc_info()
raise
test = ErrorTest('test_foo')
result = pyunit.TestResult()
test.run(result)
# We can't test that the tracebacks are equal, because Trial's
# machinery inserts a few extra frames on the top and we don't really
# want to trim them off without an extremely good reason.
#
# So, we just test that the result's stack ends with the the
# exception's stack.
expected_stack = ''.join(traceback.format_tb(test.exc_info[2]))
observed_stack = '\n'.join(result.errors[0][1].splitlines()[:-1])
self.assertEqual(expected_stack.strip(),
observed_stack[-len(expected_stack):].strip())
def test_tracebackFromCleanFailure(self):
"""
Errors added through the L{PyUnitResultAdapter} have the same
traceback information as if there were no adapter at all, even
if the Failure that held the information has been cleaned.
"""
try:
1/0
except ZeroDivisionError:
exc_info = sys.exc_info()
f = Failure()
f.cleanFailure()
pyresult = pyunit.TestResult()
result = PyUnitResultAdapter(pyresult)
result.addError(self, f)
self.assertEqual(pyresult.errors[0][1],
''.join(traceback.format_exception(*exc_info)))
def test_trialSkip(self):
"""
Skips using trial's skipping functionality are reported as skips in
the L{pyunit.TestResult}.
"""
class SkipTest(SynchronousTestCase):
def test_skip(self):
1/0
test_skip.skip = "Let's skip!"
test = SkipTest('test_skip')
result = pyunit.TestResult()
test.run(result)
self.assertEqual(result.skipped, [(test, "Let's skip!")])
def test_pyunitSkip(self):
"""
Skips using pyunit's skipping functionality are reported as skips in
the L{pyunit.TestResult}.
"""
class SkipTest(SynchronousTestCase):
@pyunit.skip("skippy")
def test_skip(self):
1/0
test = SkipTest('test_skip')
result = pyunit.TestResult()
test.run(result)
self.assertEqual(result.skipped, [(test, "skippy")])
def test_skip26(self):
"""
On Python 2.6, pyunit doesn't support skipping, so it gets added as a
failure to the L{pyunit.TestResult}.
"""
class SkipTest(SynchronousTestCase):
def test_skip(self):
1/0
test_skip.skip = "Let's skip!"
test = SkipTest('test_skip')
result = pyunit.TestResult()
test.run(result)
self.assertEqual(len(result.failures), 1)
test2, reason = result.failures[0]
self.assertIdentical(test, test2)
self.assertIn("UnsupportedTrialFeature", reason)
if sys.version_info[:2] < (2, 7):
message = "pyunit doesn't support skipping in Python 2.6"
test_trialSkip.skip = message
test_pyunitSkip.skip = message
del message
else:
test_skip26.skip = "This test is only relevant to Python 2.6"

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,867 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
import StringIO
import gc
import re
import sys
import textwrap
import types
from twisted.trial import unittest
from twisted.trial.runner import (
TrialRunner, TestSuite, DestructiveTestSuite, TestLoader)
from twisted.trial._dist.disttrial import DistTrialRunner
from twisted.scripts import trial
from twisted.python import util
from twisted.python.usage import UsageError
from twisted.python.filepath import FilePath
from twisted.trial.test.test_loader import testNames
pyunit = __import__('unittest')
def sibpath(filename):
"""
For finding files in twisted/trial/test
"""
return util.sibpath(__file__, filename)
class ForceGarbageCollection(unittest.SynchronousTestCase):
"""
Tests for the --force-gc option.
"""
def setUp(self):
self.config = trial.Options()
self.log = []
self.patch(gc, 'collect', self.collect)
test = pyunit.FunctionTestCase(self.simpleTest)
self.test = TestSuite([test, test])
def simpleTest(self):
"""
A simple test method that records that it was run.
"""
self.log.append('test')
def collect(self):
"""
A replacement for gc.collect that logs calls to itself.
"""
self.log.append('collect')
def makeRunner(self):
"""
Return a L{TrialRunner} object that is safe to use in tests.
"""
runner = trial._makeRunner(self.config)
runner.stream = StringIO.StringIO()
return runner
def test_forceGc(self):
"""
Passing the --force-gc option to the trial script forces the garbage
collector to run before and after each test.
"""
self.config['force-gc'] = True
self.config.postOptions()
runner = self.makeRunner()
runner.run(self.test)
self.assertEqual(self.log, ['collect', 'test', 'collect',
'collect', 'test', 'collect'])
def test_unforceGc(self):
"""
By default, no garbage collection is forced.
"""
self.config.postOptions()
runner = self.makeRunner()
runner.run(self.test)
self.assertEqual(self.log, ['test', 'test'])
class TestSuiteUsed(unittest.SynchronousTestCase):
"""
Check the category of tests suite used by the loader.
"""
def setUp(self):
"""
Create a trial configuration object.
"""
self.config = trial.Options()
def test_defaultSuite(self):
"""
By default, the loader should use L{DestructiveTestSuite}
"""
loader = trial._getLoader(self.config)
self.assertEqual(loader.suiteFactory, DestructiveTestSuite)
def test_untilFailureSuite(self):
"""
The C{until-failure} configuration uses the L{TestSuite} to keep
instances alive across runs.
"""
self.config['until-failure'] = True
loader = trial._getLoader(self.config)
self.assertEqual(loader.suiteFactory, TestSuite)
class TestModuleTest(unittest.SynchronousTestCase):
def setUp(self):
self.config = trial.Options()
def tearDown(self):
self.config = None
def test_testNames(self):
"""
Check that the testNames helper method accurately collects the
names of tests in suite.
"""
self.assertEqual(testNames(self), [self.id()])
def assertSuitesEqual(self, test1, names):
loader = TestLoader()
names1 = testNames(test1)
names2 = testNames(TestSuite(map(loader.loadByName, names)))
names1.sort()
names2.sort()
self.assertEqual(names1, names2)
def test_baseState(self):
self.assertEqual(0, len(self.config['tests']))
def test_testmoduleOnModule(self):
"""
Check that --testmodule loads a suite which contains the tests
referred to in test-case-name inside its parameter.
"""
self.config.opt_testmodule(sibpath('moduletest.py'))
self.assertSuitesEqual(trial._getSuite(self.config),
['twisted.trial.test.test_log'])
def test_testmoduleTwice(self):
"""
When the same module is specified with two --testmodule flags, it
should only appear once in the suite.
"""
self.config.opt_testmodule(sibpath('moduletest.py'))
self.config.opt_testmodule(sibpath('moduletest.py'))
self.assertSuitesEqual(trial._getSuite(self.config),
['twisted.trial.test.test_log'])
def test_testmoduleOnSourceAndTarget(self):
"""
If --testmodule is specified twice, once for module A and once for
a module which refers to module A, then make sure module A is only
added once.
"""
self.config.opt_testmodule(sibpath('moduletest.py'))
self.config.opt_testmodule(sibpath('test_log.py'))
self.assertSuitesEqual(trial._getSuite(self.config),
['twisted.trial.test.test_log'])
def test_testmoduleOnSelfModule(self):
"""
When given a module that refers to *itself* in the test-case-name
variable, check that --testmodule only adds the tests once.
"""
self.config.opt_testmodule(sibpath('moduleself.py'))
self.assertSuitesEqual(trial._getSuite(self.config),
['twisted.trial.test.moduleself'])
def test_testmoduleOnScript(self):
"""
Check that --testmodule loads tests referred to in test-case-name
buffer variables.
"""
self.config.opt_testmodule(sibpath('scripttest.py'))
self.assertSuitesEqual(trial._getSuite(self.config),
['twisted.trial.test.test_log',
'twisted.trial.test.test_class'])
def test_testmoduleOnNonexistentFile(self):
"""
Check that --testmodule displays a meaningful error message when
passed a non-existent filename.
"""
buffy = StringIO.StringIO()
stderr, sys.stderr = sys.stderr, buffy
filename = 'test_thisbetternoteverexist.py'
try:
self.config.opt_testmodule(filename)
self.assertEqual(0, len(self.config['tests']))
self.assertEqual("File %r doesn't exist\n" % (filename,),
buffy.getvalue())
finally:
sys.stderr = stderr
def test_testmoduleOnEmptyVars(self):
"""
Check that --testmodule adds no tests to the suite for modules
which lack test-case-name buffer variables.
"""
self.config.opt_testmodule(sibpath('novars.py'))
self.assertEqual(0, len(self.config['tests']))
def test_testmoduleOnModuleName(self):
"""
Check that --testmodule does *not* support module names as arguments
and that it displays a meaningful error message.
"""
buffy = StringIO.StringIO()
stderr, sys.stderr = sys.stderr, buffy
moduleName = 'twisted.trial.test.test_script'
try:
self.config.opt_testmodule(moduleName)
self.assertEqual(0, len(self.config['tests']))
self.assertEqual("File %r doesn't exist\n" % (moduleName,),
buffy.getvalue())
finally:
sys.stderr = stderr
def test_parseLocalVariable(self):
declaration = '-*- test-case-name: twisted.trial.test.test_tests -*-'
localVars = trial._parseLocalVariables(declaration)
self.assertEqual({'test-case-name':
'twisted.trial.test.test_tests'},
localVars)
def test_trailingSemicolon(self):
declaration = '-*- test-case-name: twisted.trial.test.test_tests; -*-'
localVars = trial._parseLocalVariables(declaration)
self.assertEqual({'test-case-name':
'twisted.trial.test.test_tests'},
localVars)
def test_parseLocalVariables(self):
declaration = ('-*- test-case-name: twisted.trial.test.test_tests; '
'foo: bar -*-')
localVars = trial._parseLocalVariables(declaration)
self.assertEqual({'test-case-name':
'twisted.trial.test.test_tests',
'foo': 'bar'},
localVars)
def test_surroundingGuff(self):
declaration = ('## -*- test-case-name: '
'twisted.trial.test.test_tests -*- #')
localVars = trial._parseLocalVariables(declaration)
self.assertEqual({'test-case-name':
'twisted.trial.test.test_tests'},
localVars)
def test_invalidLine(self):
self.failUnlessRaises(ValueError, trial._parseLocalVariables,
'foo')
def test_invalidDeclaration(self):
self.failUnlessRaises(ValueError, trial._parseLocalVariables,
'-*- foo -*-')
self.failUnlessRaises(ValueError, trial._parseLocalVariables,
'-*- foo: bar; qux -*-')
self.failUnlessRaises(ValueError, trial._parseLocalVariables,
'-*- foo: bar: baz; qux: qax -*-')
def test_variablesFromFile(self):
localVars = trial.loadLocalVariables(sibpath('moduletest.py'))
self.assertEqual({'test-case-name':
'twisted.trial.test.test_log'},
localVars)
def test_noVariablesInFile(self):
localVars = trial.loadLocalVariables(sibpath('novars.py'))
self.assertEqual({}, localVars)
def test_variablesFromScript(self):
localVars = trial.loadLocalVariables(sibpath('scripttest.py'))
self.assertEqual(
{'test-case-name': ('twisted.trial.test.test_log,'
'twisted.trial.test.test_class')},
localVars)
def test_getTestModules(self):
modules = trial.getTestModules(sibpath('moduletest.py'))
self.assertEqual(modules, ['twisted.trial.test.test_log'])
def test_getTestModules_noVars(self):
modules = trial.getTestModules(sibpath('novars.py'))
self.assertEqual(len(modules), 0)
def test_getTestModules_multiple(self):
modules = trial.getTestModules(sibpath('scripttest.py'))
self.assertEqual(set(modules),
set(['twisted.trial.test.test_log',
'twisted.trial.test.test_class']))
def test_looksLikeTestModule(self):
for filename in ['test_script.py', 'twisted/trial/test/test_script.py']:
self.failUnless(trial.isTestFile(filename),
"%r should be a test file" % (filename,))
for filename in ['twisted/trial/test/moduletest.py',
sibpath('scripttest.py'), sibpath('test_foo.bat')]:
self.failIf(trial.isTestFile(filename),
"%r should *not* be a test file" % (filename,))
class WithoutModuleTests(unittest.SynchronousTestCase):
"""
Test the C{without-module} flag.
"""
def setUp(self):
"""
Create a L{trial.Options} object to be used in the tests, and save
C{sys.modules}.
"""
self.config = trial.Options()
self.savedModules = dict(sys.modules)
def tearDown(self):
"""
Restore C{sys.modules}.
"""
for module in ('imaplib', 'smtplib'):
if module in self.savedModules:
sys.modules[module] = self.savedModules[module]
else:
sys.modules.pop(module, None)
def _checkSMTP(self):
"""
Try to import the C{smtplib} module, and return it.
"""
import smtplib
return smtplib
def _checkIMAP(self):
"""
Try to import the C{imaplib} module, and return it.
"""
import imaplib
return imaplib
def test_disableOneModule(self):
"""
Check that after disabling a module, it can't be imported anymore.
"""
self.config.parseOptions(["--without-module", "smtplib"])
self.assertRaises(ImportError, self._checkSMTP)
# Restore sys.modules
del sys.modules["smtplib"]
# Then the function should succeed
self.assertIsInstance(self._checkSMTP(), types.ModuleType)
def test_disableMultipleModules(self):
"""
Check that several modules can be disabled at once.
"""
self.config.parseOptions(["--without-module", "smtplib,imaplib"])
self.assertRaises(ImportError, self._checkSMTP)
self.assertRaises(ImportError, self._checkIMAP)
# Restore sys.modules
del sys.modules["smtplib"]
del sys.modules["imaplib"]
# Then the functions should succeed
self.assertIsInstance(self._checkSMTP(), types.ModuleType)
self.assertIsInstance(self._checkIMAP(), types.ModuleType)
def test_disableAlreadyImportedModule(self):
"""
Disabling an already imported module should produce a warning.
"""
self.assertIsInstance(self._checkSMTP(), types.ModuleType)
self.assertWarns(RuntimeWarning,
"Module 'smtplib' already imported, disabling anyway.",
trial.__file__,
self.config.parseOptions, ["--without-module", "smtplib"])
self.assertRaises(ImportError, self._checkSMTP)
class CoverageTests(unittest.SynchronousTestCase):
"""
Tests for the I{coverage} option.
"""
if getattr(sys, 'gettrace', None) is None:
skip = (
"Cannot test trace hook installation without inspection API.")
def setUp(self):
"""
Arrange for the current trace hook to be restored when the
test is complete.
"""
self.addCleanup(sys.settrace, sys.gettrace())
def test_tracerInstalled(self):
"""
L{trial.Options} handles C{"--coverage"} by installing a trace
hook to record coverage information.
"""
options = trial.Options()
options.parseOptions(["--coverage"])
self.assertEqual(sys.gettrace(), options.tracer.globaltrace)
def test_coverdirDefault(self):
"""
L{trial.Options.coverdir} returns a L{FilePath} based on the default
for the I{temp-directory} option if that option is not specified.
"""
options = trial.Options()
self.assertEqual(
options.coverdir(),
FilePath(".").descendant([options["temp-directory"], "coverage"]))
def test_coverdirOverridden(self):
"""
If a value is specified for the I{temp-directory} option,
L{trial.Options.coverdir} returns a child of that path.
"""
path = self.mktemp()
options = trial.Options()
options.parseOptions(["--temp-directory", path])
self.assertEqual(
options.coverdir(), FilePath(path).child("coverage"))
class OptionsTestCase(unittest.TestCase):
"""
Tests for L{trial.Options}.
"""
def setUp(self):
"""
Build an L{Options} object to be used in the tests.
"""
self.options = trial.Options()
def test_getWorkerArguments(self):
"""
C{_getWorkerArguments} discards options like C{random} as they only
matter in the manager, and forwards options like C{recursionlimit} or
C{disablegc}.
"""
self.addCleanup(sys.setrecursionlimit, sys.getrecursionlimit())
if gc.isenabled():
self.addCleanup(gc.enable)
self.options.parseOptions(["--recursionlimit", "2000", "--random",
"4", "--disablegc"])
args = self.options._getWorkerArguments()
self.assertIn("--disablegc", args)
args.remove("--disablegc")
self.assertEqual(["--recursionlimit", "2000"], args)
def test_jobsConflictWithDebug(self):
"""
C{parseOptions} raises a C{UsageError} when C{--debug} is passed along
C{--jobs} as it's not supported yet.
@see: U{http://twistedmatrix.com/trac/ticket/5825}
"""
error = self.assertRaises(
UsageError, self.options.parseOptions, ["--jobs", "4", "--debug"])
self.assertEqual("You can't specify --debug when using --jobs",
str(error))
def test_jobsConflictWithProfile(self):
"""
C{parseOptions} raises a C{UsageError} when C{--profile} is passed
along C{--jobs} as it's not supported yet.
@see: U{http://twistedmatrix.com/trac/ticket/5827}
"""
error = self.assertRaises(
UsageError, self.options.parseOptions,
["--jobs", "4", "--profile"])
self.assertEqual("You can't specify --profile when using --jobs",
str(error))
def test_jobsConflictWithDebugStackTraces(self):
"""
C{parseOptions} raises a C{UsageError} when C{--debug-stacktraces} is
passed along C{--jobs} as it's not supported yet.
@see: U{http://twistedmatrix.com/trac/ticket/5826}
"""
error = self.assertRaises(
UsageError, self.options.parseOptions,
["--jobs", "4", "--debug-stacktraces"])
self.assertEqual(
"You can't specify --debug-stacktraces when using --jobs",
str(error))
def test_jobsConflictWithExitFirst(self):
"""
C{parseOptions} raises a C{UsageError} when C{--exitfirst} is passed
along C{--jobs} as it's not supported yet.
@see: U{http://twistedmatrix.com/trac/ticket/6436}
"""
error = self.assertRaises(
UsageError, self.options.parseOptions,
["--jobs", "4", "--exitfirst"])
self.assertEqual(
"You can't specify --exitfirst when using --jobs",
str(error))
def test_orderConflictWithRandom(self):
"""
C{parseOptions} raises a C{UsageError} when C{--order} is passed along
with C{--random}.
"""
error = self.assertRaises(
UsageError,
self.options.parseOptions,
["--order", "alphabetical", "--random", "1234"])
self.assertEqual("You can't specify --random when using --order",
str(error))
class MakeRunnerTestCase(unittest.TestCase):
"""
Tests for the L{_makeRunner} helper.
"""
def setUp(self):
self.options = trial.Options()
def test_jobs(self):
"""
L{_makeRunner} returns a L{DistTrialRunner} instance when the C{--jobs}
option is passed, and passes the C{workerNumber} and C{workerArguments}
parameters to it.
"""
self.options.parseOptions(["--jobs", "4", "--force-gc"])
runner = trial._makeRunner(self.options)
self.assertIsInstance(runner, DistTrialRunner)
self.assertEqual(4, runner._workerNumber)
self.assertEqual(["--force-gc"], runner._workerArguments)
def test_dryRunWithJobs(self):
"""
L{_makeRunner} returns a L{TrialRunner} instance in C{DRY_RUN} mode
when the C{--dry-run} option is passed, even if C{--jobs} is set.
"""
self.options.parseOptions(["--jobs", "4", "--dry-run"])
runner = trial._makeRunner(self.options)
self.assertIsInstance(runner, TrialRunner)
self.assertEqual(TrialRunner.DRY_RUN, runner.mode)
def test_DebuggerNotFound(self):
namedAny = trial.reflect.namedAny
def namedAnyExceptdoNotFind(fqn):
if fqn == "doNotFind":
raise trial.reflect.ModuleNotFound(fqn)
return namedAny(fqn)
self.patch(trial.reflect, "namedAny", namedAnyExceptdoNotFind)
options = trial.Options()
options.parseOptions(["--debug", "--debugger", "doNotFind"])
self.assertRaises(trial._DebuggerNotFound, trial._makeRunner, options)
def test_exitfirst(self):
"""
Passing C{--exitfirst} wraps the reporter with a
L{reporter._ExitWrapper} that stops on any non-success.
"""
self.options.parseOptions(["--exitfirst"])
runner = trial._makeRunner(self.options)
self.assertTrue(runner._exitFirst)
class TestRun(unittest.TestCase):
"""
Tests for the L{run} function.
"""
def setUp(self):
# don't re-parse cmdline options, because if --reactor was passed to
# the test run trial will try to restart the (already running) reactor
self.patch(trial.Options, "parseOptions", lambda self: None)
def test_debuggerNotFound(self):
"""
When a debugger is not found, an error message is printed to the user.
"""
def _makeRunner(*args, **kwargs):
raise trial._DebuggerNotFound('foo')
self.patch(trial, "_makeRunner", _makeRunner)
try:
trial.run()
except SystemExit as e:
self.assertIn("foo", str(e))
else:
self.fail("Should have exited due to non-existent debugger!")
class TestArgumentOrderTests(unittest.TestCase):
"""
Tests for the order-preserving behavior on provided command-line tests.
"""
def setUp(self):
self.config = trial.Options()
self.loader = TestLoader()
def test_preserveArgumentOrder(self):
"""
Multiple tests passed on the command line are not reordered.
"""
tests = [
"twisted.trial.test.test_tests",
"twisted.trial.test.test_assertions",
"twisted.trial.test.test_deferreds",
]
self.config.parseOptions(tests)
suite = trial._getSuite(self.config)
names = testNames(suite)
expectedSuite = TestSuite(map(self.loader.loadByName, tests))
expectedNames = testNames(expectedSuite)
self.assertEqual(names, expectedNames)
class OrderTests(unittest.TestCase):
"""
Tests for the --order option.
"""
def setUp(self):
self.config = trial.Options()
def test_alphabetical(self):
"""
--order=alphabetical causes trial to run tests alphabetically within
each test case.
"""
self.config.parseOptions([
"--order", "alphabetical",
"twisted.trial.test.ordertests.FooTest"])
loader = trial._getLoader(self.config)
suite = loader.loadByNames(self.config['tests'])
self.assertEqual(
testNames(suite), [
'twisted.trial.test.ordertests.FooTest.test_first',
'twisted.trial.test.ordertests.FooTest.test_fourth',
'twisted.trial.test.ordertests.FooTest.test_second',
'twisted.trial.test.ordertests.FooTest.test_third'])
def test_alphabeticalModule(self):
"""
--order=alphabetical causes trial to run test classes within a given
module alphabetically.
"""
self.config.parseOptions([
"--order", "alphabetical", "twisted.trial.test.ordertests"])
loader = trial._getLoader(self.config)
suite = loader.loadByNames(self.config['tests'])
self.assertEqual(
testNames(suite), [
'twisted.trial.test.ordertests.BarTest.test_bar',
'twisted.trial.test.ordertests.BazTest.test_baz',
'twisted.trial.test.ordertests.FooTest.test_first',
'twisted.trial.test.ordertests.FooTest.test_fourth',
'twisted.trial.test.ordertests.FooTest.test_second',
'twisted.trial.test.ordertests.FooTest.test_third'])
def test_alphabeticalPackage(self):
"""
--order=alphabetical causes trial to run test modules within a given
package alphabetically, with tests within each module alphabetized.
"""
self.config.parseOptions([
"--order", "alphabetical", "twisted.trial.test"])
loader = trial._getLoader(self.config)
suite = loader.loadByNames(self.config['tests'])
names = testNames(suite)
self.assertTrue(names, msg="Failed to load any tests!")
self.assertEqual(names, sorted(names))
def test_toptobottom(self):
"""
--order=toptobottom causes trial to run test methods within a given
test case from top to bottom as they are defined in the body of the
class.
"""
self.config.parseOptions([
"--order", "toptobottom",
"twisted.trial.test.ordertests.FooTest"])
loader = trial._getLoader(self.config)
suite = loader.loadByNames(self.config['tests'])
self.assertEqual(
testNames(suite), [
'twisted.trial.test.ordertests.FooTest.test_first',
'twisted.trial.test.ordertests.FooTest.test_second',
'twisted.trial.test.ordertests.FooTest.test_third',
'twisted.trial.test.ordertests.FooTest.test_fourth'])
def test_toptobottomModule(self):
"""
--order=toptobottom causes trial to run test classes within a given
module from top to bottom as they are defined in the module's source.
"""
self.config.parseOptions([
"--order", "toptobottom", "twisted.trial.test.ordertests"])
loader = trial._getLoader(self.config)
suite = loader.loadByNames(self.config['tests'])
self.assertEqual(
testNames(suite), [
'twisted.trial.test.ordertests.FooTest.test_first',
'twisted.trial.test.ordertests.FooTest.test_second',
'twisted.trial.test.ordertests.FooTest.test_third',
'twisted.trial.test.ordertests.FooTest.test_fourth',
'twisted.trial.test.ordertests.BazTest.test_baz',
'twisted.trial.test.ordertests.BarTest.test_bar'])
def test_toptobottomPackage(self):
"""
--order=toptobottom causes trial to run test modules within a given
package alphabetically, with tests within each module run top to
bottom.
"""
self.config.parseOptions([
"--order", "toptobottom", "twisted.trial.test"])
loader = trial._getLoader(self.config)
suite = loader.loadByNames(self.config['tests'])
names = testNames(suite)
# twisted.trial.test.test_module, so split and key on the first 4 to
# get stable alphabetical sort on those
self.assertEqual(
names, sorted(names, key=lambda name : name.split(".")[:4]),
)
def test_toptobottomMissingSource(self):
"""
--order=toptobottom detects the source line of methods from modules
whose source file is missing.
"""
tempdir = self.mktemp().encode('utf-8')
package = FilePath(tempdir).child(b'twisted_toptobottom_temp')
package.makedirs()
package.child(b'__init__.py').setContent(b'')
package.child(b'test_missing.py').setContent(textwrap.dedent(b'''
from twisted.trial.unittest import TestCase
class TestMissing(TestCase):
def test_second(self): pass
def test_third(self): pass
def test_fourth(self): pass
def test_first(self): pass
'''))
pathEntry = package.parent().path.decode('utf-8')
sys.path.insert(0, pathEntry)
self.addCleanup(sys.path.remove, pathEntry)
from twisted_toptobottom_temp import test_missing
self.addCleanup(sys.modules.pop, 'twisted_toptobottom_temp')
self.addCleanup(sys.modules.pop, test_missing.__name__)
package.child(b'test_missing.py').remove()
self.config.parseOptions([
"--order", "toptobottom", "twisted.trial.test.ordertests"])
loader = trial._getLoader(self.config)
suite = loader.loadModule(test_missing)
self.assertEqual(
testNames(suite), [
'twisted_toptobottom_temp.test_missing.TestMissing.test_second',
'twisted_toptobottom_temp.test_missing.TestMissing.test_third',
'twisted_toptobottom_temp.test_missing.TestMissing.test_fourth',
'twisted_toptobottom_temp.test_missing.TestMissing.test_first'])
def test_unknownOrder(self):
"""
An unknown order passed to --order raises a L{UsageError}.
"""
self.assertRaises(
UsageError, self.config.parseOptions, ["--order", "I don't exist"])
class HelpOrderTests(unittest.TestCase):
"""
Tests for the --help-orders flag.
"""
def test_help_ordersPrintsSynopsisAndQuits(self):
"""
--help-orders prints each of the available orders and then exits.
"""
self.patch(sys, "stdout", StringIO.StringIO())
exc = self.assertRaises(
SystemExit, trial.Options().parseOptions, ["--help-orders"])
self.assertEqual(exc.code, 0)
output = sys.stdout.getvalue()
msg = "%r with its description not properly described in %r"
for orderName, (orderDesc, _) in trial._runOrders.items():
match = re.search(
"%s.*%s" % (re.escape(orderName), re.escape(orderDesc)),
output,
)
self.assertTrue(match, msg=msg % (orderName, output))

View file

@ -0,0 +1,162 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for warning suppression features of Trial.
"""
from __future__ import division, absolute_import
import unittest as pyunit
from twisted.trial import unittest
from twisted.trial.test import suppression
class SuppressionMixin(object):
"""
Tests for the warning suppression features of
L{twisted.trial.unittest.SynchronousTestCase}.
"""
def runTests(self, suite):
suite.run(pyunit.TestResult())
def _load(self, cls, methodName):
"""
Return a new L{unittest.TestSuite} with a single test method in it.
@param cls: A L{TestCase} subclass defining a test method.
@param methodName: The name of the test method from C{cls}.
"""
return pyunit.TestSuite([cls(methodName)])
def _assertWarnings(self, warnings, which):
"""
Assert that a certain number of warnings with certain messages were
emitted in a certain order.
@param warnings: A list of emitted warnings, as returned by
C{flushWarnings}.
@param which: A list of strings giving warning messages that should
appear in C{warnings}.
@raise self.failureException: If the warning messages given by C{which}
do not match the messages in the warning information in C{warnings},
or if they do not appear in the same order.
"""
self.assertEqual(
[warning['message'] for warning in warnings],
which)
def test_setUpSuppression(self):
"""
Suppressions defined by the test method being run are applied to any
warnings emitted while running the C{setUp} fixture.
"""
self.runTests(
self._load(self.TestSetUpSuppression, "testSuppressMethod"))
warningsShown = self.flushWarnings([
self.TestSetUpSuppression._emit])
self._assertWarnings(
warningsShown,
[suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG,
suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG])
def test_tearDownSuppression(self):
"""
Suppressions defined by the test method being run are applied to any
warnings emitted while running the C{tearDown} fixture.
"""
self.runTests(
self._load(self.TestTearDownSuppression, "testSuppressMethod"))
warningsShown = self.flushWarnings([
self.TestTearDownSuppression._emit])
self._assertWarnings(
warningsShown,
[suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG,
suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG])
def test_suppressMethod(self):
"""
A suppression set on a test method prevents warnings emitted by that
test method which the suppression matches from being emitted.
"""
self.runTests(
self._load(self.TestSuppression, "testSuppressMethod"))
warningsShown = self.flushWarnings([
self.TestSuppression._emit])
self._assertWarnings(
warningsShown,
[suppression.CLASS_WARNING_MSG, suppression.MODULE_WARNING_MSG])
def test_suppressClass(self):
"""
A suppression set on a L{SynchronousTestCase} subclass prevents warnings
emitted by any test methods defined on that class which match the
suppression from being emitted.
"""
self.runTests(
self._load(self.TestSuppression, "testSuppressClass"))
warningsShown = self.flushWarnings([
self.TestSuppression._emit])
self.assertEqual(
warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
self.assertEqual(
warningsShown[1]['message'], suppression.MODULE_WARNING_MSG)
self.assertEqual(len(warningsShown), 2)
def test_suppressModule(self):
"""
A suppression set on a module prevents warnings emitted by any test
mewthods defined in that module which match the suppression from being
emitted.
"""
self.runTests(
self._load(self.TestSuppression2, "testSuppressModule"))
warningsShown = self.flushWarnings([
self.TestSuppression._emit])
self.assertEqual(
warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
self.assertEqual(
warningsShown[1]['message'], suppression.CLASS_WARNING_MSG)
self.assertEqual(len(warningsShown), 2)
def test_overrideSuppressClass(self):
"""
The suppression set on a test method completely overrides a suppression
with wider scope; if it does not match a warning emitted by that test
method, the warning is emitted, even if a wider suppression matches.
"""
self.runTests(
self._load(self.TestSuppression, "testOverrideSuppressClass"))
warningsShown = self.flushWarnings([
self.TestSuppression._emit])
self.assertEqual(
warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
self.assertEqual(
warningsShown[1]['message'], suppression.CLASS_WARNING_MSG)
self.assertEqual(
warningsShown[2]['message'], suppression.MODULE_WARNING_MSG)
self.assertEqual(len(warningsShown), 3)
class SynchronousSuppressionTest(SuppressionMixin, unittest.SynchronousTestCase):
"""
@see: L{twisted.trial.test.test_tests}
"""
from twisted.trial.test.suppression import (
SynchronousTestSetUpSuppression as TestSetUpSuppression,
SynchronousTestTearDownSuppression as TestTearDownSuppression,
SynchronousTestSuppression as TestSuppression,
SynchronousTestSuppression2 as TestSuppression2)

View file

@ -0,0 +1,70 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Direct unit tests for L{twisted.trial.unittest.SynchronousTestCase} and
L{twisted.trial.unittest.TestCase}.
"""
from __future__ import division, absolute_import
from twisted.trial.unittest import SynchronousTestCase, TestCase
class TestCaseMixin(object):
"""
L{TestCase} tests.
"""
def setUp(self):
"""
Create a couple instances of C{MyTestCase}, each for the same test
method, to be used in the test methods of this class.
"""
self.first = self.MyTestCase('test_1')
self.second = self.MyTestCase('test_1')
def test_equality(self):
"""
In order for one test method to be runnable twice, two TestCase
instances with the same test method name must not compare as equal.
"""
self.assertTrue(self.first == self.first)
self.assertTrue(self.first != self.second)
self.assertFalse(self.first == self.second)
def test_hashability(self):
"""
In order for one test method to be runnable twice, two TestCase
instances with the same test method name should not have the same
hash value.
"""
container = {}
container[self.first] = None
container[self.second] = None
self.assertEqual(len(container), 2)
class SynchronousTestCaseTests(TestCaseMixin, SynchronousTestCase):
class MyTestCase(SynchronousTestCase):
"""
Some test methods which can be used to test behaviors of
L{SynchronousTestCase}.
"""
def test_1(self):
pass
# Yes, subclass SynchronousTestCase again. There are no interesting behaviors
# of self being tested below, only of self.MyTestCase.
class AsynchronousTestCaseTests(TestCaseMixin, SynchronousTestCase):
class MyTestCase(TestCase):
"""
Some test methods which can be used to test behaviors of
L{TestCase}.
"""
def test_1(self):
pass

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,780 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
#
"""
Tests for L{twisted.trial.util}
"""
from __future__ import division, absolute_import
import os, sys
from zope.interface import implementer
from twisted.python.compat import _PY3, NativeStringIO
from twisted.python import filepath
from twisted.internet.interfaces import IProcessTransport
from twisted.internet import defer
from twisted.internet.base import DelayedCall
from twisted.python.failure import Failure
from twisted.trial.unittest import SynchronousTestCase
from twisted.trial import util
from twisted.trial.util import (
DirtyReactorAggregateError, _Janitor, excInfoOrFailureToExcInfo,
acquireAttribute)
from twisted.trial.test import suppression
class TestMktemp(SynchronousTestCase):
"""
Tests for L{TestCase.mktemp}, a helper function for creating temporary file
or directory names.
"""
def test_name(self):
"""
The path name returned by C{mktemp} is directly beneath a directory
which identifies the test method which created the name.
"""
name = self.mktemp()
dirs = os.path.dirname(name).split(os.sep)[:-1]
self.assertEqual(
dirs, ['twisted.trial.test.test_util', 'TestMktemp', 'test_name'])
def test_unique(self):
"""
Repeated calls to C{mktemp} return different values.
"""
name = self.mktemp()
self.assertNotEqual(name, self.mktemp())
def test_created(self):
"""
The directory part of the path name returned by C{mktemp} exists.
"""
name = self.mktemp()
dirname = os.path.dirname(name)
self.assertTrue(os.path.exists(dirname))
self.assertFalse(os.path.exists(name))
def test_location(self):
"""
The path returned by C{mktemp} is beneath the current working directory.
"""
path = os.path.abspath(self.mktemp())
self.assertTrue(path.startswith(os.getcwd()))
class TestIntrospection(SynchronousTestCase):
def test_containers(self):
"""
When pased a test case, L{util.getPythonContainers} returns a list
including the test case and the module the test case is defined in.
"""
parents = util.getPythonContainers(
suppression.SynchronousTestSuppression2.testSuppressModule)
expected = [suppression.SynchronousTestSuppression2, suppression]
for a, b in zip(parents, expected):
self.assertEqual(a, b)
# Also, the function is deprecated.
warnings = self.flushWarnings([self.test_containers])
self.assertEqual(DeprecationWarning, warnings[0]['category'])
self.assertEqual(
"twisted.trial.util.getPythonContainers was deprecated in "
"Twisted 12.3.0: This function never worked correctly. "
"Implement lookup on your own.",
warnings[0]['message'])
self.assertEqual(1, len(warnings))
if _PY3:
test_containers.skip = "getPythonContainers is unsupported on Python 3."
class TestRunSequentially(SynchronousTestCase):
"""
Sometimes it is useful to be able to run an arbitrary list of callables,
one after the other.
When some of those callables can return Deferreds, things become complex.
"""
def assertDeferredResult(self, deferred, assertFunction, *args, **kwargs):
"""
Call the given assertion function against the current result of a
Deferred.
"""
result = []
deferred.addCallback(result.append)
assertFunction(result[0], *args, **kwargs)
def test_emptyList(self):
"""
When asked to run an empty list of callables, runSequentially returns a
successful Deferred that fires an empty list.
"""
d = util._runSequentially([])
self.assertDeferredResult(d, self.assertEqual, [])
def test_singleSynchronousSuccess(self):
"""
When given a callable that succeeds without returning a Deferred,
include the return value in the results list, tagged with a SUCCESS
flag.
"""
d = util._runSequentially([lambda: None])
self.assertDeferredResult(d, self.assertEqual, [(defer.SUCCESS, None)])
def test_singleSynchronousFailure(self):
"""
When given a callable that raises an exception, include a Failure for
that exception in the results list, tagged with a FAILURE flag.
"""
d = util._runSequentially([lambda: self.fail('foo')])
def check(results):
[(flag, fail)] = results
fail.trap(self.failureException)
self.assertEqual(fail.getErrorMessage(), 'foo')
self.assertEqual(flag, defer.FAILURE)
self.assertDeferredResult(d, check)
def test_singleAsynchronousSuccess(self):
"""
When given a callable that returns a successful Deferred, include the
result of the Deferred in the results list, tagged with a SUCCESS flag.
"""
d = util._runSequentially([lambda: defer.succeed(None)])
self.assertDeferredResult(d, self.assertEqual, [(defer.SUCCESS, None)])
def test_singleAsynchronousFailure(self):
"""
When given a callable that returns a failing Deferred, include the
failure the results list, tagged with a FAILURE flag.
"""
d = util._runSequentially([lambda: defer.fail(ValueError('foo'))])
def check(results):
[(flag, fail)] = results
fail.trap(ValueError)
self.assertEqual(fail.getErrorMessage(), 'foo')
self.assertEqual(flag, defer.FAILURE)
self.assertDeferredResult(d, check)
def test_callablesCalledInOrder(self):
"""
Check that the callables are called in the given order, one after the
other.
"""
log = []
deferreds = []
def append(value):
d = defer.Deferred()
log.append(value)
deferreds.append(d)
return d
util._runSequentially([lambda: append('foo'),
lambda: append('bar')])
# runSequentially should wait until the Deferred has fired before
# running the second callable.
self.assertEqual(log, ['foo'])
deferreds[-1].callback(None)
self.assertEqual(log, ['foo', 'bar'])
def test_continuesAfterError(self):
"""
If one of the callables raises an error, then runSequentially continues
to run the remaining callables.
"""
d = util._runSequentially([lambda: self.fail('foo'), lambda: 'bar'])
def check(results):
[(flag1, fail), (flag2, result)] = results
fail.trap(self.failureException)
self.assertEqual(flag1, defer.FAILURE)
self.assertEqual(fail.getErrorMessage(), 'foo')
self.assertEqual(flag2, defer.SUCCESS)
self.assertEqual(result, 'bar')
self.assertDeferredResult(d, check)
def test_stopOnFirstError(self):
"""
If the C{stopOnFirstError} option is passed to C{runSequentially}, then
no further callables are called after the first exception is raised.
"""
d = util._runSequentially([lambda: self.fail('foo'), lambda: 'bar'],
stopOnFirstError=True)
def check(results):
[(flag1, fail)] = results
fail.trap(self.failureException)
self.assertEqual(flag1, defer.FAILURE)
self.assertEqual(fail.getErrorMessage(), 'foo')
self.assertDeferredResult(d, check)
class DirtyReactorAggregateErrorTest(SynchronousTestCase):
"""
Tests for the L{DirtyReactorAggregateError}.
"""
def test_formatDelayedCall(self):
"""
Delayed calls are formatted nicely.
"""
error = DirtyReactorAggregateError(["Foo", "bar"])
self.assertEqual(str(error),
"""\
Reactor was unclean.
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
Foo
bar""")
def test_formatSelectables(self):
"""
Selectables are formatted nicely.
"""
error = DirtyReactorAggregateError([], ["selectable 1", "selectable 2"])
self.assertEqual(str(error),
"""\
Reactor was unclean.
Selectables:
selectable 1
selectable 2""")
def test_formatDelayedCallsAndSelectables(self):
"""
Both delayed calls and selectables can appear in the same error.
"""
error = DirtyReactorAggregateError(["bleck", "Boozo"],
["Sel1", "Sel2"])
self.assertEqual(str(error),
"""\
Reactor was unclean.
DelayedCalls: (set twisted.internet.base.DelayedCall.debug = True to debug)
bleck
Boozo
Selectables:
Sel1
Sel2""")
class StubReactor(object):
"""
A reactor stub which contains enough functionality to be used with the
L{_Janitor}.
@ivar iterations: A list of the arguments passed to L{iterate}.
@ivar removeAllCalled: Number of times that L{removeAll} was called.
@ivar selectables: The value that will be returned from L{removeAll}.
@ivar delayedCalls: The value to return from L{getDelayedCalls}.
"""
def __init__(self, delayedCalls, selectables=None):
"""
@param delayedCalls: See L{StubReactor.delayedCalls}.
@param selectables: See L{StubReactor.selectables}.
"""
self.delayedCalls = delayedCalls
self.iterations = []
self.removeAllCalled = 0
if not selectables:
selectables = []
self.selectables = selectables
def iterate(self, timeout=None):
"""
Increment C{self.iterations}.
"""
self.iterations.append(timeout)
def getDelayedCalls(self):
"""
Return C{self.delayedCalls}.
"""
return self.delayedCalls
def removeAll(self):
"""
Increment C{self.removeAllCalled} and return C{self.selectables}.
"""
self.removeAllCalled += 1
return self.selectables
class StubErrorReporter(object):
"""
A subset of L{twisted.trial.itrial.IReporter} which records L{addError}
calls.
@ivar errors: List of two-tuples of (test, error) which were passed to
L{addError}.
"""
def __init__(self):
self.errors = []
def addError(self, test, error):
"""
Record parameters in C{self.errors}.
"""
self.errors.append((test, error))
class JanitorTests(SynchronousTestCase):
"""
Tests for L{_Janitor}!
"""
def test_cleanPendingSpinsReactor(self):
"""
During pending-call cleanup, the reactor will be spun twice with an
instant timeout. This is not a requirement, it is only a test for
current behavior. Hopefully Trial will eventually not do this kind of
reactor stuff.
"""
reactor = StubReactor([])
jan = _Janitor(None, None, reactor=reactor)
jan._cleanPending()
self.assertEqual(reactor.iterations, [0, 0])
def test_cleanPendingCancelsCalls(self):
"""
During pending-call cleanup, the janitor cancels pending timed calls.
"""
def func():
return "Lulz"
cancelled = []
delayedCall = DelayedCall(300, func, (), {},
cancelled.append, lambda x: None)
reactor = StubReactor([delayedCall])
jan = _Janitor(None, None, reactor=reactor)
jan._cleanPending()
self.assertEqual(cancelled, [delayedCall])
def test_cleanPendingReturnsDelayedCallStrings(self):
"""
The Janitor produces string representations of delayed calls from the
delayed call cleanup method. It gets the string representations
*before* cancelling the calls; this is important because cancelling the
call removes critical debugging information from the string
representation.
"""
delayedCall = DelayedCall(300, lambda: None, (), {},
lambda x: None, lambda x: None,
seconds=lambda: 0)
delayedCallString = str(delayedCall)
reactor = StubReactor([delayedCall])
jan = _Janitor(None, None, reactor=reactor)
strings = jan._cleanPending()
self.assertEqual(strings, [delayedCallString])
def test_cleanReactorRemovesSelectables(self):
"""
The Janitor will remove selectables during reactor cleanup.
"""
reactor = StubReactor([])
jan = _Janitor(None, None, reactor=reactor)
jan._cleanReactor()
self.assertEqual(reactor.removeAllCalled, 1)
def test_cleanReactorKillsProcesses(self):
"""
The Janitor will kill processes during reactor cleanup.
"""
@implementer(IProcessTransport)
class StubProcessTransport(object):
"""
A stub L{IProcessTransport} provider which records signals.
@ivar signals: The signals passed to L{signalProcess}.
"""
def __init__(self):
self.signals = []
def signalProcess(self, signal):
"""
Append C{signal} to C{self.signals}.
"""
self.signals.append(signal)
pt = StubProcessTransport()
reactor = StubReactor([], [pt])
jan = _Janitor(None, None, reactor=reactor)
jan._cleanReactor()
self.assertEqual(pt.signals, ["KILL"])
def test_cleanReactorReturnsSelectableStrings(self):
"""
The Janitor returns string representations of the selectables that it
cleaned up from the reactor cleanup method.
"""
class Selectable(object):
"""
A stub Selectable which only has an interesting string
representation.
"""
def __repr__(self):
return "(SELECTABLE!)"
reactor = StubReactor([], [Selectable()])
jan = _Janitor(None, None, reactor=reactor)
self.assertEqual(jan._cleanReactor(), ["(SELECTABLE!)"])
def test_postCaseCleanupNoErrors(self):
"""
The post-case cleanup method will return True and not call C{addError}
on the result if there are no pending calls.
"""
reactor = StubReactor([])
test = object()
reporter = StubErrorReporter()
jan = _Janitor(test, reporter, reactor=reactor)
self.assertTrue(jan.postCaseCleanup())
self.assertEqual(reporter.errors, [])
def test_postCaseCleanupWithErrors(self):
"""
The post-case cleanup method will return False and call C{addError} on
the result with a L{DirtyReactorAggregateError} Failure if there are
pending calls.
"""
delayedCall = DelayedCall(300, lambda: None, (), {},
lambda x: None, lambda x: None,
seconds=lambda: 0)
delayedCallString = str(delayedCall)
reactor = StubReactor([delayedCall], [])
test = object()
reporter = StubErrorReporter()
jan = _Janitor(test, reporter, reactor=reactor)
self.assertFalse(jan.postCaseCleanup())
self.assertEqual(len(reporter.errors), 1)
self.assertEqual(reporter.errors[0][1].value.delayedCalls,
[delayedCallString])
def test_postClassCleanupNoErrors(self):
"""
The post-class cleanup method will not call C{addError} on the result
if there are no pending calls or selectables.
"""
reactor = StubReactor([])
test = object()
reporter = StubErrorReporter()
jan = _Janitor(test, reporter, reactor=reactor)
jan.postClassCleanup()
self.assertEqual(reporter.errors, [])
def test_postClassCleanupWithPendingCallErrors(self):
"""
The post-class cleanup method call C{addError} on the result with a
L{DirtyReactorAggregateError} Failure if there are pending calls.
"""
delayedCall = DelayedCall(300, lambda: None, (), {},
lambda x: None, lambda x: None,
seconds=lambda: 0)
delayedCallString = str(delayedCall)
reactor = StubReactor([delayedCall], [])
test = object()
reporter = StubErrorReporter()
jan = _Janitor(test, reporter, reactor=reactor)
jan.postClassCleanup()
self.assertEqual(len(reporter.errors), 1)
self.assertEqual(reporter.errors[0][1].value.delayedCalls,
[delayedCallString])
def test_postClassCleanupWithSelectableErrors(self):
"""
The post-class cleanup method call C{addError} on the result with a
L{DirtyReactorAggregateError} Failure if there are selectables.
"""
selectable = "SELECTABLE HERE"
reactor = StubReactor([], [selectable])
test = object()
reporter = StubErrorReporter()
jan = _Janitor(test, reporter, reactor=reactor)
jan.postClassCleanup()
self.assertEqual(len(reporter.errors), 1)
self.assertEqual(reporter.errors[0][1].value.selectables,
[repr(selectable)])
class RemoveSafelyTests(SynchronousTestCase):
"""
Tests for L{util._removeSafely}.
"""
def test_removeSafelyNoTrialMarker(self):
"""
If a path doesn't contain a node named C{"_trial_marker"}, that path is
not removed by L{util._removeSafely} and a L{util._NoTrialMarker}
exception is raised instead.
"""
directory = self.mktemp().encode("utf-8")
os.mkdir(directory)
dirPath = filepath.FilePath(directory)
self.assertRaises(util._NoTrialMarker, util._removeSafely, dirPath)
def test_removeSafelyRemoveFailsMoveSucceeds(self):
"""
If an L{OSError} is raised while removing a path in
L{util._removeSafely}, an attempt is made to move the path to a new
name.
"""
def dummyRemove():
"""
Raise an C{OSError} to emulate the branch of L{util._removeSafely}
in which path removal fails.
"""
raise OSError()
# Patch stdout so we can check the print statements in _removeSafely
out = NativeStringIO()
self.patch(sys, 'stdout', out)
# Set up a trial directory with a _trial_marker
directory = self.mktemp().encode("utf-8")
os.mkdir(directory)
dirPath = filepath.FilePath(directory)
dirPath.child(b'_trial_marker').touch()
# Ensure that path.remove() raises an OSError
dirPath.remove = dummyRemove
util._removeSafely(dirPath)
self.assertIn("could not remove FilePath", out.getvalue())
def test_removeSafelyRemoveFailsMoveFails(self):
"""
If an L{OSError} is raised while removing a path in
L{util._removeSafely}, an attempt is made to move the path to a new
name. If that attempt fails, the L{OSError} is re-raised.
"""
def dummyRemove():
"""
Raise an C{OSError} to emulate the branch of L{util._removeSafely}
in which path removal fails.
"""
raise OSError("path removal failed")
def dummyMoveTo(path):
"""
Raise an C{OSError} to emulate the branch of L{util._removeSafely}
in which path movement fails.
"""
raise OSError("path movement failed")
# Patch stdout so we can check the print statements in _removeSafely
out = NativeStringIO()
self.patch(sys, 'stdout', out)
# Set up a trial directory with a _trial_marker
directory = self.mktemp().encode("utf-8")
os.mkdir(directory)
dirPath = filepath.FilePath(directory)
dirPath.child(b'_trial_marker').touch()
# Ensure that path.remove() and path.moveTo() both raise OSErrors
dirPath.remove = dummyRemove
dirPath.moveTo = dummyMoveTo
error = self.assertRaises(OSError, util._removeSafely, dirPath)
self.assertEqual(str(error), "path movement failed")
self.assertIn("could not remove FilePath", out.getvalue())
class ExcInfoTests(SynchronousTestCase):
"""
Tests for L{excInfoOrFailureToExcInfo}.
"""
def test_excInfo(self):
"""
L{excInfoOrFailureToExcInfo} returns exactly what it is passed, if it is
passed a tuple like the one returned by L{sys.exc_info}.
"""
info = (ValueError, ValueError("foo"), None)
self.assertTrue(info is excInfoOrFailureToExcInfo(info))
def test_failure(self):
"""
When called with a L{Failure} instance, L{excInfoOrFailureToExcInfo}
returns a tuple like the one returned by L{sys.exc_info}, with the
elements taken from the type, value, and traceback of the failure.
"""
try:
1 / 0
except:
f = Failure()
self.assertEqual((f.type, f.value, f.tb), excInfoOrFailureToExcInfo(f))
class AcquireAttributeTests(SynchronousTestCase):
"""
Tests for L{acquireAttribute}.
"""
def test_foundOnEarlierObject(self):
"""
The value returned by L{acquireAttribute} is the value of the requested
attribute on the first object in the list passed in which has that
attribute.
"""
self.value = value = object()
self.assertTrue(value is acquireAttribute([self, object()], "value"))
def test_foundOnLaterObject(self):
"""
The same as L{test_foundOnEarlierObject}, but for the case where the 2nd
element in the object list has the attribute and the first does not.
"""
self.value = value = object()
self.assertTrue(value is acquireAttribute([object(), self], "value"))
def test_notFoundException(self):
"""
If none of the objects passed in the list to L{acquireAttribute} have
the requested attribute, L{AttributeError} is raised.
"""
self.assertRaises(AttributeError, acquireAttribute, [object()], "foo")
def test_notFoundDefault(self):
"""
If none of the objects passed in the list to L{acquireAttribute} have
the requested attribute and a default value is given, the default value
is returned.
"""
default = object()
self.assertTrue(default is acquireAttribute([object()], "foo", default))
class TestListToPhrase(SynchronousTestCase):
"""
Input is transformed into a string representation of the list,
with each item separated by delimiter (defaulting to a comma) and the final
two being separated by a final delimiter.
"""
def test_empty(self):
"""
If things is empty, an empty string is returned.
"""
sample = []
expected = ''
result = util._listToPhrase(sample, 'and')
self.assertEqual(expected, result)
def test_oneWord(self):
"""
With a single item, the item is returned.
"""
sample = ['One']
expected = 'One'
result = util._listToPhrase(sample, 'and')
self.assertEqual(expected, result)
def test_twoWords(self):
"""
Two words are separated by the final delimiter.
"""
sample = ['One', 'Two']
expected = 'One and Two'
result = util._listToPhrase(sample, 'and')
self.assertEqual(expected, result)
def test_threeWords(self):
"""
With more than two words, the first two are separated by the delimiter.
"""
sample = ['One', 'Two', 'Three']
expected = 'One, Two, and Three'
result = util._listToPhrase(sample, 'and')
self.assertEqual(expected, result)
def test_fourWords(self):
"""
If a delimiter is specified, it is used instead of the default comma.
"""
sample = ['One', 'Two', 'Three', 'Four']
expected = 'One; Two; Three; or Four'
result = util._listToPhrase(sample, 'or', delimiter='; ')
self.assertEqual(expected, result)
def test_notString(self):
"""
If something in things is not a string, it is converted into one.
"""
sample = [1, 2, 'three']
expected = '1, 2, and three'
result = util._listToPhrase(sample, 'and')
self.assertEqual(expected, result)
def test_stringTypeError(self):
"""
If things is a string, a TypeError is raised.
"""
sample = "One, two, three"
error = self.assertRaises(TypeError, util._listToPhrase, sample, 'and')
self.assertEqual(str(error), "Things must be a list or a tuple")
def test_iteratorTypeError(self):
"""
If things is an iterator, a TypeError is raised.
"""
sample = iter([1, 2, 3])
error = self.assertRaises(TypeError, util._listToPhrase, sample, 'and')
self.assertEqual(str(error), "Things must be a list or a tuple")
def test_generatorTypeError(self):
"""
If things is a generator, a TypeError is raised.
"""
def sample():
for i in range(2):
yield i
error = self.assertRaises(TypeError, util._listToPhrase, sample, 'and')
self.assertEqual(str(error), "Things must be a list or a tuple")

View file

@ -0,0 +1,491 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for Trial's interaction with the Python warning system.
"""
from __future__ import division, absolute_import
import sys, warnings
from unittest import TestResult
from twisted.python.compat import NativeStringIO as StringIO
from twisted.python.filepath import FilePath
from twisted.trial.unittest import (
SynchronousTestCase, _collectWarnings, _setWarningRegistryToNone)
class Mask(object):
"""
Hide a test case definition from trial's automatic discovery mechanism.
"""
class MockTests(SynchronousTestCase):
"""
A test case which is used by L{FlushWarningsTests} to verify behavior
which cannot be verified by code inside a single test method.
"""
message = "some warning text"
category = UserWarning
def test_unflushed(self):
"""
Generate a warning and don't flush it.
"""
warnings.warn(self.message, self.category)
def test_flushed(self):
"""
Generate a warning and flush it.
"""
warnings.warn(self.message, self.category)
self.assertEqual(len(self.flushWarnings()), 1)
class FlushWarningsTests(SynchronousTestCase):
"""
Tests for C{flushWarnings}, an API for examining the warnings
emitted so far in a test.
"""
def assertDictSubset(self, set, subset):
"""
Assert that all the keys present in C{subset} are also present in
C{set} and that the corresponding values are equal.
"""
for k, v in subset.items():
self.assertEqual(set[k], v)
def assertDictSubsets(self, sets, subsets):
"""
For each pair of corresponding elements in C{sets} and C{subsets},
assert that the element from C{subsets} is a subset of the element from
C{sets}.
"""
self.assertEqual(len(sets), len(subsets))
for a, b in zip(sets, subsets):
self.assertDictSubset(a, b)
def test_none(self):
"""
If no warnings are emitted by a test, C{flushWarnings} returns an empty
list.
"""
self.assertEqual(self.flushWarnings(), [])
def test_several(self):
"""
If several warnings are emitted by a test, C{flushWarnings} returns a
list containing all of them.
"""
firstMessage = "first warning message"
firstCategory = UserWarning
warnings.warn(message=firstMessage, category=firstCategory)
secondMessage = "second warning message"
secondCategory = RuntimeWarning
warnings.warn(message=secondMessage, category=secondCategory)
self.assertDictSubsets(
self.flushWarnings(),
[{'category': firstCategory, 'message': firstMessage},
{'category': secondCategory, 'message': secondMessage}])
def test_repeated(self):
"""
The same warning triggered twice from the same place is included twice
in the list returned by C{flushWarnings}.
"""
message = "the message"
category = RuntimeWarning
for i in range(2):
warnings.warn(message=message, category=category)
self.assertDictSubsets(
self.flushWarnings(),
[{'category': category, 'message': message}] * 2)
def test_cleared(self):
"""
After a particular warning event has been returned by C{flushWarnings},
it is not returned by subsequent calls.
"""
message = "the message"
category = RuntimeWarning
warnings.warn(message=message, category=category)
self.assertDictSubsets(
self.flushWarnings(),
[{'category': category, 'message': message}])
self.assertEqual(self.flushWarnings(), [])
def test_unflushed(self):
"""
Any warnings emitted by a test which are not flushed are emitted to the
Python warning system.
"""
result = TestResult()
case = Mask.MockTests('test_unflushed')
case.run(result)
warningsShown = self.flushWarnings([Mask.MockTests.test_unflushed])
self.assertEqual(warningsShown[0]['message'], 'some warning text')
self.assertIdentical(warningsShown[0]['category'], UserWarning)
where = type(case).test_unflushed.__code__
filename = where.co_filename
# If someone edits MockTests.test_unflushed, the value added to
# firstlineno might need to change.
lineno = where.co_firstlineno + 4
self.assertEqual(warningsShown[0]['filename'], filename)
self.assertEqual(warningsShown[0]['lineno'], lineno)
self.assertEqual(len(warningsShown), 1)
def test_flushed(self):
"""
Any warnings emitted by a test which are flushed are not emitted to the
Python warning system.
"""
result = TestResult()
case = Mask.MockTests('test_flushed')
output = StringIO()
monkey = self.patch(sys, 'stdout', output)
case.run(result)
monkey.restore()
self.assertEqual(output.getvalue(), "")
def test_warningsConfiguredAsErrors(self):
"""
If a warnings filter has been installed which turns warnings into
exceptions, tests have an error added to the reporter for them for each
unflushed warning.
"""
class CustomWarning(Warning):
pass
result = TestResult()
case = Mask.MockTests('test_unflushed')
case.category = CustomWarning
originalWarnings = warnings.filters[:]
try:
warnings.simplefilter('error')
case.run(result)
self.assertEqual(len(result.errors), 1)
self.assertIdentical(result.errors[0][0], case)
self.assertTrue(
# Different python versions differ in whether they report the
# fully qualified class name or just the class name.
result.errors[0][1].splitlines()[-1].endswith(
"CustomWarning: some warning text"))
finally:
warnings.filters[:] = originalWarnings
def test_flushedWarningsConfiguredAsErrors(self):
"""
If a warnings filter has been installed which turns warnings into
exceptions, tests which emit those warnings but flush them do not have
an error added to the reporter.
"""
class CustomWarning(Warning):
pass
result = TestResult()
case = Mask.MockTests('test_flushed')
case.category = CustomWarning
originalWarnings = warnings.filters[:]
try:
warnings.simplefilter('error')
case.run(result)
self.assertEqual(result.errors, [])
finally:
warnings.filters[:] = originalWarnings
def test_multipleFlushes(self):
"""
Any warnings emitted after a call to C{flushWarnings} can be flushed by
another call to C{flushWarnings}.
"""
warnings.warn("first message")
self.assertEqual(len(self.flushWarnings()), 1)
warnings.warn("second message")
self.assertEqual(len(self.flushWarnings()), 1)
def test_filterOnOffendingFunction(self):
"""
The list returned by C{flushWarnings} includes only those
warnings which refer to the source of the function passed as the value
for C{offendingFunction}, if a value is passed for that parameter.
"""
firstMessage = "first warning text"
firstCategory = UserWarning
def one():
warnings.warn(firstMessage, firstCategory, stacklevel=1)
secondMessage = "some text"
secondCategory = RuntimeWarning
def two():
warnings.warn(secondMessage, secondCategory, stacklevel=1)
one()
two()
self.assertDictSubsets(
self.flushWarnings(offendingFunctions=[one]),
[{'category': firstCategory, 'message': firstMessage}])
self.assertDictSubsets(
self.flushWarnings(offendingFunctions=[two]),
[{'category': secondCategory, 'message': secondMessage}])
def test_functionBoundaries(self):
"""
Verify that warnings emitted at the very edges of a function are still
determined to be emitted from that function.
"""
def warner():
warnings.warn("first line warning")
warnings.warn("internal line warning")
warnings.warn("last line warning")
warner()
self.assertEqual(
len(self.flushWarnings(offendingFunctions=[warner])), 3)
def test_invalidFilter(self):
"""
If an object which is neither a function nor a method is included in the
C{offendingFunctions} list, C{flushWarnings} raises L{ValueError}. Such
a call flushes no warnings.
"""
warnings.warn("oh no")
self.assertRaises(ValueError, self.flushWarnings, [None])
self.assertEqual(len(self.flushWarnings()), 1)
def test_missingSource(self):
"""
Warnings emitted by a function the source code of which is not
available can still be flushed.
"""
package = FilePath(self.mktemp().encode('utf-8')).child(b'twisted_private_helper')
package.makedirs()
package.child(b'__init__.py').setContent(b'')
package.child(b'missingsourcefile.py').setContent(b'''
import warnings
def foo():
warnings.warn("oh no")
''')
pathEntry = package.parent().path.decode('utf-8')
sys.path.insert(0, pathEntry)
self.addCleanup(sys.path.remove, pathEntry)
from twisted_private_helper import missingsourcefile
self.addCleanup(sys.modules.pop, 'twisted_private_helper')
self.addCleanup(sys.modules.pop, missingsourcefile.__name__)
package.child(b'missingsourcefile.py').remove()
missingsourcefile.foo()
self.assertEqual(len(self.flushWarnings([missingsourcefile.foo])), 1)
def test_renamedSource(self):
"""
Warnings emitted by a function defined in a file which has been renamed
since it was initially compiled can still be flushed.
This is testing the code which specifically supports working around the
unfortunate behavior of CPython to write a .py source file name into
the .pyc files it generates and then trust that it is correct in
various places. If source files are renamed, .pyc files may not be
regenerated, but they will contain incorrect filenames.
"""
package = FilePath(self.mktemp().encode('utf-8')).child(b'twisted_private_helper')
package.makedirs()
package.child(b'__init__.py').setContent(b'')
package.child(b'module.py').setContent(b'''
import warnings
def foo():
warnings.warn("oh no")
''')
pathEntry = package.parent().path.decode('utf-8')
sys.path.insert(0, pathEntry)
self.addCleanup(sys.path.remove, pathEntry)
# Import it to cause pycs to be generated
from twisted_private_helper import module
# Clean up the state resulting from that import; we're not going to use
# this module, so it should go away.
del sys.modules['twisted_private_helper']
del sys.modules[module.__name__]
# Some Python versions have extra state related to the just
# imported/renamed package. Clean it up too. See also
# http://bugs.python.org/issue15912
try:
from importlib import invalidate_caches
except ImportError:
pass
else:
invalidate_caches()
# Rename the source directory
package.moveTo(package.sibling(b'twisted_renamed_helper'))
# Import the newly renamed version
from twisted_renamed_helper import module
self.addCleanup(sys.modules.pop, 'twisted_renamed_helper')
self.addCleanup(sys.modules.pop, module.__name__)
# Generate the warning
module.foo()
# Flush it
self.assertEqual(len(self.flushWarnings([module.foo])), 1)
class FakeWarning(Warning):
pass
class CollectWarningsTests(SynchronousTestCase):
"""
Tests for L{_collectWarnings}.
"""
def test_callsObserver(self):
"""
L{_collectWarnings} calls the observer with each emitted warning.
"""
firstMessage = "dummy calls observer warning"
secondMessage = firstMessage[::-1]
events = []
def f():
events.append('call')
warnings.warn(firstMessage)
warnings.warn(secondMessage)
events.append('returning')
_collectWarnings(events.append, f)
self.assertEqual(events[0], 'call')
self.assertEqual(events[1].message, firstMessage)
self.assertEqual(events[2].message, secondMessage)
self.assertEqual(events[3], 'returning')
self.assertEqual(len(events), 4)
def test_suppresses(self):
"""
Any warnings emitted by a call to a function passed to
L{_collectWarnings} are not actually emitted to the warning system.
"""
output = StringIO()
self.patch(sys, 'stdout', output)
_collectWarnings(lambda x: None, warnings.warn, "text")
self.assertEqual(output.getvalue(), "")
def test_callsFunction(self):
"""
L{_collectWarnings} returns the result of calling the callable passed to
it with the parameters given.
"""
arguments = []
value = object()
def f(*args, **kwargs):
arguments.append((args, kwargs))
return value
result = _collectWarnings(lambda x: None, f, 1, 'a', b=2, c='d')
self.assertEqual(arguments, [((1, 'a'), {'b': 2, 'c': 'd'})])
self.assertIdentical(result, value)
def test_duplicateWarningCollected(self):
"""
Subsequent emissions of a warning from a particular source site can be
collected by L{_collectWarnings}. In particular, the per-module
emitted-warning cache should be bypassed (I{__warningregistry__}).
"""
# Make sure the worst case is tested: if __warningregistry__ isn't in a
# module's globals, then the warning system will add it and start using
# it to avoid emitting duplicate warnings. Delete __warningregistry__
# to ensure that even modules which are first imported as a test is
# running still interact properly with the warning system.
global __warningregistry__
del __warningregistry__
def f():
warnings.warn("foo")
warnings.simplefilter('default')
f()
events = []
_collectWarnings(events.append, f)
self.assertEqual(len(events), 1)
self.assertEqual(events[0].message, "foo")
self.assertEqual(len(self.flushWarnings()), 1)
def test_immutableObject(self):
"""
L{_collectWarnings}'s behavior is not altered by the presence of an
object which cannot have attributes set on it as a value in
C{sys.modules}.
"""
key = object()
sys.modules[key] = key
self.addCleanup(sys.modules.pop, key)
self.test_duplicateWarningCollected()
def test_setWarningRegistryChangeWhileIterating(self):
"""
If the dictionary passed to L{_setWarningRegistryToNone} changes size
partway through the process, C{_setWarningRegistryToNone} continues to
set C{__warningregistry__} to C{None} on the rest of the values anyway.
This might be caused by C{sys.modules} containing something that's not
really a module and imports things on setattr. py.test does this, as
does L{twisted.python.deprecate.deprecatedModuleAttribute}.
"""
d = {}
class A(object):
def __init__(self, key):
self.__dict__['_key'] = key
def __setattr__(self, value, item):
d[self._key] = None
key1 = object()
key2 = object()
d[key1] = A(key2)
key3 = object()
key4 = object()
d[key3] = A(key4)
_setWarningRegistryToNone(d)
# If both key2 and key4 were added, then both A instanced were
# processed.
self.assertEqual(set([key1, key2, key3, key4]), set(d.keys()))

View file

@ -0,0 +1,23 @@
from __future__ import division, absolute_import
import unittest
from twisted.internet import defer
# Used in test_tests.TestUnhandledDeferred
class TestBleeding(unittest.TestCase):
"""This test creates an unhandled Deferred and leaves it in a cycle.
The Deferred is left in a cycle so that the garbage collector won't pick it
up immediately. We were having some problems where unhandled Deferreds in
one test were failing random other tests. (See #1507, #1213)
"""
def test_unhandledDeferred(self):
try:
1/0
except ZeroDivisionError:
f = defer.fail()
# these two lines create the cycle. don't remove them
l = [f]
l.append(l)

View file

@ -0,0 +1,41 @@
# -*- test-case-name: twisted.trial.test -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Things likely to be used by writers of unit tests.
"""
from __future__ import division, absolute_import
# Define the public API from the two implementation modules
from twisted.trial._synctest import (
FailTest, SkipTest, SynchronousTestCase, PyUnitResultAdapter, Todo,
makeTodo)
from twisted.trial._asynctest import TestCase
from twisted.python.compat import _PY3
if not _PY3:
from twisted.trial._asyncrunner import (
TestSuite, TestDecorator, decorate)
from twisted.trial._asyncrunner import (
_ForceGarbageCollectionDecorator, _iterateTests, _clearSuite)
# Further obscure the origins of these objects, to reduce surprise (and this is
# what the values were before code got shuffled around between files, but was
# otherwise unchanged).
FailTest.__module__ = SkipTest.__module__ = __name__
# Grab some implementation details so tests can continue to import them from
# here, rather than being concerned with which implementation module they come
# from (is this a good idea?)
from twisted.trial._synctest import (
_LogObserver, _logObserver, _collectWarnings, _setWarningRegistryToNone)
__all__ = [
'FailTest', 'SkipTest', 'SynchronousTestCase', 'Todo', 'makeTodo',
'TestCase', 'TestSuite', 'decorate']

View file

@ -0,0 +1,432 @@
# -*- test-case-name: twisted.trial.test.test_util -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
#
"""
A collection of utility functions and classes, used internally by Trial.
This code is for Trial's internal use. Do NOT use this code if you are writing
tests. It is subject to change at the Trial maintainer's whim. There is
nothing here in this module for you to use unless you are maintaining Trial.
Any non-Trial Twisted code that uses this module will be shot.
Maintainer: Jonathan Lange
@var DEFAULT_TIMEOUT_DURATION: The default timeout which will be applied to
asynchronous (ie, Deferred-returning) test methods, in seconds.
"""
from __future__ import division, absolute_import, print_function
import sys
from random import randrange
from twisted.internet import defer, utils, interfaces
from twisted.python.failure import Failure
from twisted.python import deprecate, versions
from twisted.python.filepath import FilePath
from twisted.python.lockfile import FilesystemLock
__all__ = [
'DEFAULT_TIMEOUT_DURATION',
'excInfoOrFailureToExcInfo', 'suppress', 'acquireAttribute']
DEFAULT_TIMEOUT = object()
DEFAULT_TIMEOUT_DURATION = 120.0
class DirtyReactorAggregateError(Exception):
"""
Passed to L{twisted.trial.itrial.IReporter.addError} when the reactor is
left in an unclean state after a test.
@ivar delayedCalls: The L{DelayedCall<twisted.internet.base.DelayedCall>}
objects which weren't cleaned up.
@ivar selectables: The selectables which weren't cleaned up.
"""
def __init__(self, delayedCalls, selectables=None):
self.delayedCalls = delayedCalls
self.selectables = selectables
def __str__(self):
"""
Return a multi-line message describing all of the unclean state.
"""
msg = "Reactor was unclean."
if self.delayedCalls:
msg += ("\nDelayedCalls: (set "
"twisted.internet.base.DelayedCall.debug = True to "
"debug)\n")
msg += "\n".join(map(str, self.delayedCalls))
if self.selectables:
msg += "\nSelectables:\n"
msg += "\n".join(map(str, self.selectables))
return msg
class _Janitor(object):
"""
The guy that cleans up after you.
@ivar test: The L{TestCase} to report errors about.
@ivar result: The L{IReporter} to report errors to.
@ivar reactor: The reactor to use. If None, the global reactor
will be used.
"""
def __init__(self, test, result, reactor=None):
"""
@param test: See L{_Janitor.test}.
@param result: See L{_Janitor.result}.
@param reactor: See L{_Janitor.reactor}.
"""
self.test = test
self.result = result
self.reactor = reactor
def postCaseCleanup(self):
"""
Called by L{unittest.TestCase} after a test to catch any logged errors
or pending L{DelayedCall<twisted.internet.base.DelayedCall>}s.
"""
calls = self._cleanPending()
if calls:
aggregate = DirtyReactorAggregateError(calls)
self.result.addError(self.test, Failure(aggregate))
return False
return True
def postClassCleanup(self):
"""
Called by L{unittest.TestCase} after the last test in a C{TestCase}
subclass. Ensures the reactor is clean by murdering the threadpool,
catching any pending
L{DelayedCall<twisted.internet.base.DelayedCall>}s, open sockets etc.
"""
selectables = self._cleanReactor()
calls = self._cleanPending()
if selectables or calls:
aggregate = DirtyReactorAggregateError(calls, selectables)
self.result.addError(self.test, Failure(aggregate))
self._cleanThreads()
def _getReactor(self):
"""
Get either the passed-in reactor or the global reactor.
"""
if self.reactor is not None:
reactor = self.reactor
else:
from twisted.internet import reactor
return reactor
def _cleanPending(self):
"""
Cancel all pending calls and return their string representations.
"""
reactor = self._getReactor()
# flush short-range timers
reactor.iterate(0)
reactor.iterate(0)
delayedCallStrings = []
for p in reactor.getDelayedCalls():
if p.active():
delayedString = str(p)
p.cancel()
else:
print("WEIRDNESS! pending timed call not active!")
delayedCallStrings.append(delayedString)
return delayedCallStrings
_cleanPending = utils.suppressWarnings(
_cleanPending, (('ignore',), {'category': DeprecationWarning,
'message':
r'reactor\.iterate cannot be used.*'}))
def _cleanThreads(self):
reactor = self._getReactor()
if interfaces.IReactorThreads.providedBy(reactor):
if reactor.threadpool is not None:
# Stop the threadpool now so that a new one is created.
# This improves test isolation somewhat (although this is a
# post class cleanup hook, so it's only isolating classes
# from each other, not methods from each other).
reactor._stopThreadPool()
def _cleanReactor(self):
"""
Remove all selectables from the reactor, kill any of them that were
processes, and return their string representation.
"""
reactor = self._getReactor()
selectableStrings = []
for sel in reactor.removeAll():
if interfaces.IProcessTransport.providedBy(sel):
sel.signalProcess('KILL')
selectableStrings.append(repr(sel))
return selectableStrings
_DEFAULT = object()
def acquireAttribute(objects, attr, default=_DEFAULT):
"""
Go through the list 'objects' sequentially until we find one which has
attribute 'attr', then return the value of that attribute. If not found,
return 'default' if set, otherwise, raise AttributeError.
"""
for obj in objects:
if hasattr(obj, attr):
return getattr(obj, attr)
if default is not _DEFAULT:
return default
raise AttributeError('attribute %r not found in %r' % (attr, objects))
def excInfoOrFailureToExcInfo(err):
"""
Coerce a Failure to an _exc_info, if err is a Failure.
@param err: Either a tuple such as returned by L{sys.exc_info} or a
L{Failure} object.
@return: A tuple like the one returned by L{sys.exc_info}. e.g.
C{exception_type, exception_object, traceback_object}.
"""
if isinstance(err, Failure):
# Unwrap the Failure into a exc_info tuple.
err = (err.type, err.value, err.getTracebackObject())
return err
def suppress(action='ignore', **kwarg):
"""
Sets up the .suppress tuple properly, pass options to this method as you
would the stdlib warnings.filterwarnings()
So, to use this with a .suppress magic attribute you would do the
following:
>>> from twisted.trial import unittest, util
>>> import warnings
>>>
>>> class TestFoo(unittest.TestCase):
... def testFooBar(self):
... warnings.warn("i am deprecated", DeprecationWarning)
... testFooBar.suppress = [util.suppress(message='i am deprecated')]
...
>>>
Note that as with the todo and timeout attributes: the module level
attribute acts as a default for the class attribute which acts as a default
for the method attribute. The suppress attribute can be overridden at any
level by specifying C{.suppress = []}
"""
return ((action,), kwarg)
# This should be deleted, and replaced with twisted.application's code; see
# #6016:
def profiled(f, outputFile):
def _(*args, **kwargs):
import profile
prof = profile.Profile()
try:
result = prof.runcall(f, *args, **kwargs)
prof.dump_stats(outputFile)
except SystemExit:
pass
prof.print_stats()
return result
return _
def getPythonContainers(meth):
"""Walk up the Python tree from method 'meth', finding its class, its module
and all containing packages."""
containers = []
containers.append(meth.im_class)
moduleName = meth.im_class.__module__
while moduleName is not None:
module = sys.modules.get(moduleName, None)
if module is None:
module = __import__(moduleName)
containers.append(module)
moduleName = getattr(module, '__module__', None)
return containers
deprecate.deprecatedModuleAttribute(
versions.Version("Twisted", 12, 3, 0),
"This function never worked correctly. Implement lookup on your own.",
__name__, "getPythonContainers")
def _runSequentially(callables, stopOnFirstError=False):
"""
Run the given callables one after the other. If a callable returns a
Deferred, wait until it has finished before running the next callable.
@param callables: An iterable of callables that take no parameters.
@param stopOnFirstError: If True, then stop running callables as soon as
one raises an exception or fires an errback. False by default.
@return: A L{Deferred} that fires a list of C{(flag, value)} tuples. Each
tuple will be either C{(SUCCESS, <return value>)} or C{(FAILURE,
<Failure>)}.
"""
results = []
for f in callables:
d = defer.maybeDeferred(f)
thing = defer.waitForDeferred(d)
yield thing
try:
results.append((defer.SUCCESS, thing.getResult()))
except:
results.append((defer.FAILURE, Failure()))
if stopOnFirstError:
break
yield results
_runSequentially = defer.deferredGenerator(_runSequentially)
class _NoTrialMarker(Exception):
"""
No trial marker file could be found.
Raised when trial attempts to remove a trial temporary working directory
that does not contain a marker file.
"""
def _removeSafely(path):
"""
Safely remove a path, recursively.
If C{path} does not contain a node named C{_trial_marker}, a
L{_NoTrialMarker} exception is raised and the path is not removed.
"""
if not path.child(b'_trial_marker').exists():
raise _NoTrialMarker(
'%r is not a trial temporary path, refusing to remove it'
% (path,))
try:
path.remove()
except OSError as e:
print ("could not remove %r, caught OSError [Errno %s]: %s"
% (path, e.errno, e.strerror))
try:
newPath = FilePath(b'_trial_temp_old' +
str(randrange(10000000)).encode("utf-8"))
path.moveTo(newPath)
except OSError as e:
print ("could not rename path, caught OSError [Errno %s]: %s"
% (e.errno,e.strerror))
raise
class _WorkingDirectoryBusy(Exception):
"""
A working directory was specified to the runner, but another test run is
currently using that directory.
"""
def _unusedTestDirectory(base):
"""
Find an unused directory named similarly to C{base}.
Once a directory is found, it will be locked and a marker dropped into it to
identify it as a trial temporary directory.
@param base: A template path for the discovery process. If this path
exactly cannot be used, a path which varies only in a suffix of the
basename will be used instead.
@type base: L{FilePath}
@return: A two-tuple. The first element is a L{FilePath} representing the
directory which was found and created. The second element is a locked
L{FilesystemLock<twisted.python.lockfile.FilesystemLock>}. Another
call to C{_unusedTestDirectory} will not be able to reused the the
same name until the lock is released, either explicitly or by this
process exiting.
"""
counter = 0
while True:
if counter:
testdir = base.sibling('%s-%d' % (base.basename(), counter))
else:
testdir = base
testDirLock = FilesystemLock(testdir.path + '.lock')
if testDirLock.lock():
# It is not in use
if testdir.exists():
# It exists though - delete it
_removeSafely(testdir)
# Create it anew and mark it as ours so the next _removeSafely on it
# succeeds.
testdir.makedirs()
testdir.child('_trial_marker').setContent('')
return testdir, testDirLock
else:
# It is in use
if base.basename() == '_trial_temp':
counter += 1
else:
raise _WorkingDirectoryBusy()
def _listToPhrase(things, finalDelimiter, delimiter=', '):
"""
Produce a string containing each thing in C{things},
separated by a C{delimiter}, with the last couple being separated
by C{finalDelimiter}
@param things: The elements of the resulting phrase
@type things: L{list} or L{tuple}
@param finalDelimiter: What to put between the last two things
(typically 'and' or 'or')
@type finalDelimiter: L{str}
@param delimiter: The separator to use between each thing,
not including the last two. Should typically include a trailing space.
@type delimiter: L{str}
@return: The resulting phrase
@rtype: L{str}
"""
if not isinstance(things, (list, tuple)):
raise TypeError("Things must be a list or a tuple")
if not things:
return ''
if len(things) == 1:
return str(things[0])
if len(things) == 2:
return "%s %s %s" % (str(things[0]), finalDelimiter, str(things[1]))
else:
strThings = []
for thing in things:
strThings.append(str(thing))
return "%s%s%s %s" % (delimiter.join(strThings[:-1]),
delimiter, finalDelimiter, strThings[-1])