test_assertions.py 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for assertions provided by C{SynchronousTestCase} and C{TestCase},
  5. provided by L{twisted.trial.unittest}.
  6. L{TestFailureTests} demonstrates that L{SynchronousTestCase.fail} works, so that
  7. is the only method on C{twisted.trial.unittest.SynchronousTestCase} that is
  8. initially assumed to work. The test classes are arranged so that the methods
  9. demonstrated to work earlier in the file are used by those later in the file
  10. (even though the runner will probably not run the tests in this order).
  11. """
  12. from __future__ import division, absolute_import
  13. import warnings
  14. import unittest as pyunit
  15. from twisted.python.util import FancyEqMixin
  16. from twisted.python.reflect import (
  17. prefixedMethods, accumulateMethods, fullyQualifiedName)
  18. from twisted.python.deprecate import deprecated
  19. from incremental import Version, getVersionString
  20. from twisted.python.failure import Failure
  21. from twisted.trial import unittest
  22. from twisted.internet.defer import Deferred, fail, succeed
  23. class MockEquality(FancyEqMixin, object):
  24. compareAttributes = ("name",)
  25. def __init__(self, name):
  26. self.name = name
  27. def __repr__(self):
  28. return "MockEquality(%s)" % (self.name,)
  29. class ComparisonError(object):
  30. """
  31. An object which raises exceptions from its comparison methods.
  32. """
  33. def _error(self, other):
  34. raise ValueError("Comparison is broken")
  35. __eq__ = __ne__ = _error
  36. class TestFailureTests(pyunit.TestCase):
  37. """
  38. Tests for the most basic functionality of L{SynchronousTestCase}, for
  39. failing tests.
  40. This class contains tests to demonstrate that L{SynchronousTestCase.fail}
  41. can be used to fail a test, and that that failure is reflected in the test
  42. result object. This should be sufficient functionality so that further
  43. tests can be built on L{SynchronousTestCase} instead of
  44. L{unittest.TestCase}. This depends on L{unittest.TestCase} working.
  45. """
  46. class FailingTest(unittest.SynchronousTestCase):
  47. def test_fails(self):
  48. self.fail("This test fails.")
  49. def setUp(self):
  50. """
  51. Load a suite of one test which can be used to exercise the failure
  52. handling behavior.
  53. """
  54. components = [
  55. __name__, self.__class__.__name__, self.FailingTest.__name__]
  56. self.loader = pyunit.TestLoader()
  57. self.suite = self.loader.loadTestsFromName(".".join(components))
  58. self.test = list(self.suite)[0]
  59. def test_fail(self):
  60. """
  61. L{SynchronousTestCase.fail} raises
  62. L{SynchronousTestCase.failureException} with the given argument.
  63. """
  64. try:
  65. self.test.fail("failed")
  66. except self.test.failureException as result:
  67. self.assertEqual("failed", str(result))
  68. else:
  69. self.fail(
  70. "SynchronousTestCase.fail method did not raise "
  71. "SynchronousTestCase.failureException")
  72. def test_failingExceptionFails(self):
  73. """
  74. When a test method raises L{SynchronousTestCase.failureException}, the test is
  75. marked as having failed on the L{TestResult}.
  76. """
  77. result = pyunit.TestResult()
  78. self.suite.run(result)
  79. self.assertFalse(result.wasSuccessful())
  80. self.assertEqual(result.errors, [])
  81. self.assertEqual(len(result.failures), 1)
  82. self.assertEqual(result.failures[0][0], self.test)
  83. class AssertFalseTests(unittest.SynchronousTestCase):
  84. """
  85. Tests for L{SynchronousTestCase}'s C{assertFalse} and C{failIf} assertion
  86. methods.
  87. This is pretty paranoid. Still, a certain paranoia is healthy if you
  88. are testing a unit testing framework.
  89. @note: As of 11.2, C{assertFalse} is preferred over C{failIf}.
  90. """
  91. def _assertFalseFalse(self, method):
  92. """
  93. Perform the positive case test for C{failIf} or C{assertFalse}.
  94. @param method: The test method to test.
  95. """
  96. for notTrue in [0, 0.0, False, None, (), []]:
  97. result = method(notTrue, "failed on %r" % (notTrue,))
  98. if result != notTrue:
  99. self.fail("Did not return argument %r" % (notTrue,))
  100. def _assertFalseTrue(self, method):
  101. """
  102. Perform the negative case test for C{failIf} or C{assertFalse}.
  103. @param method: The test method to test.
  104. """
  105. for true in [1, True, 'cat', [1,2], (3,4)]:
  106. try:
  107. method(true, "failed on %r" % (true,))
  108. except self.failureException as e:
  109. self.assertIn(
  110. "failed on %r" % (true,), str(e),
  111. "Raised incorrect exception on %r: %r" % (true, e)
  112. )
  113. else:
  114. self.fail(
  115. "Call to %s(%r) didn't fail" % (method.__name__, true,)
  116. )
  117. def test_failIfFalse(self):
  118. """
  119. L{SynchronousTestCase.failIf} returns its argument if its argument is
  120. not considered true.
  121. """
  122. self._assertFalseFalse(self.failIf)
  123. def test_assertFalseFalse(self):
  124. """
  125. L{SynchronousTestCase.assertFalse} returns its argument if its argument
  126. is not considered true.
  127. """
  128. self._assertFalseFalse(self.assertFalse)
  129. def test_failIfTrue(self):
  130. """
  131. L{SynchronousTestCase.failIf} raises
  132. L{SynchronousTestCase.failureException} if its argument is considered
  133. true.
  134. """
  135. self._assertFalseTrue(self.failIf)
  136. def test_assertFalseTrue(self):
  137. """
  138. L{SynchronousTestCase.assertFalse} raises
  139. L{SynchronousTestCase.failureException} if its argument is considered
  140. true.
  141. """
  142. self._assertFalseTrue(self.assertFalse)
  143. class AssertTrueTests(unittest.SynchronousTestCase):
  144. """
  145. Tests for L{SynchronousTestCase}'s C{assertTrue} and C{failUnless} assertion
  146. methods.
  147. This is pretty paranoid. Still, a certain paranoia is healthy if you
  148. are testing a unit testing framework.
  149. @note: As of 11.2, C{assertTrue} is preferred over C{failUnless}.
  150. """
  151. def _assertTrueFalse(self, method):
  152. """
  153. Perform the negative case test for C{assertTrue} and C{failUnless}.
  154. @param method: The test method to test.
  155. """
  156. for notTrue in [0, 0.0, False, None, (), []]:
  157. try:
  158. method(notTrue, "failed on %r" % (notTrue,))
  159. except self.failureException as e:
  160. self.assertIn(
  161. "failed on %r" % (notTrue,), str(e),
  162. "Raised incorrect exception on %r: %r" % (notTrue, e)
  163. )
  164. else:
  165. self.fail(
  166. "Call to %s(%r) didn't fail" % (method.__name__, notTrue,)
  167. )
  168. def _assertTrueTrue(self, method):
  169. """
  170. Perform the positive case test for C{assertTrue} and C{failUnless}.
  171. @param method: The test method to test.
  172. """
  173. for true in [1, True, 'cat', [1,2], (3,4)]:
  174. result = method(true, "failed on %r" % (true,))
  175. if result != true:
  176. self.fail("Did not return argument %r" % (true,))
  177. def test_assertTrueFalse(self):
  178. """
  179. L{SynchronousTestCase.assertTrue} raises
  180. L{SynchronousTestCase.failureException} if its argument is not
  181. considered true.
  182. """
  183. self._assertTrueFalse(self.assertTrue)
  184. def test_failUnlessFalse(self):
  185. """
  186. L{SynchronousTestCase.failUnless} raises
  187. L{SynchronousTestCase.failureException} if its argument is not
  188. considered true.
  189. """
  190. self._assertTrueFalse(self.failUnless)
  191. def test_assertTrueTrue(self):
  192. """
  193. L{SynchronousTestCase.assertTrue} returns its argument if its argument
  194. is considered true.
  195. """
  196. self._assertTrueTrue(self.assertTrue)
  197. def test_failUnlessTrue(self):
  198. """
  199. L{SynchronousTestCase.failUnless} returns its argument if its argument
  200. is considered true.
  201. """
  202. self._assertTrueTrue(self.failUnless)
  203. class SynchronousAssertionsTests(unittest.SynchronousTestCase):
  204. """
  205. Tests for L{SynchronousTestCase}'s assertion methods. That is, failUnless*,
  206. failIf*, assert* (not covered by other more specific test classes).
  207. Note: As of 11.2, assertEqual is preferred over the failUnlessEqual(s)
  208. variants. Tests have been modified to reflect this preference.
  209. This is pretty paranoid. Still, a certain paranoia is healthy if you are
  210. testing a unit testing framework.
  211. """
  212. def _testEqualPair(self, first, second):
  213. x = self.assertEqual(first, second)
  214. if x != first:
  215. self.fail("assertEqual should return first parameter")
  216. def _testUnequalPair(self, first, second):
  217. """
  218. Assert that when called with unequal arguments, C{assertEqual} raises a
  219. failure exception with the same message as the standard library
  220. C{assertEqual} would have raised.
  221. """
  222. raised = False
  223. try:
  224. self.assertEqual(first, second)
  225. except self.failureException as ourFailure:
  226. case = pyunit.TestCase("setUp")
  227. try:
  228. case.assertEqual(first, second)
  229. except case.failureException as theirFailure:
  230. raised = True
  231. got = str(ourFailure)
  232. expected = str(theirFailure)
  233. if expected != got:
  234. self.fail("Expected: %r; Got: %r" % (expected, got))
  235. if not raised:
  236. self.fail(
  237. "Call to assertEqual(%r, %r) didn't fail" % (first, second)
  238. )
  239. def test_assertEqual_basic(self):
  240. self._testEqualPair('cat', 'cat')
  241. self._testUnequalPair('cat', 'dog')
  242. self._testEqualPair([1], [1])
  243. self._testUnequalPair([1], 'orange')
  244. def test_assertEqual_custom(self):
  245. x = MockEquality('first')
  246. y = MockEquality('second')
  247. z = MockEquality('first')
  248. self._testEqualPair(x, x)
  249. self._testEqualPair(x, z)
  250. self._testUnequalPair(x, y)
  251. self._testUnequalPair(y, z)
  252. def test_assertEqualMessage(self):
  253. """
  254. When a message is passed to L{assertEqual} it is included in the error
  255. message.
  256. """
  257. message = 'message'
  258. exception = self.assertRaises(
  259. self.failureException, self.assertEqual,
  260. 'foo', 'bar', message
  261. )
  262. self.assertIn(message, str(exception))
  263. def test_assertEqualNoneMessage(self):
  264. """
  265. If a message is specified as L{None}, it is not included in the error
  266. message of L{assertEqual}.
  267. """
  268. exceptionForNone = self.assertRaises(
  269. self.failureException, self.assertEqual, 'foo', 'bar', None
  270. )
  271. exceptionWithout = self.assertRaises(
  272. self.failureException, self.assertEqual, 'foo', 'bar'
  273. )
  274. self.assertEqual(str(exceptionWithout), str(exceptionForNone))
  275. def test_assertEqual_incomparable(self):
  276. apple = ComparisonError()
  277. orange = ["orange"]
  278. try:
  279. self.assertEqual(apple, orange)
  280. except self.failureException:
  281. self.fail("Fail raised when ValueError ought to have been raised.")
  282. except ValueError:
  283. # good. error not swallowed
  284. pass
  285. else:
  286. self.fail("Comparing %r and %r should have raised an exception"
  287. % (apple, orange))
  288. def _raiseError(self, error):
  289. raise error
  290. def test_failUnlessRaises_expected(self):
  291. x = self.failUnlessRaises(ValueError, self._raiseError, ValueError)
  292. self.assertTrue(isinstance(x, ValueError),
  293. "Expect failUnlessRaises to return instance of raised "
  294. "exception.")
  295. def test_failUnlessRaises_unexpected(self):
  296. try:
  297. self.failUnlessRaises(ValueError, self._raiseError, TypeError)
  298. except TypeError:
  299. self.fail("failUnlessRaises shouldn't re-raise unexpected "
  300. "exceptions")
  301. except self.failureException:
  302. # what we expect
  303. pass
  304. else:
  305. self.fail("Expected exception wasn't raised. Should have failed")
  306. def test_failUnlessRaises_noException(self):
  307. returnValue = 3
  308. try:
  309. self.failUnlessRaises(ValueError, lambda : returnValue)
  310. except self.failureException as e:
  311. self.assertEqual(str(e),
  312. 'ValueError not raised (3 returned)')
  313. else:
  314. self.fail("Exception not raised. Should have failed")
  315. def test_failUnlessRaises_failureException(self):
  316. x = self.failUnlessRaises(self.failureException, self._raiseError,
  317. self.failureException)
  318. self.assertTrue(isinstance(x, self.failureException),
  319. "Expected %r instance to be returned"
  320. % (self.failureException,))
  321. try:
  322. x = self.failUnlessRaises(self.failureException, self._raiseError,
  323. ValueError)
  324. except self.failureException:
  325. # what we expect
  326. pass
  327. else:
  328. self.fail("Should have raised exception")
  329. def test_assertRaisesContextExpected(self):
  330. """
  331. If C{assertRaises} is used to create a context manager and an exception
  332. is raised from the body of the C{with} statement then the context
  333. manager's C{exception} attribute is set to the exception that was
  334. raised.
  335. """
  336. exception = ValueError('marker')
  337. with self.assertRaises(ValueError) as context:
  338. raise exception
  339. self.assertIs(exception, context.exception)
  340. def test_assertRaisesContextUnexpected(self):
  341. """
  342. If C{assertRaises} is used to create a context manager and the wrong
  343. exception type is raised from the body of the C{with} statement then
  344. the C{with} statement raises C{failureException} describing the
  345. mismatch.
  346. """
  347. try:
  348. with self.assertRaises(ValueError):
  349. raise TypeError('marker')
  350. except self.failureException as exception:
  351. message = str(exception)
  352. expected = (
  353. "{type} raised instead of ValueError:\n"
  354. " Traceback").format(type=fullyQualifiedName(TypeError))
  355. self.assertTrue(
  356. message.startswith(expected),
  357. "Exception message did not begin with expected information: "
  358. "{0}".format(message))
  359. else:
  360. self.fail(
  361. "Mismatched exception type should have caused test failure.")
  362. def test_assertRaisesContextNoException(self):
  363. """
  364. If C{assertRaises} is used to create a context manager and no exception
  365. is raised from the body of the C{with} statement then the C{with}
  366. statement raises C{failureException} describing the lack of exception.
  367. """
  368. try:
  369. with self.assertRaises(ValueError):
  370. # No exception is raised.
  371. pass
  372. except self.failureException as exception:
  373. message = str(exception)
  374. # `(None returned)` text is here for backward compatibility and should
  375. # be ignored for context manager use case.
  376. self.assertEqual(message, "ValueError not raised (None returned)")
  377. else:
  378. self.fail("Non-exception result should have caused test failure.")
  379. def test_brokenName(self):
  380. """
  381. If the exception type passed to C{assertRaises} does not have a
  382. C{__name__} then the context manager still manages to construct a
  383. descriptive string for it.
  384. """
  385. try:
  386. with self.assertRaises((ValueError, TypeError)):
  387. # Just some other kind of exception
  388. raise AttributeError()
  389. except self.failureException as exception:
  390. message = str(exception)
  391. valueError = "ValueError" not in message
  392. typeError = "TypeError" not in message
  393. errors = []
  394. if valueError:
  395. errors.append("expected ValueError in exception message")
  396. if typeError:
  397. errors.append("expected TypeError in exception message")
  398. if errors:
  399. self.fail("; ".join(errors), "message = {0}".format(message))
  400. else:
  401. self.fail(
  402. "Mismatched exception type should have caused test failure.")
  403. def test_failIfEqual_basic(self):
  404. x, y, z = [1], [2], [1]
  405. ret = self.failIfEqual(x, y)
  406. self.assertEqual(ret, x,
  407. "failIfEqual should return first parameter")
  408. self.failUnlessRaises(self.failureException,
  409. self.failIfEqual, x, x)
  410. self.failUnlessRaises(self.failureException,
  411. self.failIfEqual, x, z)
  412. def test_failIfEqual_customEq(self):
  413. x = MockEquality('first')
  414. y = MockEquality('second')
  415. z = MockEquality('fecund')
  416. ret = self.failIfEqual(x, y)
  417. self.assertEqual(ret, x,
  418. "failIfEqual should return first parameter")
  419. self.failUnlessRaises(self.failureException,
  420. self.failIfEqual, x, x)
  421. self.failIfEqual(x, z, "__ne__ should make these not equal")
  422. def test_failIfIdenticalPositive(self):
  423. """
  424. C{failIfIdentical} returns its first argument if its first and second
  425. arguments are not the same object.
  426. """
  427. x = object()
  428. y = object()
  429. result = self.failIfIdentical(x, y)
  430. self.assertEqual(x, result)
  431. def test_failIfIdenticalNegative(self):
  432. """
  433. C{failIfIdentical} raises C{failureException} if its first and second
  434. arguments are the same object.
  435. """
  436. x = object()
  437. self.failUnlessRaises(self.failureException,
  438. self.failIfIdentical, x, x)
  439. def test_failUnlessIdentical(self):
  440. x, y, z = [1], [1], [2]
  441. ret = self.failUnlessIdentical(x, x)
  442. self.assertEqual(ret, x,
  443. 'failUnlessIdentical should return first '
  444. 'parameter')
  445. self.failUnlessRaises(self.failureException,
  446. self.failUnlessIdentical, x, y)
  447. self.failUnlessRaises(self.failureException,
  448. self.failUnlessIdentical, x, z)
  449. def test_failUnlessApproximates(self):
  450. x, y, z = 1.0, 1.1, 1.2
  451. self.failUnlessApproximates(x, x, 0.2)
  452. ret = self.failUnlessApproximates(x, y, 0.2)
  453. self.assertEqual(ret, x, "failUnlessApproximates should return "
  454. "first parameter")
  455. self.failUnlessRaises(self.failureException,
  456. self.failUnlessApproximates, x, z, 0.1)
  457. self.failUnlessRaises(self.failureException,
  458. self.failUnlessApproximates, x, y, 0.1)
  459. def test_failUnlessAlmostEqual(self):
  460. precision = 5
  461. x = 8.000001
  462. y = 8.00001
  463. z = 8.000002
  464. self.failUnlessAlmostEqual(x, x, precision)
  465. ret = self.failUnlessAlmostEqual(x, z, precision)
  466. self.assertEqual(ret, x, "failUnlessAlmostEqual should return "
  467. "first parameter (%r, %r)" % (ret, x))
  468. self.failUnlessRaises(self.failureException,
  469. self.failUnlessAlmostEqual, x, y, precision)
  470. def test_failIfAlmostEqual(self):
  471. precision = 5
  472. x = 8.000001
  473. y = 8.00001
  474. z = 8.000002
  475. ret = self.failIfAlmostEqual(x, y, precision)
  476. self.assertEqual(ret, x, "failIfAlmostEqual should return "
  477. "first parameter (%r, %r)" % (ret, x))
  478. self.failUnlessRaises(self.failureException,
  479. self.failIfAlmostEqual, x, x, precision)
  480. self.failUnlessRaises(self.failureException,
  481. self.failIfAlmostEqual, x, z, precision)
  482. def test_failUnlessSubstring(self):
  483. x = "cat"
  484. y = "the dog sat"
  485. z = "the cat sat"
  486. self.failUnlessSubstring(x, x)
  487. ret = self.failUnlessSubstring(x, z)
  488. self.assertEqual(ret, x, 'should return first parameter')
  489. self.failUnlessRaises(self.failureException,
  490. self.failUnlessSubstring, x, y)
  491. self.failUnlessRaises(self.failureException,
  492. self.failUnlessSubstring, z, x)
  493. def test_failIfSubstring(self):
  494. x = "cat"
  495. y = "the dog sat"
  496. z = "the cat sat"
  497. self.failIfSubstring(z, x)
  498. ret = self.failIfSubstring(x, y)
  499. self.assertEqual(ret, x, 'should return first parameter')
  500. self.failUnlessRaises(self.failureException,
  501. self.failIfSubstring, x, x)
  502. self.failUnlessRaises(self.failureException,
  503. self.failIfSubstring, x, z)
  504. def test_assertIs(self):
  505. """
  506. L{assertIs} passes if two objects are identical.
  507. """
  508. a = MockEquality("first")
  509. self.assertIs(a, a)
  510. def test_assertIsError(self):
  511. """
  512. L{assertIs} fails if two objects are not identical.
  513. """
  514. a, b = MockEquality("first"), MockEquality("first")
  515. self.assertEqual(a, b)
  516. self.assertRaises(self.failureException, self.assertIs, a, b)
  517. def test_assertIsNot(self):
  518. """
  519. L{assertIsNot} passes if two objects are not identical.
  520. """
  521. a, b = MockEquality("first"), MockEquality("first")
  522. self.assertEqual(a, b)
  523. self.assertIsNot(a, b)
  524. def test_assertIsNotError(self):
  525. """
  526. L{assertIsNot} fails if two objects are identical.
  527. """
  528. a = MockEquality("first")
  529. self.assertRaises(self.failureException, self.assertIsNot, a, a)
  530. def test_assertIsInstance(self):
  531. """
  532. Test a true condition of assertIsInstance.
  533. """
  534. A = type('A', (object,), {})
  535. a = A()
  536. self.assertIsInstance(a, A)
  537. def test_assertIsInstanceMultipleClasses(self):
  538. """
  539. Test a true condition of assertIsInstance with multiple classes.
  540. """
  541. A = type('A', (object,), {})
  542. B = type('B', (object,), {})
  543. a = A()
  544. self.assertIsInstance(a, (A, B))
  545. def test_assertIsInstanceError(self):
  546. """
  547. Test an error with assertIsInstance.
  548. """
  549. A = type('A', (object,), {})
  550. B = type('B', (object,), {})
  551. a = A()
  552. self.assertRaises(self.failureException, self.assertIsInstance, a, B)
  553. def test_assertIsInstanceErrorMultipleClasses(self):
  554. """
  555. Test an error with assertIsInstance and multiple classes.
  556. """
  557. A = type('A', (object,), {})
  558. B = type('B', (object,), {})
  559. C = type('C', (object,), {})
  560. a = A()
  561. self.assertRaises(self.failureException, self.assertIsInstance, a, (B, C))
  562. def test_assertIsInstanceCustomMessage(self):
  563. """
  564. If L{TestCase.assertIsInstance} is passed a custom message as its 3rd
  565. argument, the message is included in the failure exception raised when
  566. the assertion fails.
  567. """
  568. exc = self.assertRaises(
  569. self.failureException,
  570. self.assertIsInstance, 3, str, "Silly assertion")
  571. self.assertIn("Silly assertion", str(exc))
  572. def test_assertNotIsInstance(self):
  573. """
  574. Test a true condition of assertNotIsInstance.
  575. """
  576. A = type('A', (object,), {})
  577. B = type('B', (object,), {})
  578. a = A()
  579. self.assertNotIsInstance(a, B)
  580. def test_assertNotIsInstanceMultipleClasses(self):
  581. """
  582. Test a true condition of assertNotIsInstance and multiple classes.
  583. """
  584. A = type('A', (object,), {})
  585. B = type('B', (object,), {})
  586. C = type('C', (object,), {})
  587. a = A()
  588. self.assertNotIsInstance(a, (B, C))
  589. def test_assertNotIsInstanceError(self):
  590. """
  591. Test an error with assertNotIsInstance.
  592. """
  593. A = type('A', (object,), {})
  594. a = A()
  595. error = self.assertRaises(self.failureException,
  596. self.assertNotIsInstance, a, A)
  597. self.assertEqual(str(error), "%r is an instance of %s" % (a, A))
  598. def test_assertNotIsInstanceErrorMultipleClasses(self):
  599. """
  600. Test an error with assertNotIsInstance and multiple classes.
  601. """
  602. A = type('A', (object,), {})
  603. B = type('B', (object,), {})
  604. a = A()
  605. self.assertRaises(self.failureException, self.assertNotIsInstance, a, (A, B))
  606. def test_assertDictEqual(self):
  607. """
  608. L{twisted.trial.unittest.TestCase} supports the C{assertDictEqual}
  609. method inherited from the standard library in Python 2.7.
  610. """
  611. self.assertDictEqual({'a': 1}, {'a': 1})
  612. if getattr(unittest.SynchronousTestCase, 'assertDictEqual', None) is None:
  613. test_assertDictEqual.skip = (
  614. "assertDictEqual is not available on this version of Python")
  615. class WarningAssertionTests(unittest.SynchronousTestCase):
  616. def test_assertWarns(self):
  617. """
  618. Test basic assertWarns report.
  619. """
  620. def deprecated(a):
  621. warnings.warn("Woo deprecated", category=DeprecationWarning)
  622. return a
  623. r = self.assertWarns(DeprecationWarning, "Woo deprecated", __file__,
  624. deprecated, 123)
  625. self.assertEqual(r, 123)
  626. def test_assertWarnsRegistryClean(self):
  627. """
  628. Test that assertWarns cleans the warning registry, so the warning is
  629. not swallowed the second time.
  630. """
  631. def deprecated(a):
  632. warnings.warn("Woo deprecated", category=DeprecationWarning)
  633. return a
  634. r1 = self.assertWarns(DeprecationWarning, "Woo deprecated", __file__,
  635. deprecated, 123)
  636. self.assertEqual(r1, 123)
  637. # The warning should be raised again
  638. r2 = self.assertWarns(DeprecationWarning, "Woo deprecated", __file__,
  639. deprecated, 321)
  640. self.assertEqual(r2, 321)
  641. def test_assertWarnsError(self):
  642. """
  643. Test assertWarns failure when no warning is generated.
  644. """
  645. def normal(a):
  646. return a
  647. self.assertRaises(self.failureException,
  648. self.assertWarns, DeprecationWarning, "Woo deprecated", __file__,
  649. normal, 123)
  650. def test_assertWarnsWrongCategory(self):
  651. """
  652. Test assertWarns failure when the category is wrong.
  653. """
  654. def deprecated(a):
  655. warnings.warn("Foo deprecated", category=DeprecationWarning)
  656. return a
  657. self.assertRaises(self.failureException,
  658. self.assertWarns, UserWarning, "Foo deprecated", __file__,
  659. deprecated, 123)
  660. def test_assertWarnsWrongMessage(self):
  661. """
  662. Test assertWarns failure when the message is wrong.
  663. """
  664. def deprecated(a):
  665. warnings.warn("Foo deprecated", category=DeprecationWarning)
  666. return a
  667. self.assertRaises(self.failureException,
  668. self.assertWarns, DeprecationWarning, "Bar deprecated", __file__,
  669. deprecated, 123)
  670. def test_assertWarnsWrongFile(self):
  671. """
  672. If the warning emitted by a function refers to a different file than is
  673. passed to C{assertWarns}, C{failureException} is raised.
  674. """
  675. def deprecated(a):
  676. # stacklevel=2 points at the direct caller of the function. The
  677. # way assertRaises is invoked below, the direct caller will be
  678. # something somewhere in trial, not something in this file. In
  679. # Python 2.5 and earlier, stacklevel of 0 resulted in a warning
  680. # pointing to the warnings module itself. Starting in Python 2.6,
  681. # stacklevel of 0 and 1 both result in a warning pointing to *this*
  682. # file, presumably due to the fact that the warn function is
  683. # implemented in C and has no convenient Python
  684. # filename/linenumber.
  685. warnings.warn(
  686. "Foo deprecated", category=DeprecationWarning, stacklevel=2)
  687. self.assertRaises(
  688. self.failureException,
  689. # Since the direct caller isn't in this file, try to assert that
  690. # the warning *does* point to this file, so that assertWarns raises
  691. # an exception.
  692. self.assertWarns, DeprecationWarning, "Foo deprecated", __file__,
  693. deprecated, 123)
  694. def test_assertWarnsOnClass(self):
  695. """
  696. Test assertWarns works when creating a class instance.
  697. """
  698. class Warn:
  699. def __init__(self):
  700. warnings.warn("Do not call me", category=RuntimeWarning)
  701. r = self.assertWarns(RuntimeWarning, "Do not call me", __file__,
  702. Warn)
  703. self.assertTrue(isinstance(r, Warn))
  704. r = self.assertWarns(RuntimeWarning, "Do not call me", __file__,
  705. Warn)
  706. self.assertTrue(isinstance(r, Warn))
  707. def test_assertWarnsOnMethod(self):
  708. """
  709. Test assertWarns works when used on an instance method.
  710. """
  711. class Warn:
  712. def deprecated(self, a):
  713. warnings.warn("Bar deprecated", category=DeprecationWarning)
  714. return a
  715. w = Warn()
  716. r = self.assertWarns(DeprecationWarning, "Bar deprecated", __file__,
  717. w.deprecated, 321)
  718. self.assertEqual(r, 321)
  719. r = self.assertWarns(DeprecationWarning, "Bar deprecated", __file__,
  720. w.deprecated, 321)
  721. self.assertEqual(r, 321)
  722. def test_assertWarnsOnCall(self):
  723. """
  724. Test assertWarns works on instance with C{__call__} method.
  725. """
  726. class Warn:
  727. def __call__(self, a):
  728. warnings.warn("Egg deprecated", category=DeprecationWarning)
  729. return a
  730. w = Warn()
  731. r = self.assertWarns(DeprecationWarning, "Egg deprecated", __file__,
  732. w, 321)
  733. self.assertEqual(r, 321)
  734. r = self.assertWarns(DeprecationWarning, "Egg deprecated", __file__,
  735. w, 321)
  736. self.assertEqual(r, 321)
  737. def test_assertWarnsFilter(self):
  738. """
  739. Test assertWarns on a warning filtered by default.
  740. """
  741. def deprecated(a):
  742. warnings.warn("Woo deprecated", category=PendingDeprecationWarning)
  743. return a
  744. r = self.assertWarns(PendingDeprecationWarning, "Woo deprecated",
  745. __file__, deprecated, 123)
  746. self.assertEqual(r, 123)
  747. def test_assertWarnsMultipleWarnings(self):
  748. """
  749. C{assertWarns} does not raise an exception if the function it is passed
  750. triggers the same warning more than once.
  751. """
  752. def deprecated():
  753. warnings.warn("Woo deprecated", category=PendingDeprecationWarning)
  754. def f():
  755. deprecated()
  756. deprecated()
  757. self.assertWarns(
  758. PendingDeprecationWarning, "Woo deprecated", __file__, f)
  759. def test_assertWarnsDifferentWarnings(self):
  760. """
  761. For now, assertWarns is unable to handle multiple different warnings,
  762. so it should raise an exception if it's the case.
  763. """
  764. def deprecated(a):
  765. warnings.warn("Woo deprecated", category=DeprecationWarning)
  766. warnings.warn("Another one", category=PendingDeprecationWarning)
  767. e = self.assertRaises(self.failureException,
  768. self.assertWarns, DeprecationWarning, "Woo deprecated",
  769. __file__, deprecated, 123)
  770. self.assertEqual(str(e), "Can't handle different warnings")
  771. def test_assertWarnsAfterUnassertedWarning(self):
  772. """
  773. Warnings emitted before L{TestCase.assertWarns} is called do not get
  774. flushed and do not alter the behavior of L{TestCase.assertWarns}.
  775. """
  776. class TheWarning(Warning):
  777. pass
  778. def f(message):
  779. warnings.warn(message, category=TheWarning)
  780. f("foo")
  781. self.assertWarns(TheWarning, "bar", __file__, f, "bar")
  782. [warning] = self.flushWarnings([f])
  783. self.assertEqual(warning['message'], "foo")
  784. class ResultOfAssertionsTests(unittest.SynchronousTestCase):
  785. """
  786. Tests for L{SynchronousTestCase.successResultOf},
  787. L{SynchronousTestCase.failureResultOf}, and
  788. L{SynchronousTestCase.assertNoResult}.
  789. """
  790. result = object()
  791. failure = Failure(Exception("Bad times"))
  792. def test_withoutSuccessResult(self):
  793. """
  794. L{SynchronousTestCase.successResultOf} raises
  795. L{SynchronousTestCase.failureException} when called with a L{Deferred}
  796. with no current result.
  797. """
  798. self.assertRaises(
  799. self.failureException, self.successResultOf, Deferred())
  800. def test_successResultOfWithFailure(self):
  801. """
  802. L{SynchronousTestCase.successResultOf} raises
  803. L{SynchronousTestCase.failureException} when called with a L{Deferred}
  804. with a failure result.
  805. """
  806. self.assertRaises(
  807. self.failureException, self.successResultOf, fail(self.failure))
  808. def test_successResultOfWithFailureHasTraceback(self):
  809. """
  810. L{SynchronousTestCase.successResultOf} raises a
  811. L{SynchronousTestCase.failureException} that has the original failure
  812. traceback when called with a L{Deferred} with a failure result.
  813. """
  814. try:
  815. self.successResultOf(fail(self.failure))
  816. except self.failureException as e:
  817. self.assertIn(self.failure.getTraceback(), str(e))
  818. def test_withoutFailureResult(self):
  819. """
  820. L{SynchronousTestCase.failureResultOf} raises
  821. L{SynchronousTestCase.failureException} when called with a L{Deferred}
  822. with no current result.
  823. """
  824. self.assertRaises(
  825. self.failureException, self.failureResultOf, Deferred())
  826. def test_failureResultOfWithSuccess(self):
  827. """
  828. L{SynchronousTestCase.failureResultOf} raises
  829. L{SynchronousTestCase.failureException} when called with a L{Deferred}
  830. with a success result.
  831. """
  832. self.assertRaises(
  833. self.failureException, self.failureResultOf, succeed(self.result))
  834. def test_failureResultOfWithWrongFailure(self):
  835. """
  836. L{SynchronousTestCase.failureResultOf} raises
  837. L{SynchronousTestCase.failureException} when called with a L{Deferred}
  838. with a failure type that was not expected.
  839. """
  840. self.assertRaises(
  841. self.failureException, self.failureResultOf, fail(self.failure),
  842. KeyError)
  843. def test_failureResultOfWithWrongFailureOneExpectedFailure(self):
  844. """
  845. L{SynchronousTestCase.failureResultOf} raises
  846. L{SynchronousTestCase.failureException} when called with a L{Deferred}
  847. with a failure type that was not expected, and the
  848. L{SynchronousTestCase.failureException} message contains the original
  849. failure traceback as well as the expected failure type
  850. """
  851. try:
  852. self.failureResultOf(fail(self.failure), KeyError)
  853. except self.failureException as e:
  854. self.assertIn(self.failure.getTraceback(), str(e))
  855. self.assertIn(
  856. "Failure of type ({0}.{1}) expected on".format(
  857. KeyError.__module__, KeyError.__name__),
  858. str(e))
  859. def test_failureResultOfWithWrongFailureMultiExpectedFailure(self):
  860. """
  861. L{SynchronousTestCase.failureResultOf} raises
  862. L{SynchronousTestCase.failureException} when called with a L{Deferred}
  863. with a failure type that was not expected, and the
  864. L{SynchronousTestCase.failureException} message contains the original
  865. failure traceback as well as the expected failure types in the error
  866. message
  867. """
  868. try:
  869. self.failureResultOf(fail(self.failure), KeyError, IOError)
  870. except self.failureException as e:
  871. self.assertIn(self.failure.getTraceback(), str(e))
  872. self.assertIn(
  873. "Failure of type ({0}.{1} or {2}.{3}) expected on".format(
  874. KeyError.__module__, KeyError.__name__,
  875. IOError.__module__, IOError.__name__),
  876. str(e))
  877. def test_withSuccessResult(self):
  878. """
  879. When passed a L{Deferred} which currently has a result (ie,
  880. L{Deferred.addCallback} would cause the added callback to be called
  881. before C{addCallback} returns), L{SynchronousTestCase.successResultOf}
  882. returns that result.
  883. """
  884. self.assertIdentical(
  885. self.result, self.successResultOf(succeed(self.result)))
  886. def test_withExpectedFailureResult(self):
  887. """
  888. When passed a L{Deferred} which currently has a L{Failure} result (ie,
  889. L{Deferred.addErrback} would cause the added errback to be called
  890. before C{addErrback} returns), L{SynchronousTestCase.failureResultOf}
  891. returns that L{Failure} if that L{Failure}'s type is expected.
  892. """
  893. self.assertIdentical(
  894. self.failure,
  895. self.failureResultOf(fail(self.failure), self.failure.type,
  896. KeyError))
  897. def test_withFailureResult(self):
  898. """
  899. When passed a L{Deferred} which currently has a L{Failure} result
  900. (ie, L{Deferred.addErrback} would cause the added errback to be called
  901. before C{addErrback} returns), L{SynchronousTestCase.failureResultOf}
  902. returns that L{Failure}.
  903. """
  904. self.assertIdentical(
  905. self.failure, self.failureResultOf(fail(self.failure)))
  906. def test_assertNoResultSuccess(self):
  907. """
  908. When passed a L{Deferred} which currently has a success result (see
  909. L{test_withSuccessResult}), L{SynchronousTestCase.assertNoResult} raises
  910. L{SynchronousTestCase.failureException}.
  911. """
  912. self.assertRaises(
  913. self.failureException, self.assertNoResult, succeed(self.result))
  914. def test_assertNoResultFailure(self):
  915. """
  916. When passed a L{Deferred} which currently has a failure result (see
  917. L{test_withFailureResult}), L{SynchronousTestCase.assertNoResult} raises
  918. L{SynchronousTestCase.failureException}.
  919. """
  920. self.assertRaises(
  921. self.failureException, self.assertNoResult, fail(self.failure))
  922. def test_assertNoResult(self):
  923. """
  924. When passed a L{Deferred} with no current result,
  925. """
  926. self.assertNoResult(Deferred())
  927. def test_assertNoResultPropagatesSuccess(self):
  928. """
  929. When passed a L{Deferred} with no current result, which is then
  930. fired with a success result, L{SynchronousTestCase.assertNoResult}
  931. doesn't modify the result of the L{Deferred}.
  932. """
  933. d = Deferred()
  934. self.assertNoResult(d)
  935. d.callback(self.result)
  936. self.assertEqual(self.result, self.successResultOf(d))
  937. def test_assertNoResultPropagatesLaterFailure(self):
  938. """
  939. When passed a L{Deferred} with no current result, which is then
  940. fired with a L{Failure} result, L{SynchronousTestCase.assertNoResult}
  941. doesn't modify the result of the L{Deferred}.
  942. """
  943. d = Deferred()
  944. self.assertNoResult(d)
  945. d.errback(self.failure)
  946. self.assertEqual(self.failure, self.failureResultOf(d))
  947. def test_assertNoResultSwallowsImmediateFailure(self):
  948. """
  949. When passed a L{Deferred} which currently has a L{Failure} result,
  950. L{SynchronousTestCase.assertNoResult} changes the result of the
  951. L{Deferred} to a success.
  952. """
  953. d = fail(self.failure)
  954. try:
  955. self.assertNoResult(d)
  956. except self.failureException:
  957. pass
  958. self.assertEqual(None, self.successResultOf(d))
  959. class AssertionNamesTests(unittest.SynchronousTestCase):
  960. """
  961. Tests for consistency of naming within TestCase assertion methods
  962. """
  963. def _getAsserts(self):
  964. dct = {}
  965. accumulateMethods(self, dct, 'assert')
  966. return [ dct[k] for k in dct if not k.startswith('Not') and k != '_' ]
  967. def _name(self, x):
  968. return x.__name__
  969. def test_failUnlessMatchesAssert(self):
  970. """
  971. The C{failUnless*} test methods are a subset of the C{assert*} test
  972. methods. This is intended to ensure that methods using the
  973. I{failUnless} naming scheme are not added without corresponding methods
  974. using the I{assert} naming scheme. The I{assert} naming scheme is
  975. preferred, and new I{assert}-prefixed methods may be added without
  976. corresponding I{failUnless}-prefixed methods.
  977. """
  978. asserts = set(self._getAsserts())
  979. failUnlesses = set(prefixedMethods(self, 'failUnless'))
  980. self.assertEqual(
  981. failUnlesses, asserts.intersection(failUnlesses))
  982. def test_failIf_matches_assertNot(self):
  983. asserts = prefixedMethods(unittest.SynchronousTestCase, 'assertNot')
  984. failIfs = prefixedMethods(unittest.SynchronousTestCase, 'failIf')
  985. self.assertEqual(sorted(asserts, key=self._name),
  986. sorted(failIfs, key=self._name))
  987. def test_equalSpelling(self):
  988. for name, value in vars(self).items():
  989. if not callable(value):
  990. continue
  991. if name.endswith('Equal'):
  992. self.assertTrue(hasattr(self, name+'s'),
  993. "%s but no %ss" % (name, name))
  994. self.assertEqual(value, getattr(self, name+'s'))
  995. if name.endswith('Equals'):
  996. self.assertTrue(hasattr(self, name[:-1]),
  997. "%s but no %s" % (name, name[:-1]))
  998. self.assertEqual(value, getattr(self, name[:-1]))
  999. class CallDeprecatedTests(unittest.SynchronousTestCase):
  1000. """
  1001. Test use of the L{SynchronousTestCase.callDeprecated} method with version objects.
  1002. """
  1003. version = Version('Twisted', 8, 0, 0)
  1004. def test_callDeprecatedSuppressesWarning(self):
  1005. """
  1006. callDeprecated calls a deprecated callable, suppressing the
  1007. deprecation warning.
  1008. """
  1009. self.callDeprecated(self.version, oldMethod, 'foo')
  1010. self.assertEqual(
  1011. self.flushWarnings(), [], "No warnings should be shown")
  1012. def test_callDeprecatedCallsFunction(self):
  1013. """
  1014. L{callDeprecated} actually calls the callable passed to it, and
  1015. forwards the result.
  1016. """
  1017. result = self.callDeprecated(self.version, oldMethod, 'foo')
  1018. self.assertEqual('foo', result)
  1019. def test_failsWithoutDeprecation(self):
  1020. """
  1021. L{callDeprecated} raises a test failure if the callable is not
  1022. deprecated.
  1023. """
  1024. def notDeprecated():
  1025. pass
  1026. exception = self.assertRaises(
  1027. self.failureException,
  1028. self.callDeprecated, self.version, notDeprecated)
  1029. self.assertEqual(
  1030. "%r is not deprecated." % notDeprecated, str(exception))
  1031. def test_failsWithIncorrectDeprecation(self):
  1032. """
  1033. callDeprecated raises a test failure if the callable was deprecated
  1034. at a different version to the one expected.
  1035. """
  1036. differentVersion = Version('Foo', 1, 2, 3)
  1037. exception = self.assertRaises(
  1038. self.failureException,
  1039. self.callDeprecated,
  1040. differentVersion, oldMethod, 'foo')
  1041. self.assertIn(getVersionString(self.version), str(exception))
  1042. self.assertIn(getVersionString(differentVersion), str(exception))
  1043. def test_nestedDeprecation(self):
  1044. """
  1045. L{callDeprecated} ignores all deprecations apart from the first.
  1046. Multiple warnings are generated when a deprecated function calls
  1047. another deprecated function. The first warning is the one generated by
  1048. the explicitly called function. That's the warning that we care about.
  1049. """
  1050. differentVersion = Version('Foo', 1, 2, 3)
  1051. def nestedDeprecation(*args):
  1052. return oldMethod(*args)
  1053. nestedDeprecation = deprecated(differentVersion)(nestedDeprecation)
  1054. self.callDeprecated(differentVersion, nestedDeprecation, 24)
  1055. # The oldMethod deprecation should have been emitted too, not captured
  1056. # by callDeprecated. Flush it now to make sure it did happen and to
  1057. # prevent it from showing up on stdout.
  1058. warningsShown = self.flushWarnings()
  1059. self.assertEqual(len(warningsShown), 1)
  1060. def test_callDeprecationWithMessage(self):
  1061. """
  1062. L{callDeprecated} can take a message argument used to check the warning
  1063. emitted.
  1064. """
  1065. self.callDeprecated((self.version, "newMethod"),
  1066. oldMethodReplaced, 1)
  1067. def test_callDeprecationWithWrongMessage(self):
  1068. """
  1069. If the message passed to L{callDeprecated} doesn't match,
  1070. L{callDeprecated} raises a test failure.
  1071. """
  1072. exception = self.assertRaises(
  1073. self.failureException,
  1074. self.callDeprecated,
  1075. (self.version, "something.wrong"),
  1076. oldMethodReplaced, 1)
  1077. self.assertIn(getVersionString(self.version), str(exception))
  1078. self.assertIn("please use newMethod instead", str(exception))
  1079. @deprecated(CallDeprecatedTests.version)
  1080. def oldMethod(x):
  1081. """
  1082. Deprecated method for testing.
  1083. """
  1084. return x
  1085. @deprecated(CallDeprecatedTests.version, replacement="newMethod")
  1086. def oldMethodReplaced(x):
  1087. """
  1088. Another deprecated method, which has been deprecated in favor of the
  1089. mythical 'newMethod'.
  1090. """
  1091. return 2 * x