12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820 |
- import contextlib
- import difflib
- import pprint
- import pickle
- import re
- import sys
- import logging
- import warnings
- import weakref
- import inspect
- from copy import deepcopy
- from test import support
- import unittest
- from unittest.test.support import (
- TestEquality, TestHashing, LoggingResult, LegacyLoggingResult,
- ResultWithNoStartTestRunStopTestRun
- )
- from test.support import captured_stderr
- log_foo = logging.getLogger('foo')
- log_foobar = logging.getLogger('foo.bar')
- log_quux = logging.getLogger('quux')
- class Test(object):
- "Keep these TestCase classes out of the main namespace"
- class Foo(unittest.TestCase):
- def runTest(self): pass
- def test1(self): pass
- class Bar(Foo):
- def test2(self): pass
- class LoggingTestCase(unittest.TestCase):
- """A test case which logs its calls."""
- def __init__(self, events):
- super(Test.LoggingTestCase, self).__init__('test')
- self.events = events
- def setUp(self):
- self.events.append('setUp')
- def test(self):
- self.events.append('test')
- def tearDown(self):
- self.events.append('tearDown')
- class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
- ### Set up attributes used by inherited tests
- ################################################################
- # Used by TestHashing.test_hash and TestEquality.test_eq
- eq_pairs = [(Test.Foo('test1'), Test.Foo('test1'))]
- # Used by TestEquality.test_ne
- ne_pairs = [(Test.Foo('test1'), Test.Foo('runTest')),
- (Test.Foo('test1'), Test.Bar('test1')),
- (Test.Foo('test1'), Test.Bar('test2'))]
- ################################################################
- ### /Set up attributes used by inherited tests
- # "class TestCase([methodName])"
- # ...
- # "Each instance of TestCase will run a single test method: the
- # method named methodName."
- # ...
- # "methodName defaults to "runTest"."
- #
- # Make sure it really is optional, and that it defaults to the proper
- # thing.
- def test_init__no_test_name(self):
- class Test(unittest.TestCase):
- def runTest(self): raise MyException()
- def test(self): pass
- self.assertEqual(Test().id()[-13:], '.Test.runTest')
- # test that TestCase can be instantiated with no args
- # primarily for use at the interactive interpreter
- test = unittest.TestCase()
- test.assertEqual(3, 3)
- with test.assertRaises(test.failureException):
- test.assertEqual(3, 2)
- with self.assertRaises(AttributeError):
- test.run()
- # "class TestCase([methodName])"
- # ...
- # "Each instance of TestCase will run a single test method: the
- # method named methodName."
- def test_init__test_name__valid(self):
- class Test(unittest.TestCase):
- def runTest(self): raise MyException()
- def test(self): pass
- self.assertEqual(Test('test').id()[-10:], '.Test.test')
- # "class TestCase([methodName])"
- # ...
- # "Each instance of TestCase will run a single test method: the
- # method named methodName."
- def test_init__test_name__invalid(self):
- class Test(unittest.TestCase):
- def runTest(self): raise MyException()
- def test(self): pass
- try:
- Test('testfoo')
- except ValueError:
- pass
- else:
- self.fail("Failed to raise ValueError")
- # "Return the number of tests represented by the this test object. For
- # TestCase instances, this will always be 1"
- def test_countTestCases(self):
- class Foo(unittest.TestCase):
- def test(self): pass
- self.assertEqual(Foo('test').countTestCases(), 1)
- # "Return the default type of test result object to be used to run this
- # test. For TestCase instances, this will always be
- # unittest.TestResult; subclasses of TestCase should
- # override this as necessary."
- def test_defaultTestResult(self):
- class Foo(unittest.TestCase):
- def runTest(self):
- pass
- result = Foo().defaultTestResult()
- self.assertEqual(type(result), unittest.TestResult)
- # "When a setUp() method is defined, the test runner will run that method
- # prior to each test. Likewise, if a tearDown() method is defined, the
- # test runner will invoke that method after each test. In the example,
- # setUp() was used to create a fresh sequence for each test."
- #
- # Make sure the proper call order is maintained, even if setUp() raises
- # an exception.
- def test_run_call_order__error_in_setUp(self):
- events = []
- result = LoggingResult(events)
- class Foo(Test.LoggingTestCase):
- def setUp(self):
- super(Foo, self).setUp()
- raise RuntimeError('raised by Foo.setUp')
- Foo(events).run(result)
- expected = ['startTest', 'setUp', 'addError', 'stopTest']
- self.assertEqual(events, expected)
- # "With a temporary result stopTestRun is called when setUp errors.
- def test_run_call_order__error_in_setUp_default_result(self):
- events = []
- class Foo(Test.LoggingTestCase):
- def defaultTestResult(self):
- return LoggingResult(self.events)
- def setUp(self):
- super(Foo, self).setUp()
- raise RuntimeError('raised by Foo.setUp')
- Foo(events).run()
- expected = ['startTestRun', 'startTest', 'setUp', 'addError',
- 'stopTest', 'stopTestRun']
- self.assertEqual(events, expected)
- # "When a setUp() method is defined, the test runner will run that method
- # prior to each test. Likewise, if a tearDown() method is defined, the
- # test runner will invoke that method after each test. In the example,
- # setUp() was used to create a fresh sequence for each test."
- #
- # Make sure the proper call order is maintained, even if the test raises
- # an error (as opposed to a failure).
- def test_run_call_order__error_in_test(self):
- events = []
- result = LoggingResult(events)
- class Foo(Test.LoggingTestCase):
- def test(self):
- super(Foo, self).test()
- raise RuntimeError('raised by Foo.test')
- expected = ['startTest', 'setUp', 'test', 'tearDown',
- 'addError', 'stopTest']
- Foo(events).run(result)
- self.assertEqual(events, expected)
- # "With a default result, an error in the test still results in stopTestRun
- # being called."
- def test_run_call_order__error_in_test_default_result(self):
- events = []
- class Foo(Test.LoggingTestCase):
- def defaultTestResult(self):
- return LoggingResult(self.events)
- def test(self):
- super(Foo, self).test()
- raise RuntimeError('raised by Foo.test')
- expected = ['startTestRun', 'startTest', 'setUp', 'test',
- 'tearDown', 'addError', 'stopTest', 'stopTestRun']
- Foo(events).run()
- self.assertEqual(events, expected)
- # "When a setUp() method is defined, the test runner will run that method
- # prior to each test. Likewise, if a tearDown() method is defined, the
- # test runner will invoke that method after each test. In the example,
- # setUp() was used to create a fresh sequence for each test."
- #
- # Make sure the proper call order is maintained, even if the test signals
- # a failure (as opposed to an error).
- def test_run_call_order__failure_in_test(self):
- events = []
- result = LoggingResult(events)
- class Foo(Test.LoggingTestCase):
- def test(self):
- super(Foo, self).test()
- self.fail('raised by Foo.test')
- expected = ['startTest', 'setUp', 'test', 'tearDown',
- 'addFailure', 'stopTest']
- Foo(events).run(result)
- self.assertEqual(events, expected)
- # "When a test fails with a default result stopTestRun is still called."
- def test_run_call_order__failure_in_test_default_result(self):
- class Foo(Test.LoggingTestCase):
- def defaultTestResult(self):
- return LoggingResult(self.events)
- def test(self):
- super(Foo, self).test()
- self.fail('raised by Foo.test')
- expected = ['startTestRun', 'startTest', 'setUp', 'test',
- 'tearDown', 'addFailure', 'stopTest', 'stopTestRun']
- events = []
- Foo(events).run()
- self.assertEqual(events, expected)
- # "When a setUp() method is defined, the test runner will run that method
- # prior to each test. Likewise, if a tearDown() method is defined, the
- # test runner will invoke that method after each test. In the example,
- # setUp() was used to create a fresh sequence for each test."
- #
- # Make sure the proper call order is maintained, even if tearDown() raises
- # an exception.
- def test_run_call_order__error_in_tearDown(self):
- events = []
- result = LoggingResult(events)
- class Foo(Test.LoggingTestCase):
- def tearDown(self):
- super(Foo, self).tearDown()
- raise RuntimeError('raised by Foo.tearDown')
- Foo(events).run(result)
- expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
- 'stopTest']
- self.assertEqual(events, expected)
- # "When tearDown errors with a default result stopTestRun is still called."
- def test_run_call_order__error_in_tearDown_default_result(self):
- class Foo(Test.LoggingTestCase):
- def defaultTestResult(self):
- return LoggingResult(self.events)
- def tearDown(self):
- super(Foo, self).tearDown()
- raise RuntimeError('raised by Foo.tearDown')
- events = []
- Foo(events).run()
- expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown',
- 'addError', 'stopTest', 'stopTestRun']
- self.assertEqual(events, expected)
- # "TestCase.run() still works when the defaultTestResult is a TestResult
- # that does not support startTestRun and stopTestRun.
- def test_run_call_order_default_result(self):
- class Foo(unittest.TestCase):
- def defaultTestResult(self):
- return ResultWithNoStartTestRunStopTestRun()
- def test(self):
- pass
- Foo('test').run()
- def _check_call_order__subtests(self, result, events, expected_events):
- class Foo(Test.LoggingTestCase):
- def test(self):
- super(Foo, self).test()
- for i in [1, 2, 3]:
- with self.subTest(i=i):
- if i == 1:
- self.fail('failure')
- for j in [2, 3]:
- with self.subTest(j=j):
- if i * j == 6:
- raise RuntimeError('raised by Foo.test')
- 1 / 0
- # Order is the following:
- # i=1 => subtest failure
- # i=2, j=2 => subtest success
- # i=2, j=3 => subtest error
- # i=3, j=2 => subtest error
- # i=3, j=3 => subtest success
- # toplevel => error
- Foo(events).run(result)
- self.assertEqual(events, expected_events)
- def test_run_call_order__subtests(self):
- events = []
- result = LoggingResult(events)
- expected = ['startTest', 'setUp', 'test', 'tearDown',
- 'addSubTestFailure', 'addSubTestSuccess',
- 'addSubTestFailure', 'addSubTestFailure',
- 'addSubTestSuccess', 'addError', 'stopTest']
- self._check_call_order__subtests(result, events, expected)
- def test_run_call_order__subtests_legacy(self):
- # With a legacy result object (without a addSubTest method),
- # text execution stops after the first subtest failure.
- events = []
- result = LegacyLoggingResult(events)
- expected = ['startTest', 'setUp', 'test', 'tearDown',
- 'addFailure', 'stopTest']
- self._check_call_order__subtests(result, events, expected)
- def _check_call_order__subtests_success(self, result, events, expected_events):
- class Foo(Test.LoggingTestCase):
- def test(self):
- super(Foo, self).test()
- for i in [1, 2]:
- with self.subTest(i=i):
- for j in [2, 3]:
- with self.subTest(j=j):
- pass
- Foo(events).run(result)
- self.assertEqual(events, expected_events)
- def test_run_call_order__subtests_success(self):
- events = []
- result = LoggingResult(events)
- # The 6 subtest successes are individually recorded, in addition
- # to the whole test success.
- expected = (['startTest', 'setUp', 'test', 'tearDown']
- + 6 * ['addSubTestSuccess']
- + ['addSuccess', 'stopTest'])
- self._check_call_order__subtests_success(result, events, expected)
- def test_run_call_order__subtests_success_legacy(self):
- # With a legacy result, only the whole test success is recorded.
- events = []
- result = LegacyLoggingResult(events)
- expected = ['startTest', 'setUp', 'test', 'tearDown',
- 'addSuccess', 'stopTest']
- self._check_call_order__subtests_success(result, events, expected)
- def test_run_call_order__subtests_failfast(self):
- events = []
- result = LoggingResult(events)
- result.failfast = True
- class Foo(Test.LoggingTestCase):
- def test(self):
- super(Foo, self).test()
- with self.subTest(i=1):
- self.fail('failure')
- with self.subTest(i=2):
- self.fail('failure')
- self.fail('failure')
- expected = ['startTest', 'setUp', 'test', 'tearDown',
- 'addSubTestFailure', 'stopTest']
- Foo(events).run(result)
- self.assertEqual(events, expected)
- def test_subtests_failfast(self):
- # Ensure proper test flow with subtests and failfast (issue #22894)
- events = []
- class Foo(unittest.TestCase):
- def test_a(self):
- with self.subTest():
- events.append('a1')
- events.append('a2')
- def test_b(self):
- with self.subTest():
- events.append('b1')
- with self.subTest():
- self.fail('failure')
- events.append('b2')
- def test_c(self):
- events.append('c')
- result = unittest.TestResult()
- result.failfast = True
- suite = unittest.makeSuite(Foo)
- suite.run(result)
- expected = ['a1', 'a2', 'b1']
- self.assertEqual(events, expected)
- # "This class attribute gives the exception raised by the test() method.
- # If a test framework needs to use a specialized exception, possibly to
- # carry additional information, it must subclass this exception in
- # order to ``play fair'' with the framework. The initial value of this
- # attribute is AssertionError"
- def test_failureException__default(self):
- class Foo(unittest.TestCase):
- def test(self):
- pass
- self.assertIs(Foo('test').failureException, AssertionError)
- # "This class attribute gives the exception raised by the test() method.
- # If a test framework needs to use a specialized exception, possibly to
- # carry additional information, it must subclass this exception in
- # order to ``play fair'' with the framework."
- #
- # Make sure TestCase.run() respects the designated failureException
- def test_failureException__subclassing__explicit_raise(self):
- events = []
- result = LoggingResult(events)
- class Foo(unittest.TestCase):
- def test(self):
- raise RuntimeError()
- failureException = RuntimeError
- self.assertIs(Foo('test').failureException, RuntimeError)
- Foo('test').run(result)
- expected = ['startTest', 'addFailure', 'stopTest']
- self.assertEqual(events, expected)
- # "This class attribute gives the exception raised by the test() method.
- # If a test framework needs to use a specialized exception, possibly to
- # carry additional information, it must subclass this exception in
- # order to ``play fair'' with the framework."
- #
- # Make sure TestCase.run() respects the designated failureException
- def test_failureException__subclassing__implicit_raise(self):
- events = []
- result = LoggingResult(events)
- class Foo(unittest.TestCase):
- def test(self):
- self.fail("foo")
- failureException = RuntimeError
- self.assertIs(Foo('test').failureException, RuntimeError)
- Foo('test').run(result)
- expected = ['startTest', 'addFailure', 'stopTest']
- self.assertEqual(events, expected)
- # "The default implementation does nothing."
- def test_setUp(self):
- class Foo(unittest.TestCase):
- def runTest(self):
- pass
- # ... and nothing should happen
- Foo().setUp()
- # "The default implementation does nothing."
- def test_tearDown(self):
- class Foo(unittest.TestCase):
- def runTest(self):
- pass
- # ... and nothing should happen
- Foo().tearDown()
- # "Return a string identifying the specific test case."
- #
- # Because of the vague nature of the docs, I'm not going to lock this
- # test down too much. Really all that can be asserted is that the id()
- # will be a string (either 8-byte or unicode -- again, because the docs
- # just say "string")
- def test_id(self):
- class Foo(unittest.TestCase):
- def runTest(self):
- pass
- self.assertIsInstance(Foo().id(), str)
- # "If result is omitted or None, a temporary result object is created,
- # used, and is made available to the caller. As TestCase owns the
- # temporary result startTestRun and stopTestRun are called.
- def test_run__uses_defaultTestResult(self):
- events = []
- defaultResult = LoggingResult(events)
- class Foo(unittest.TestCase):
- def test(self):
- events.append('test')
- def defaultTestResult(self):
- return defaultResult
- # Make run() find a result object on its own
- result = Foo('test').run()
- self.assertIs(result, defaultResult)
- expected = ['startTestRun', 'startTest', 'test', 'addSuccess',
- 'stopTest', 'stopTestRun']
- self.assertEqual(events, expected)
- # "The result object is returned to run's caller"
- def test_run__returns_given_result(self):
- class Foo(unittest.TestCase):
- def test(self):
- pass
- result = unittest.TestResult()
- retval = Foo('test').run(result)
- self.assertIs(retval, result)
- # "The same effect [as method run] may be had by simply calling the
- # TestCase instance."
- def test_call__invoking_an_instance_delegates_to_run(self):
- resultIn = unittest.TestResult()
- resultOut = unittest.TestResult()
- class Foo(unittest.TestCase):
- def test(self):
- pass
- def run(self, result):
- self.assertIs(result, resultIn)
- return resultOut
- retval = Foo('test')(resultIn)
- self.assertIs(retval, resultOut)
- def testShortDescriptionWithoutDocstring(self):
- self.assertIsNone(self.shortDescription())
- @unittest.skipIf(sys.flags.optimize >= 2,
- "Docstrings are omitted with -O2 and above")
- def testShortDescriptionWithOneLineDocstring(self):
- """Tests shortDescription() for a method with a docstring."""
- self.assertEqual(
- self.shortDescription(),
- 'Tests shortDescription() for a method with a docstring.')
- @unittest.skipIf(sys.flags.optimize >= 2,
- "Docstrings are omitted with -O2 and above")
- def testShortDescriptionWithMultiLineDocstring(self):
- """Tests shortDescription() for a method with a longer docstring.
- This method ensures that only the first line of a docstring is
- returned used in the short description, no matter how long the
- whole thing is.
- """
- self.assertEqual(
- self.shortDescription(),
- 'Tests shortDescription() for a method with a longer '
- 'docstring.')
- def testAddTypeEqualityFunc(self):
- class SadSnake(object):
- """Dummy class for test_addTypeEqualityFunc."""
- s1, s2 = SadSnake(), SadSnake()
- self.assertFalse(s1 == s2)
- def AllSnakesCreatedEqual(a, b, msg=None):
- return type(a) == type(b) == SadSnake
- self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual)
- self.assertEqual(s1, s2)
- # No this doesn't clean up and remove the SadSnake equality func
- # from this TestCase instance but since its a local nothing else
- # will ever notice that.
- def testAssertIs(self):
- thing = object()
- self.assertIs(thing, thing)
- self.assertRaises(self.failureException, self.assertIs, thing, object())
- def testAssertIsNot(self):
- thing = object()
- self.assertIsNot(thing, object())
- self.assertRaises(self.failureException, self.assertIsNot, thing, thing)
- def testAssertIsInstance(self):
- thing = []
- self.assertIsInstance(thing, list)
- self.assertRaises(self.failureException, self.assertIsInstance,
- thing, dict)
- def testAssertNotIsInstance(self):
- thing = []
- self.assertNotIsInstance(thing, dict)
- self.assertRaises(self.failureException, self.assertNotIsInstance,
- thing, list)
- def testAssertIn(self):
- animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
- self.assertIn('a', 'abc')
- self.assertIn(2, [1, 2, 3])
- self.assertIn('monkey', animals)
- self.assertNotIn('d', 'abc')
- self.assertNotIn(0, [1, 2, 3])
- self.assertNotIn('otter', animals)
- self.assertRaises(self.failureException, self.assertIn, 'x', 'abc')
- self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3])
- self.assertRaises(self.failureException, self.assertIn, 'elephant',
- animals)
- self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc')
- self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3])
- self.assertRaises(self.failureException, self.assertNotIn, 'cow',
- animals)
- def testAssertDictContainsSubset(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- self.assertDictContainsSubset({}, {})
- self.assertDictContainsSubset({}, {'a': 1})
- self.assertDictContainsSubset({'a': 1}, {'a': 1})
- self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})
- self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
- with self.assertRaises(self.failureException):
- self.assertDictContainsSubset({1: "one"}, {})
- with self.assertRaises(self.failureException):
- self.assertDictContainsSubset({'a': 2}, {'a': 1})
- with self.assertRaises(self.failureException):
- self.assertDictContainsSubset({'c': 1}, {'a': 1})
- with self.assertRaises(self.failureException):
- self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
- with self.assertRaises(self.failureException):
- self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
- one = ''.join(chr(i) for i in range(255))
- # this used to cause a UnicodeDecodeError constructing the failure msg
- with self.assertRaises(self.failureException):
- self.assertDictContainsSubset({'foo': one}, {'foo': '\uFFFD'})
- def testAssertEqual(self):
- equal_pairs = [
- ((), ()),
- ({}, {}),
- ([], []),
- (set(), set()),
- (frozenset(), frozenset())]
- for a, b in equal_pairs:
- # This mess of try excepts is to test the assertEqual behavior
- # itself.
- try:
- self.assertEqual(a, b)
- except self.failureException:
- self.fail('assertEqual(%r, %r) failed' % (a, b))
- try:
- self.assertEqual(a, b, msg='foo')
- except self.failureException:
- self.fail('assertEqual(%r, %r) with msg= failed' % (a, b))
- try:
- self.assertEqual(a, b, 'foo')
- except self.failureException:
- self.fail('assertEqual(%r, %r) with third parameter failed' %
- (a, b))
- unequal_pairs = [
- ((), []),
- ({}, set()),
- (set([4,1]), frozenset([4,2])),
- (frozenset([4,5]), set([2,3])),
- (set([3,4]), set([5,4]))]
- for a, b in unequal_pairs:
- self.assertRaises(self.failureException, self.assertEqual, a, b)
- self.assertRaises(self.failureException, self.assertEqual, a, b,
- 'foo')
- self.assertRaises(self.failureException, self.assertEqual, a, b,
- msg='foo')
- def testEquality(self):
- self.assertListEqual([], [])
- self.assertTupleEqual((), ())
- self.assertSequenceEqual([], ())
- a = [0, 'a', []]
- b = []
- self.assertRaises(unittest.TestCase.failureException,
- self.assertListEqual, a, b)
- self.assertRaises(unittest.TestCase.failureException,
- self.assertListEqual, tuple(a), tuple(b))
- self.assertRaises(unittest.TestCase.failureException,
- self.assertSequenceEqual, a, tuple(b))
- b.extend(a)
- self.assertListEqual(a, b)
- self.assertTupleEqual(tuple(a), tuple(b))
- self.assertSequenceEqual(a, tuple(b))
- self.assertSequenceEqual(tuple(a), b)
- self.assertRaises(self.failureException, self.assertListEqual,
- a, tuple(b))
- self.assertRaises(self.failureException, self.assertTupleEqual,
- tuple(a), b)
- self.assertRaises(self.failureException, self.assertListEqual, None, b)
- self.assertRaises(self.failureException, self.assertTupleEqual, None,
- tuple(b))
- self.assertRaises(self.failureException, self.assertSequenceEqual,
- None, tuple(b))
- self.assertRaises(self.failureException, self.assertListEqual, 1, 1)
- self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1)
- self.assertRaises(self.failureException, self.assertSequenceEqual,
- 1, 1)
- self.assertDictEqual({}, {})
- c = { 'x': 1 }
- d = {}
- self.assertRaises(unittest.TestCase.failureException,
- self.assertDictEqual, c, d)
- d.update(c)
- self.assertDictEqual(c, d)
- d['x'] = 0
- self.assertRaises(unittest.TestCase.failureException,
- self.assertDictEqual, c, d, 'These are unequal')
- self.assertRaises(self.failureException, self.assertDictEqual, None, d)
- self.assertRaises(self.failureException, self.assertDictEqual, [], d)
- self.assertRaises(self.failureException, self.assertDictEqual, 1, 1)
- def testAssertSequenceEqualMaxDiff(self):
- self.assertEqual(self.maxDiff, 80*8)
- seq1 = 'a' + 'x' * 80**2
- seq2 = 'b' + 'x' * 80**2
- diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(),
- pprint.pformat(seq2).splitlines()))
- # the +1 is the leading \n added by assertSequenceEqual
- omitted = unittest.case.DIFF_OMITTED % (len(diff) + 1,)
- self.maxDiff = len(diff)//2
- try:
- self.assertSequenceEqual(seq1, seq2)
- except self.failureException as e:
- msg = e.args[0]
- else:
- self.fail('assertSequenceEqual did not fail.')
- self.assertLess(len(msg), len(diff))
- self.assertIn(omitted, msg)
- self.maxDiff = len(diff) * 2
- try:
- self.assertSequenceEqual(seq1, seq2)
- except self.failureException as e:
- msg = e.args[0]
- else:
- self.fail('assertSequenceEqual did not fail.')
- self.assertGreater(len(msg), len(diff))
- self.assertNotIn(omitted, msg)
- self.maxDiff = None
- try:
- self.assertSequenceEqual(seq1, seq2)
- except self.failureException as e:
- msg = e.args[0]
- else:
- self.fail('assertSequenceEqual did not fail.')
- self.assertGreater(len(msg), len(diff))
- self.assertNotIn(omitted, msg)
- def testTruncateMessage(self):
- self.maxDiff = 1
- message = self._truncateMessage('foo', 'bar')
- omitted = unittest.case.DIFF_OMITTED % len('bar')
- self.assertEqual(message, 'foo' + omitted)
- self.maxDiff = None
- message = self._truncateMessage('foo', 'bar')
- self.assertEqual(message, 'foobar')
- self.maxDiff = 4
- message = self._truncateMessage('foo', 'bar')
- self.assertEqual(message, 'foobar')
- def testAssertDictEqualTruncates(self):
- test = unittest.TestCase('assertEqual')
- def truncate(msg, diff):
- return 'foo'
- test._truncateMessage = truncate
- try:
- test.assertDictEqual({}, {1: 0})
- except self.failureException as e:
- self.assertEqual(str(e), 'foo')
- else:
- self.fail('assertDictEqual did not fail')
- def testAssertMultiLineEqualTruncates(self):
- test = unittest.TestCase('assertEqual')
- def truncate(msg, diff):
- return 'foo'
- test._truncateMessage = truncate
- try:
- test.assertMultiLineEqual('foo', 'bar')
- except self.failureException as e:
- self.assertEqual(str(e), 'foo')
- else:
- self.fail('assertMultiLineEqual did not fail')
- def testAssertEqual_diffThreshold(self):
- # check threshold value
- self.assertEqual(self._diffThreshold, 2**16)
- # disable madDiff to get diff markers
- self.maxDiff = None
- # set a lower threshold value and add a cleanup to restore it
- old_threshold = self._diffThreshold
- self._diffThreshold = 2**5
- self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold))
- # under the threshold: diff marker (^) in error message
- s = 'x' * (2**4)
- with self.assertRaises(self.failureException) as cm:
- self.assertEqual(s + 'a', s + 'b')
- self.assertIn('^', str(cm.exception))
- self.assertEqual(s + 'a', s + 'a')
- # over the threshold: diff not used and marker (^) not in error message
- s = 'x' * (2**6)
- # if the path that uses difflib is taken, _truncateMessage will be
- # called -- replace it with explodingTruncation to verify that this
- # doesn't happen
- def explodingTruncation(message, diff):
- raise SystemError('this should not be raised')
- old_truncate = self._truncateMessage
- self._truncateMessage = explodingTruncation
- self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate))
- s1, s2 = s + 'a', s + 'b'
- with self.assertRaises(self.failureException) as cm:
- self.assertEqual(s1, s2)
- self.assertNotIn('^', str(cm.exception))
- self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2))
- self.assertEqual(s + 'a', s + 'a')
- def testAssertEqual_shorten(self):
- # set a lower threshold value and add a cleanup to restore it
- old_threshold = self._diffThreshold
- self._diffThreshold = 0
- self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold))
- s = 'x' * 100
- s1, s2 = s + 'a', s + 'b'
- with self.assertRaises(self.failureException) as cm:
- self.assertEqual(s1, s2)
- c = 'xxxx[35 chars]' + 'x' * 61
- self.assertEqual(str(cm.exception), "'%sa' != '%sb'" % (c, c))
- self.assertEqual(s + 'a', s + 'a')
- p = 'y' * 50
- s1, s2 = s + 'a' + p, s + 'b' + p
- with self.assertRaises(self.failureException) as cm:
- self.assertEqual(s1, s2)
- c = 'xxxx[85 chars]xxxxxxxxxxx'
- self.assertEqual(str(cm.exception), "'%sa%s' != '%sb%s'" % (c, p, c, p))
- p = 'y' * 100
- s1, s2 = s + 'a' + p, s + 'b' + p
- with self.assertRaises(self.failureException) as cm:
- self.assertEqual(s1, s2)
- c = 'xxxx[91 chars]xxxxx'
- d = 'y' * 40 + '[56 chars]yyyy'
- self.assertEqual(str(cm.exception), "'%sa%s' != '%sb%s'" % (c, d, c, d))
- def testAssertCountEqual(self):
- a = object()
- self.assertCountEqual([1, 2, 3], [3, 2, 1])
- self.assertCountEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo'])
- self.assertCountEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2))
- self.assertCountEqual([1, "2", "a", "a"], ["a", "2", True, "a"])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [1, 2] + [3] * 100, [1] * 100 + [2, 3])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [1, "2", "a", "a"], ["a", "2", True, 1])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [10], [10, 11])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [10, 11], [10])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [10, 11, 10], [10, 11])
- # Test that sequences of unhashable objects can be tested for sameness:
- self.assertCountEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]])
- # Test that iterator of unhashable objects can be tested for sameness:
- self.assertCountEqual(iter([1, 2, [], 3, 4]),
- iter([1, 2, [], 3, 4]))
- # hashable types, but not orderable
- self.assertRaises(self.failureException, self.assertCountEqual,
- [], [divmod, 'x', 1, 5j, 2j, frozenset()])
- # comparing dicts
- self.assertCountEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
- # comparing heterogenous non-hashable sequences
- self.assertCountEqual([1, 'x', divmod, []], [divmod, [], 'x', 1])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [], [divmod, [], 'x', 1, 5j, 2j, set()])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [[1]], [[2]])
- # Same elements, but not same sequence length
- self.assertRaises(self.failureException, self.assertCountEqual,
- [1, 1, 2], [2, 1])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [1, 1, "2", "a", "a"], ["2", "2", True, "a"])
- self.assertRaises(self.failureException, self.assertCountEqual,
- [1, {'b': 2}, None, True], [{'b': 2}, True, None])
- # Same elements which don't reliably compare, in
- # different order, see issue 10242
- a = [{2,4}, {1,2}]
- b = a[::-1]
- self.assertCountEqual(a, b)
- # test utility functions supporting assertCountEqual()
- diffs = set(unittest.util._count_diff_all_purpose('aaabccd', 'abbbcce'))
- expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}
- self.assertEqual(diffs, expected)
- diffs = unittest.util._count_diff_all_purpose([[]], [])
- self.assertEqual(diffs, [(1, 0, [])])
- diffs = set(unittest.util._count_diff_hashable('aaabccd', 'abbbcce'))
- expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}
- self.assertEqual(diffs, expected)
- def testAssertSetEqual(self):
- set1 = set()
- set2 = set()
- self.assertSetEqual(set1, set2)
- self.assertRaises(self.failureException, self.assertSetEqual, None, set2)
- self.assertRaises(self.failureException, self.assertSetEqual, [], set2)
- self.assertRaises(self.failureException, self.assertSetEqual, set1, None)
- self.assertRaises(self.failureException, self.assertSetEqual, set1, [])
- set1 = set(['a'])
- set2 = set()
- self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
- set1 = set(['a'])
- set2 = set(['a'])
- self.assertSetEqual(set1, set2)
- set1 = set(['a'])
- set2 = set(['a', 'b'])
- self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
- set1 = set(['a'])
- set2 = frozenset(['a', 'b'])
- self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
- set1 = set(['a', 'b'])
- set2 = frozenset(['a', 'b'])
- self.assertSetEqual(set1, set2)
- set1 = set()
- set2 = "foo"
- self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
- self.assertRaises(self.failureException, self.assertSetEqual, set2, set1)
- # make sure any string formatting is tuple-safe
- set1 = set([(0, 1), (2, 3)])
- set2 = set([(4, 5)])
- self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
- def testInequality(self):
- # Try ints
- self.assertGreater(2, 1)
- self.assertGreaterEqual(2, 1)
- self.assertGreaterEqual(1, 1)
- self.assertLess(1, 2)
- self.assertLessEqual(1, 2)
- self.assertLessEqual(1, 1)
- self.assertRaises(self.failureException, self.assertGreater, 1, 2)
- self.assertRaises(self.failureException, self.assertGreater, 1, 1)
- self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2)
- self.assertRaises(self.failureException, self.assertLess, 2, 1)
- self.assertRaises(self.failureException, self.assertLess, 1, 1)
- self.assertRaises(self.failureException, self.assertLessEqual, 2, 1)
- # Try Floats
- self.assertGreater(1.1, 1.0)
- self.assertGreaterEqual(1.1, 1.0)
- self.assertGreaterEqual(1.0, 1.0)
- self.assertLess(1.0, 1.1)
- self.assertLessEqual(1.0, 1.1)
- self.assertLessEqual(1.0, 1.0)
- self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1)
- self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0)
- self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1)
- self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0)
- self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0)
- self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0)
- # Try Strings
- self.assertGreater('bug', 'ant')
- self.assertGreaterEqual('bug', 'ant')
- self.assertGreaterEqual('ant', 'ant')
- self.assertLess('ant', 'bug')
- self.assertLessEqual('ant', 'bug')
- self.assertLessEqual('ant', 'ant')
- self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug')
- self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant')
- self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug')
- self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant')
- self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant')
- self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant')
- # Try bytes
- self.assertGreater(b'bug', b'ant')
- self.assertGreaterEqual(b'bug', b'ant')
- self.assertGreaterEqual(b'ant', b'ant')
- self.assertLess(b'ant', b'bug')
- self.assertLessEqual(b'ant', b'bug')
- self.assertLessEqual(b'ant', b'ant')
- self.assertRaises(self.failureException, self.assertGreater, b'ant', b'bug')
- self.assertRaises(self.failureException, self.assertGreater, b'ant', b'ant')
- self.assertRaises(self.failureException, self.assertGreaterEqual, b'ant',
- b'bug')
- self.assertRaises(self.failureException, self.assertLess, b'bug', b'ant')
- self.assertRaises(self.failureException, self.assertLess, b'ant', b'ant')
- self.assertRaises(self.failureException, self.assertLessEqual, b'bug', b'ant')
- def testAssertMultiLineEqual(self):
- sample_text = """\
- http://www.python.org/doc/2.3/lib/module-unittest.html
- test case
- A test case is the smallest unit of testing. [...]
- """
- revised_sample_text = """\
- http://www.python.org/doc/2.4.1/lib/module-unittest.html
- test case
- A test case is the smallest unit of testing. [...] You may provide your
- own implementation that does not subclass from TestCase, of course.
- """
- sample_text_error = """\
- - http://www.python.org/doc/2.3/lib/module-unittest.html
- ? ^
- + http://www.python.org/doc/2.4.1/lib/module-unittest.html
- ? ^^^
- test case
- - A test case is the smallest unit of testing. [...]
- + A test case is the smallest unit of testing. [...] You may provide your
- ? +++++++++++++++++++++
- + own implementation that does not subclass from TestCase, of course.
- """
- self.maxDiff = None
- try:
- self.assertMultiLineEqual(sample_text, revised_sample_text)
- except self.failureException as e:
- # need to remove the first line of the error message
- error = str(e).split('\n', 1)[1]
- self.assertEqual(sample_text_error, error)
- def testAssertEqualSingleLine(self):
- sample_text = "laden swallows fly slowly"
- revised_sample_text = "unladen swallows fly quickly"
- sample_text_error = """\
- - laden swallows fly slowly
- ? ^^^^
- + unladen swallows fly quickly
- ? ++ ^^^^^
- """
- try:
- self.assertEqual(sample_text, revised_sample_text)
- except self.failureException as e:
- # need to remove the first line of the error message
- error = str(e).split('\n', 1)[1]
- self.assertEqual(sample_text_error, error)
- def testEqualityBytesWarning(self):
- if sys.flags.bytes_warning:
- def bytes_warning():
- return self.assertWarnsRegex(BytesWarning,
- 'Comparison between bytes and string')
- else:
- def bytes_warning():
- return contextlib.ExitStack()
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertEqual('a', b'a')
- with bytes_warning():
- self.assertNotEqual('a', b'a')
- a = [0, 'a']
- b = [0, b'a']
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertListEqual(a, b)
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertTupleEqual(tuple(a), tuple(b))
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertSequenceEqual(a, tuple(b))
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertSequenceEqual(tuple(a), b)
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertSequenceEqual('a', b'a')
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertSetEqual(set(a), set(b))
- with self.assertRaises(self.failureException):
- self.assertListEqual(a, tuple(b))
- with self.assertRaises(self.failureException):
- self.assertTupleEqual(tuple(a), b)
- a = [0, b'a']
- b = [0]
- with self.assertRaises(self.failureException):
- self.assertListEqual(a, b)
- with self.assertRaises(self.failureException):
- self.assertTupleEqual(tuple(a), tuple(b))
- with self.assertRaises(self.failureException):
- self.assertSequenceEqual(a, tuple(b))
- with self.assertRaises(self.failureException):
- self.assertSequenceEqual(tuple(a), b)
- with self.assertRaises(self.failureException):
- self.assertSetEqual(set(a), set(b))
- a = [0]
- b = [0, b'a']
- with self.assertRaises(self.failureException):
- self.assertListEqual(a, b)
- with self.assertRaises(self.failureException):
- self.assertTupleEqual(tuple(a), tuple(b))
- with self.assertRaises(self.failureException):
- self.assertSequenceEqual(a, tuple(b))
- with self.assertRaises(self.failureException):
- self.assertSequenceEqual(tuple(a), b)
- with self.assertRaises(self.failureException):
- self.assertSetEqual(set(a), set(b))
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertDictEqual({'a': 0}, {b'a': 0})
- with self.assertRaises(self.failureException):
- self.assertDictEqual({}, {b'a': 0})
- with self.assertRaises(self.failureException):
- self.assertDictEqual({b'a': 0}, {})
- with self.assertRaises(self.failureException):
- self.assertCountEqual([b'a', b'a'], [b'a', b'a', b'a'])
- with bytes_warning():
- self.assertCountEqual(['a', b'a'], ['a', b'a'])
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertCountEqual(['a', 'a'], [b'a', b'a'])
- with bytes_warning(), self.assertRaises(self.failureException):
- self.assertCountEqual(['a', 'a', []], [b'a', b'a', []])
- def testAssertIsNone(self):
- self.assertIsNone(None)
- self.assertRaises(self.failureException, self.assertIsNone, False)
- self.assertIsNotNone('DjZoPloGears on Rails')
- self.assertRaises(self.failureException, self.assertIsNotNone, None)
- def testAssertRegex(self):
- self.assertRegex('asdfabasdf', r'ab+')
- self.assertRaises(self.failureException, self.assertRegex,
- 'saaas', r'aaaa')
- def testAssertRaisesCallable(self):
- class ExceptionMock(Exception):
- pass
- def Stub():
- raise ExceptionMock('We expect')
- self.assertRaises(ExceptionMock, Stub)
- # A tuple of exception classes is accepted
- self.assertRaises((ValueError, ExceptionMock), Stub)
- # *args and **kwargs also work
- self.assertRaises(ValueError, int, '19', base=8)
- # Failure when no exception is raised
- with self.assertRaises(self.failureException):
- self.assertRaises(ExceptionMock, lambda: 0)
- # Failure when the function is None
- with self.assertWarns(DeprecationWarning):
- self.assertRaises(ExceptionMock, None)
- # Failure when another exception is raised
- with self.assertRaises(ExceptionMock):
- self.assertRaises(ValueError, Stub)
- def testAssertRaisesContext(self):
- class ExceptionMock(Exception):
- pass
- def Stub():
- raise ExceptionMock('We expect')
- with self.assertRaises(ExceptionMock):
- Stub()
- # A tuple of exception classes is accepted
- with self.assertRaises((ValueError, ExceptionMock)) as cm:
- Stub()
- # The context manager exposes caught exception
- self.assertIsInstance(cm.exception, ExceptionMock)
- self.assertEqual(cm.exception.args[0], 'We expect')
- # *args and **kwargs also work
- with self.assertRaises(ValueError):
- int('19', base=8)
- # Failure when no exception is raised
- with self.assertRaises(self.failureException):
- with self.assertRaises(ExceptionMock):
- pass
- # Custom message
- with self.assertRaisesRegex(self.failureException, 'foobar'):
- with self.assertRaises(ExceptionMock, msg='foobar'):
- pass
- # Invalid keyword argument
- with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
- self.assertRaises(AssertionError):
- with self.assertRaises(ExceptionMock, foobar=42):
- pass
- # Failure when another exception is raised
- with self.assertRaises(ExceptionMock):
- self.assertRaises(ValueError, Stub)
- def testAssertRaisesNoExceptionType(self):
- with self.assertRaises(TypeError):
- self.assertRaises()
- with self.assertRaises(TypeError):
- self.assertRaises(1)
- with self.assertRaises(TypeError):
- self.assertRaises(object)
- with self.assertRaises(TypeError):
- self.assertRaises((ValueError, 1))
- with self.assertRaises(TypeError):
- self.assertRaises((ValueError, object))
- def testAssertRaisesRegex(self):
- class ExceptionMock(Exception):
- pass
- def Stub():
- raise ExceptionMock('We expect')
- self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub)
- self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
- with self.assertWarns(DeprecationWarning):
- self.assertRaisesRegex(ExceptionMock, 'expect$', None)
- def testAssertNotRaisesRegex(self):
- self.assertRaisesRegex(
- self.failureException, '^Exception not raised by <lambda>$',
- self.assertRaisesRegex, Exception, re.compile('x'),
- lambda: None)
- self.assertRaisesRegex(
- self.failureException, '^Exception not raised by <lambda>$',
- self.assertRaisesRegex, Exception, 'x',
- lambda: None)
- # Custom message
- with self.assertRaisesRegex(self.failureException, 'foobar'):
- with self.assertRaisesRegex(Exception, 'expect', msg='foobar'):
- pass
- # Invalid keyword argument
- with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
- self.assertRaises(AssertionError):
- with self.assertRaisesRegex(Exception, 'expect', foobar=42):
- pass
- def testAssertRaisesRegexInvalidRegex(self):
- # Issue 20145.
- class MyExc(Exception):
- pass
- self.assertRaises(TypeError, self.assertRaisesRegex, MyExc, lambda: True)
- def testAssertWarnsRegexInvalidRegex(self):
- # Issue 20145.
- class MyWarn(Warning):
- pass
- self.assertRaises(TypeError, self.assertWarnsRegex, MyWarn, lambda: True)
- def testAssertRaisesRegexMismatch(self):
- def Stub():
- raise Exception('Unexpected')
- self.assertRaisesRegex(
- self.failureException,
- r'"\^Expected\$" does not match "Unexpected"',
- self.assertRaisesRegex, Exception, '^Expected$',
- Stub)
- self.assertRaisesRegex(
- self.failureException,
- r'"\^Expected\$" does not match "Unexpected"',
- self.assertRaisesRegex, Exception,
- re.compile('^Expected$'), Stub)
- def testAssertRaisesExcValue(self):
- class ExceptionMock(Exception):
- pass
- def Stub(foo):
- raise ExceptionMock(foo)
- v = "particular value"
- ctx = self.assertRaises(ExceptionMock)
- with ctx:
- Stub(v)
- e = ctx.exception
- self.assertIsInstance(e, ExceptionMock)
- self.assertEqual(e.args[0], v)
- def testAssertRaisesRegexNoExceptionType(self):
- with self.assertRaises(TypeError):
- self.assertRaisesRegex()
- with self.assertRaises(TypeError):
- self.assertRaisesRegex(ValueError)
- with self.assertRaises(TypeError):
- self.assertRaisesRegex(1, 'expect')
- with self.assertRaises(TypeError):
- self.assertRaisesRegex(object, 'expect')
- with self.assertRaises(TypeError):
- self.assertRaisesRegex((ValueError, 1), 'expect')
- with self.assertRaises(TypeError):
- self.assertRaisesRegex((ValueError, object), 'expect')
- def testAssertWarnsCallable(self):
- def _runtime_warn():
- warnings.warn("foo", RuntimeWarning)
- # Success when the right warning is triggered, even several times
- self.assertWarns(RuntimeWarning, _runtime_warn)
- self.assertWarns(RuntimeWarning, _runtime_warn)
- # A tuple of warning classes is accepted
- self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn)
- # *args and **kwargs also work
- self.assertWarns(RuntimeWarning,
- warnings.warn, "foo", category=RuntimeWarning)
- # Failure when no warning is triggered
- with self.assertRaises(self.failureException):
- self.assertWarns(RuntimeWarning, lambda: 0)
- # Failure when the function is None
- with self.assertWarns(DeprecationWarning):
- self.assertWarns(RuntimeWarning, None)
- # Failure when another warning is triggered
- with warnings.catch_warnings():
- # Force default filter (in case tests are run with -We)
- warnings.simplefilter("default", RuntimeWarning)
- with self.assertRaises(self.failureException):
- self.assertWarns(DeprecationWarning, _runtime_warn)
- # Filters for other warnings are not modified
- with warnings.catch_warnings():
- warnings.simplefilter("error", RuntimeWarning)
- with self.assertRaises(RuntimeWarning):
- self.assertWarns(DeprecationWarning, _runtime_warn)
- def testAssertWarnsContext(self):
- # Believe it or not, it is preferable to duplicate all tests above,
- # to make sure the __warningregistry__ $@ is circumvented correctly.
- def _runtime_warn():
- warnings.warn("foo", RuntimeWarning)
- _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
- with self.assertWarns(RuntimeWarning) as cm:
- _runtime_warn()
- # A tuple of warning classes is accepted
- with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm:
- _runtime_warn()
- # The context manager exposes various useful attributes
- self.assertIsInstance(cm.warning, RuntimeWarning)
- self.assertEqual(cm.warning.args[0], "foo")
- self.assertIn("test_case.py", cm.filename)
- self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
- # Same with several warnings
- with self.assertWarns(RuntimeWarning):
- _runtime_warn()
- _runtime_warn()
- with self.assertWarns(RuntimeWarning):
- warnings.warn("foo", category=RuntimeWarning)
- # Failure when no warning is triggered
- with self.assertRaises(self.failureException):
- with self.assertWarns(RuntimeWarning):
- pass
- # Custom message
- with self.assertRaisesRegex(self.failureException, 'foobar'):
- with self.assertWarns(RuntimeWarning, msg='foobar'):
- pass
- # Invalid keyword argument
- with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
- self.assertRaises(AssertionError):
- with self.assertWarns(RuntimeWarning, foobar=42):
- pass
- # Failure when another warning is triggered
- with warnings.catch_warnings():
- # Force default filter (in case tests are run with -We)
- warnings.simplefilter("default", RuntimeWarning)
- with self.assertRaises(self.failureException):
- with self.assertWarns(DeprecationWarning):
- _runtime_warn()
- # Filters for other warnings are not modified
- with warnings.catch_warnings():
- warnings.simplefilter("error", RuntimeWarning)
- with self.assertRaises(RuntimeWarning):
- with self.assertWarns(DeprecationWarning):
- _runtime_warn()
- def testAssertWarnsNoExceptionType(self):
- with self.assertRaises(TypeError):
- self.assertWarns()
- with self.assertRaises(TypeError):
- self.assertWarns(1)
- with self.assertRaises(TypeError):
- self.assertWarns(object)
- with self.assertRaises(TypeError):
- self.assertWarns((UserWarning, 1))
- with self.assertRaises(TypeError):
- self.assertWarns((UserWarning, object))
- with self.assertRaises(TypeError):
- self.assertWarns((UserWarning, Exception))
- def testAssertWarnsRegexCallable(self):
- def _runtime_warn(msg):
- warnings.warn(msg, RuntimeWarning)
- self.assertWarnsRegex(RuntimeWarning, "o+",
- _runtime_warn, "foox")
- # Failure when no warning is triggered
- with self.assertRaises(self.failureException):
- self.assertWarnsRegex(RuntimeWarning, "o+",
- lambda: 0)
- # Failure when the function is None
- with self.assertWarns(DeprecationWarning):
- self.assertWarnsRegex(RuntimeWarning, "o+", None)
- # Failure when another warning is triggered
- with warnings.catch_warnings():
- # Force default filter (in case tests are run with -We)
- warnings.simplefilter("default", RuntimeWarning)
- with self.assertRaises(self.failureException):
- self.assertWarnsRegex(DeprecationWarning, "o+",
- _runtime_warn, "foox")
- # Failure when message doesn't match
- with self.assertRaises(self.failureException):
- self.assertWarnsRegex(RuntimeWarning, "o+",
- _runtime_warn, "barz")
- # A little trickier: we ask RuntimeWarnings to be raised, and then
- # check for some of them. It is implementation-defined whether
- # non-matching RuntimeWarnings are simply re-raised, or produce a
- # failureException.
- with warnings.catch_warnings():
- warnings.simplefilter("error", RuntimeWarning)
- with self.assertRaises((RuntimeWarning, self.failureException)):
- self.assertWarnsRegex(RuntimeWarning, "o+",
- _runtime_warn, "barz")
- def testAssertWarnsRegexContext(self):
- # Same as above, but with assertWarnsRegex as a context manager
- def _runtime_warn(msg):
- warnings.warn(msg, RuntimeWarning)
- _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
- with self.assertWarnsRegex(RuntimeWarning, "o+") as cm:
- _runtime_warn("foox")
- self.assertIsInstance(cm.warning, RuntimeWarning)
- self.assertEqual(cm.warning.args[0], "foox")
- self.assertIn("test_case.py", cm.filename)
- self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
- # Failure when no warning is triggered
- with self.assertRaises(self.failureException):
- with self.assertWarnsRegex(RuntimeWarning, "o+"):
- pass
- # Custom message
- with self.assertRaisesRegex(self.failureException, 'foobar'):
- with self.assertWarnsRegex(RuntimeWarning, 'o+', msg='foobar'):
- pass
- # Invalid keyword argument
- with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
- self.assertRaises(AssertionError):
- with self.assertWarnsRegex(RuntimeWarning, 'o+', foobar=42):
- pass
- # Failure when another warning is triggered
- with warnings.catch_warnings():
- # Force default filter (in case tests are run with -We)
- warnings.simplefilter("default", RuntimeWarning)
- with self.assertRaises(self.failureException):
- with self.assertWarnsRegex(DeprecationWarning, "o+"):
- _runtime_warn("foox")
- # Failure when message doesn't match
- with self.assertRaises(self.failureException):
- with self.assertWarnsRegex(RuntimeWarning, "o+"):
- _runtime_warn("barz")
- # A little trickier: we ask RuntimeWarnings to be raised, and then
- # check for some of them. It is implementation-defined whether
- # non-matching RuntimeWarnings are simply re-raised, or produce a
- # failureException.
- with warnings.catch_warnings():
- warnings.simplefilter("error", RuntimeWarning)
- with self.assertRaises((RuntimeWarning, self.failureException)):
- with self.assertWarnsRegex(RuntimeWarning, "o+"):
- _runtime_warn("barz")
- def testAssertWarnsRegexNoExceptionType(self):
- with self.assertRaises(TypeError):
- self.assertWarnsRegex()
- with self.assertRaises(TypeError):
- self.assertWarnsRegex(UserWarning)
- with self.assertRaises(TypeError):
- self.assertWarnsRegex(1, 'expect')
- with self.assertRaises(TypeError):
- self.assertWarnsRegex(object, 'expect')
- with self.assertRaises(TypeError):
- self.assertWarnsRegex((UserWarning, 1), 'expect')
- with self.assertRaises(TypeError):
- self.assertWarnsRegex((UserWarning, object), 'expect')
- with self.assertRaises(TypeError):
- self.assertWarnsRegex((UserWarning, Exception), 'expect')
- @contextlib.contextmanager
- def assertNoStderr(self):
- with captured_stderr() as buf:
- yield
- self.assertEqual(buf.getvalue(), "")
- def assertLogRecords(self, records, matches):
- self.assertEqual(len(records), len(matches))
- for rec, match in zip(records, matches):
- self.assertIsInstance(rec, logging.LogRecord)
- for k, v in match.items():
- self.assertEqual(getattr(rec, k), v)
- def testAssertLogsDefaults(self):
- # defaults: root logger, level INFO
- with self.assertNoStderr():
- with self.assertLogs() as cm:
- log_foo.info("1")
- log_foobar.debug("2")
- self.assertEqual(cm.output, ["INFO:foo:1"])
- self.assertLogRecords(cm.records, [{'name': 'foo'}])
- def testAssertLogsTwoMatchingMessages(self):
- # Same, but with two matching log messages
- with self.assertNoStderr():
- with self.assertLogs() as cm:
- log_foo.info("1")
- log_foobar.debug("2")
- log_quux.warning("3")
- self.assertEqual(cm.output, ["INFO:foo:1", "WARNING:quux:3"])
- self.assertLogRecords(cm.records,
- [{'name': 'foo'}, {'name': 'quux'}])
- def checkAssertLogsPerLevel(self, level):
- # Check level filtering
- with self.assertNoStderr():
- with self.assertLogs(level=level) as cm:
- log_foo.warning("1")
- log_foobar.error("2")
- log_quux.critical("3")
- self.assertEqual(cm.output, ["ERROR:foo.bar:2", "CRITICAL:quux:3"])
- self.assertLogRecords(cm.records,
- [{'name': 'foo.bar'}, {'name': 'quux'}])
- def testAssertLogsPerLevel(self):
- self.checkAssertLogsPerLevel(logging.ERROR)
- self.checkAssertLogsPerLevel('ERROR')
- def checkAssertLogsPerLogger(self, logger):
- # Check per-logger filtering
- with self.assertNoStderr():
- with self.assertLogs(level='DEBUG') as outer_cm:
- with self.assertLogs(logger, level='DEBUG') as cm:
- log_foo.info("1")
- log_foobar.debug("2")
- log_quux.warning("3")
- self.assertEqual(cm.output, ["INFO:foo:1", "DEBUG:foo.bar:2"])
- self.assertLogRecords(cm.records,
- [{'name': 'foo'}, {'name': 'foo.bar'}])
- # The outer catchall caught the quux log
- self.assertEqual(outer_cm.output, ["WARNING:quux:3"])
- def testAssertLogsPerLogger(self):
- self.checkAssertLogsPerLogger(logging.getLogger('foo'))
- self.checkAssertLogsPerLogger('foo')
- def testAssertLogsFailureNoLogs(self):
- # Failure due to no logs
- with self.assertNoStderr():
- with self.assertRaises(self.failureException):
- with self.assertLogs():
- pass
- def testAssertLogsFailureLevelTooHigh(self):
- # Failure due to level too high
- with self.assertNoStderr():
- with self.assertRaises(self.failureException):
- with self.assertLogs(level='WARNING'):
- log_foo.info("1")
- def testAssertLogsFailureMismatchingLogger(self):
- # Failure due to mismatching logger (and the logged message is
- # passed through)
- with self.assertLogs('quux', level='ERROR'):
- with self.assertRaises(self.failureException):
- with self.assertLogs('foo'):
- log_quux.error("1")
- def testDeprecatedMethodNames(self):
- """
- Test that the deprecated methods raise a DeprecationWarning. See #9424.
- """
- old = (
- (self.failIfEqual, (3, 5)),
- (self.assertNotEquals, (3, 5)),
- (self.failUnlessEqual, (3, 3)),
- (self.assertEquals, (3, 3)),
- (self.failUnlessAlmostEqual, (2.0, 2.0)),
- (self.assertAlmostEquals, (2.0, 2.0)),
- (self.failIfAlmostEqual, (3.0, 5.0)),
- (self.assertNotAlmostEquals, (3.0, 5.0)),
- (self.failUnless, (True,)),
- (self.assert_, (True,)),
- (self.failUnlessRaises, (TypeError, lambda _: 3.14 + 'spam')),
- (self.failIf, (False,)),
- (self.assertDictContainsSubset, (dict(a=1, b=2), dict(a=1, b=2, c=3))),
- (self.assertRaisesRegexp, (KeyError, 'foo', lambda: {}['foo'])),
- (self.assertRegexpMatches, ('bar', 'bar')),
- )
- for meth, args in old:
- with self.assertWarns(DeprecationWarning):
- meth(*args)
- # disable this test for now. When the version where the fail* methods will
- # be removed is decided, re-enable it and update the version
- def _testDeprecatedFailMethods(self):
- """Test that the deprecated fail* methods get removed in 3.x"""
- if sys.version_info[:2] < (3, 3):
- return
- deprecated_names = [
- 'failIfEqual', 'failUnlessEqual', 'failUnlessAlmostEqual',
- 'failIfAlmostEqual', 'failUnless', 'failUnlessRaises', 'failIf',
- 'assertDictContainsSubset',
- ]
- for deprecated_name in deprecated_names:
- with self.assertRaises(AttributeError):
- getattr(self, deprecated_name) # remove these in 3.x
- def testDeepcopy(self):
- # Issue: 5660
- class TestableTest(unittest.TestCase):
- def testNothing(self):
- pass
- test = TestableTest('testNothing')
- # This shouldn't blow up
- deepcopy(test)
- def testPickle(self):
- # Issue 10326
- # Can't use TestCase classes defined in Test class as
- # pickle does not work with inner classes
- test = unittest.TestCase('run')
- for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
- # blew up prior to fix
- pickled_test = pickle.dumps(test, protocol=protocol)
- unpickled_test = pickle.loads(pickled_test)
- self.assertEqual(test, unpickled_test)
- # exercise the TestCase instance in a way that will invoke
- # the type equality lookup mechanism
- unpickled_test.assertEqual(set(), set())
- def testKeyboardInterrupt(self):
- def _raise(self=None):
- raise KeyboardInterrupt
- def nothing(self):
- pass
- class Test1(unittest.TestCase):
- test_something = _raise
- class Test2(unittest.TestCase):
- setUp = _raise
- test_something = nothing
- class Test3(unittest.TestCase):
- test_something = nothing
- tearDown = _raise
- class Test4(unittest.TestCase):
- def test_something(self):
- self.addCleanup(_raise)
- for klass in (Test1, Test2, Test3, Test4):
- with self.assertRaises(KeyboardInterrupt):
- klass('test_something').run()
- def testSkippingEverywhere(self):
- def _skip(self=None):
- raise unittest.SkipTest('some reason')
- def nothing(self):
- pass
- class Test1(unittest.TestCase):
- test_something = _skip
- class Test2(unittest.TestCase):
- setUp = _skip
- test_something = nothing
- class Test3(unittest.TestCase):
- test_something = nothing
- tearDown = _skip
- class Test4(unittest.TestCase):
- def test_something(self):
- self.addCleanup(_skip)
- for klass in (Test1, Test2, Test3, Test4):
- result = unittest.TestResult()
- klass('test_something').run(result)
- self.assertEqual(len(result.skipped), 1)
- self.assertEqual(result.testsRun, 1)
- def testSystemExit(self):
- def _raise(self=None):
- raise SystemExit
- def nothing(self):
- pass
- class Test1(unittest.TestCase):
- test_something = _raise
- class Test2(unittest.TestCase):
- setUp = _raise
- test_something = nothing
- class Test3(unittest.TestCase):
- test_something = nothing
- tearDown = _raise
- class Test4(unittest.TestCase):
- def test_something(self):
- self.addCleanup(_raise)
- for klass in (Test1, Test2, Test3, Test4):
- result = unittest.TestResult()
- klass('test_something').run(result)
- self.assertEqual(len(result.errors), 1)
- self.assertEqual(result.testsRun, 1)
- @support.cpython_only
- def testNoCycles(self):
- case = unittest.TestCase()
- wr = weakref.ref(case)
- with support.disable_gc():
- del case
- self.assertFalse(wr())
- def test_no_exception_leak(self):
- # Issue #19880: TestCase.run() should not keep a reference
- # to the exception
- class MyException(Exception):
- ninstance = 0
- def __init__(self):
- MyException.ninstance += 1
- Exception.__init__(self)
- def __del__(self):
- MyException.ninstance -= 1
- class TestCase(unittest.TestCase):
- def test1(self):
- raise MyException()
- @unittest.expectedFailure
- def test2(self):
- raise MyException()
- for method_name in ('test1', 'test2'):
- testcase = TestCase(method_name)
- testcase.run()
- self.assertEqual(MyException.ninstance, 0)
- if __name__ == "__main__":
- unittest.main()
|