本文整理汇总了Python中twisted.trial.unittest.SynchronousTestCase方法的典型用法代码示例。如果您正苦于以下问题:Python unittest.SynchronousTestCase方法的具体用法?Python unittest.SynchronousTestCase怎么用?Python unittest.SynchronousTestCase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.trial.unittest
的用法示例。
在下文中一共展示了unittest.SynchronousTestCase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeTestCaseClasses
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def makeTestCaseClasses(cls):
"""
Create a L{SynchronousTestCase} subclass which mixes in C{cls} for each
known reactor and return a dict mapping their names to them.
"""
classes = {}
for reactor in cls._reactors:
shortReactorName = reactor.split(".")[-1]
name = (cls.__name__ + "." + shortReactorName + "Tests").replace(".", "_")
class testcase(cls, SynchronousTestCase):
__module__ = cls.__module__
if reactor in cls.skippedReactors:
skip = cls.skippedReactors[reactor]
try:
reactorFactory = namedAny(reactor)
except:
skip = Failure().getErrorMessage()
testcase.__name__ = name
if hasattr(cls, "__qualname__"):
testcase.__qualname__ = ".".join(cls.__qualname__.split()[0:-1] + [name])
classes[testcase.__name__] = testcase
return classes
示例2: test_failureResultOfWithWrongFailureMultiExpectedFailure
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def test_failureResultOfWithWrongFailureMultiExpectedFailure(self):
"""
L{SynchronousTestCase.failureResultOf} raises
L{SynchronousTestCase.failureException} when called with a L{Deferred}
with a failure type that was not expected, and the
L{SynchronousTestCase.failureException} message contains the original
failure traceback as well as the expected failure types in the error
message
"""
try:
self.failureResultOf(fail(self.failure), KeyError, IOError)
except self.failureException as e:
self.assertIn(self.failure.getTraceback(), str(e))
self.assertIn(
"Failure of type ({0}.{1} or {2}.{3}) expected on".format(
KeyError.__module__, KeyError.__name__,
IOError.__module__, IOError.__name__),
str(e))
示例3: assertIsSubdomainOf
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def assertIsSubdomainOf(testCase, descendant, ancestor):
"""
Assert that C{descendant} *is* a subdomain of C{ancestor}.
@type testCase: L{unittest.SynchronousTestCase}
@param testCase: The test case on which to run the assertions.
@type descendant: C{str}
@param descendant: The subdomain name to test.
@type ancestor: C{str}
@param ancestor: The superdomain name to test.
"""
testCase.assertTrue(
dns._isSubdomainOf(descendant, ancestor),
'%r is not a subdomain of %r' % (descendant, ancestor))
示例4: assertIsNotSubdomainOf
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def assertIsNotSubdomainOf(testCase, descendant, ancestor):
"""
Assert that C{descendant} *is not* a subdomain of C{ancestor}.
@type testCase: L{unittest.SynchronousTestCase}
@param testCase: The test case on which to run the assertions.
@type descendant: C{str}
@param descendant: The subdomain name to test.
@type ancestor: C{str}
@param ancestor: The superdomain name to test.
"""
testCase.assertFalse(
dns._isSubdomainOf(descendant, ancestor),
'%r is a subdomain of %r' % (descendant, ancestor))
示例5: test_tearDownRunsOnTestFailure
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def test_tearDownRunsOnTestFailure(self):
"""
L{SynchronousTestCase.tearDown} runs when a test method fails.
"""
suite = self.loader.loadTestsFromTestCase(
self.TestFailureButTearDownRuns)
case = list(suite)[0]
self.assertFalse(case.tornDown)
suite.run(self.reporter)
errors = self.reporter.errors
self.assertTrue(len(errors) > 0)
self.assertIsInstance(errors[0][1].value, erroneous.FoolishError)
self.assertEqual(0, self.reporter.successes)
self.assertTrue(case.tornDown)
示例6: test_failureResultOfWithWrongFailureOneExpectedFailure
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def test_failureResultOfWithWrongFailureOneExpectedFailure(self):
"""
L{SynchronousTestCase.failureResultOf} raises
L{SynchronousTestCase.failureException} when called with a L{Deferred}
with a failure type that was not expected, and the
L{SynchronousTestCase.failureException} message contains the original
failure traceback as well as the expected failure type
"""
try:
self.failureResultOf(fail(self.failure), KeyError)
except self.failureException as e:
self.assertIn(self.failure.getTraceback(), str(e))
self.assertIn(
"Failure of type ({0}.{1}) expected on".format(
KeyError.__module__, KeyError.__name__),
str(e))
示例7: stopOnError
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def stopOnError(case, reactor, publisher=None):
"""
Stop the reactor as soon as any error is logged on the given publisher.
This is beneficial for tests which will wait for a L{Deferred} to fire
before completing (by passing or failing). Certain implementation bugs may
prevent the L{Deferred} from firing with any result at all (consider a
protocol's {dataReceived} method that raises an exception: this exception
is logged but it won't ever cause a L{Deferred} to fire). In that case the
test would have to complete by timing out which is a much less desirable
outcome than completing as soon as the unexpected error is encountered.
@param case: A L{SynchronousTestCase} to use to clean up the necessary log
observer when the test is over.
@param reactor: The reactor to stop.
@param publisher: A L{LogPublisher} to watch for errors. If L{None}, the
global log publisher will be watched.
"""
if publisher is None:
from twisted.python import log as publisher
running = [None]
def stopIfError(event):
if running and event.get('isError'):
running.pop()
reactor.stop()
publisher.addObserver(stopIfError)
case.addCleanup(publisher.removeObserver, stopIfError)
示例8: makeTestFixtures
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def makeTestFixtures(self):
class PyunitCase(unittest.SynchronousTestCase):
def test_foo(self):
pass
self.test = PyunitCase('test_foo')
self.suite = pyunit.TestSuite()
示例9: test_concurrentExplicitWorkingDirectory
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def test_concurrentExplicitWorkingDirectory(self):
"""
If a working directory which is already in use is explicitly specified,
L{TrialRunner.run} raises L{_WorkingDirectoryBusy}.
"""
self.parseOptions(['--temp-directory', os.path.abspath(self.mktemp())])
initialDirectory = os.getcwd()
self.addCleanup(os.chdir, initialDirectory)
firstRunner = self.getRunner()
secondRunner = self.getRunner()
class ConcurrentCase(unittest.SynchronousTestCase):
def test_concurrent(self):
"""
Try to start another runner in the same working directory and
assert that it raises L{_WorkingDirectoryBusy}.
"""
self.assertRaises(
util._WorkingDirectoryBusy,
secondRunner.run, ConcurrentCase('test_failure'))
def test_failure(self):
"""
Should not be called, always fails.
"""
self.fail("test_failure should never be called.")
result = firstRunner.run(ConcurrentCase('test_concurrent'))
bad = result.errors + result.failures
if bad:
self.fail(bad[0][1])
示例10: test_synchronousTestCaseErrorOnGeneratorFunction
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
def test_synchronousTestCaseErrorOnGeneratorFunction(self):
"""
In a SynchronousTestCase, a test method which is a generator function
is reported as an error, as such a method will never run assertions.
"""
class GeneratorSynchronousTestCase(unittest.SynchronousTestCase):
"""
A fake SynchronousTestCase for testing purposes.
"""
def test_generator(self):
"""
A method which is also a generator function, for testing
purposes.
"""
self.fail('this should never be reached')
yield
testCase = GeneratorSynchronousTestCase('test_generator')
result = reporter.TestResult()
testCase.run(result)
self.assertEqual(len(result.failures), 0)
self.assertEqual(len(result.errors), 1)
self.assertIn("GeneratorSynchronousTestCase.test_generator",
result.errors[0][1].value.args[0])
self.assertIn("GeneratorSynchronousTestCase testMethod=test_generator",
result.errors[0][1].value.args[0])
self.assertIn("is a generator function and therefore will never run",
result.errors[0][1].value.args[0])
示例11: test_success
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
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.assertTrue(test.ran)
self.assertEqual(1, result.testsRun)
self.assertTrue(result.wasSuccessful())
示例12: test_failure
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
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.assertTrue(test.ran)
self.assertEqual(1, result.testsRun)
self.assertEqual(1, len(result.failures))
self.assertFalse(result.wasSuccessful())
示例13: test_setUpError
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
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.assertFalse(test.ran)
self.assertEqual(1, result.testsRun)
self.assertEqual(1, len(result.errors))
self.assertFalse(result.wasSuccessful())
示例14: test_traceback
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
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
# 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())
示例15: test_pyunitSkip
# 需要导入模块: from twisted.trial import unittest [as 别名]
# 或者: from twisted.trial.unittest import SynchronousTestCase [as 别名]
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")])