How to use invariants method in hypothesis

Best Python code snippet using hypothesis

test_with.py

Source:test_with.py Github

copy

Full Screen

1#!/usr/bin/env python23"""Unit tests for the with statement specified in PEP 343."""456__author__ = "Mike Bland"7__email__ = "mbland at acm dot org"89import sys10import unittest11from collections import deque12from contextlib import GeneratorContextManager, contextmanager13from test.test_support import run_unittest141516class MockContextManager(GeneratorContextManager):17 def __init__(self, gen):18 GeneratorContextManager.__init__(self, gen)19 self.enter_called = False20 self.exit_called = False21 self.exit_args = None2223 def __enter__(self):24 self.enter_called = True25 return GeneratorContextManager.__enter__(self)2627 def __exit__(self, type, value, traceback):28 self.exit_called = True29 self.exit_args = (type, value, traceback)30 return GeneratorContextManager.__exit__(self, type,31 value, traceback)323334def mock_contextmanager(func):35 def helper(*args, **kwds):36 return MockContextManager(func(*args, **kwds))37 return helper383940class MockResource(object):41 def __init__(self):42 self.yielded = False43 self.stopped = False444546@mock_contextmanager47def mock_contextmanager_generator():48 mock = MockResource()49 try:50 mock.yielded = True51 yield mock52 finally:53 mock.stopped = True545556class Nested(object):5758 def __init__(self, *managers):59 self.managers = managers60 self.entered = None6162 def __enter__(self):63 if self.entered is not None:64 raise RuntimeError("Context is not reentrant")65 self.entered = deque()66 vars = []67 try:68 for mgr in self.managers:69 vars.append(mgr.__enter__())70 self.entered.appendleft(mgr)71 except:72 if not self.__exit__(*sys.exc_info()):73 raise74 return vars7576 def __exit__(self, *exc_info):77 # Behave like nested with statements78 # first in, last out79 # New exceptions override old ones80 ex = exc_info81 for mgr in self.entered:82 try:83 if mgr.__exit__(*ex):84 ex = (None, None, None)85 except:86 ex = sys.exc_info()87 self.entered = None88 if ex is not exc_info:89 raise ex[0], ex[1], ex[2]909192class MockNested(Nested):93 def __init__(self, *managers):94 Nested.__init__(self, *managers)95 self.enter_called = False96 self.exit_called = False97 self.exit_args = None9899 def __enter__(self):100 self.enter_called = True101 return Nested.__enter__(self)102103 def __exit__(self, *exc_info):104 self.exit_called = True105 self.exit_args = exc_info106 return Nested.__exit__(self, *exc_info)107108109class FailureTestCase(unittest.TestCase):110 def testNameError(self):111 def fooNotDeclared():112 with foo: pass113 self.assertRaises(NameError, fooNotDeclared)114115 def testEnterAttributeError(self):116 class LacksEnter(object):117 def __exit__(self, type, value, traceback):118 pass119120 def fooLacksEnter():121 foo = LacksEnter()122 with foo: pass123 self.assertRaises(AttributeError, fooLacksEnter)124125 def testExitAttributeError(self):126 class LacksExit(object):127 def __enter__(self):128 pass129130 def fooLacksExit():131 foo = LacksExit()132 with foo: pass133 self.assertRaises(AttributeError, fooLacksExit)134135 def assertRaisesSyntaxError(self, codestr):136 def shouldRaiseSyntaxError(s):137 compile(s, '', 'single')138 self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr)139140 def testAssignmentToNoneError(self):141 self.assertRaisesSyntaxError('with mock as None:\n pass')142 self.assertRaisesSyntaxError(143 'with mock as (None):\n'144 ' pass')145146 def testAssignmentToEmptyTupleError(self):147 self.assertRaisesSyntaxError(148 'with mock as ():\n'149 ' pass')150151 def testAssignmentToTupleOnlyContainingNoneError(self):152 self.assertRaisesSyntaxError('with mock as None,:\n pass')153 self.assertRaisesSyntaxError(154 'with mock as (None,):\n'155 ' pass')156157 def testAssignmentToTupleContainingNoneError(self):158 self.assertRaisesSyntaxError(159 'with mock as (foo, None, bar):\n'160 ' pass')161162 def testEnterThrows(self):163 class EnterThrows(object):164 def __enter__(self):165 raise RuntimeError("Enter threw")166 def __exit__(self, *args):167 pass168169 def shouldThrow():170 ct = EnterThrows()171 self.foo = None172 with ct as self.foo:173 pass174 self.assertRaises(RuntimeError, shouldThrow)175 self.assertEqual(self.foo, None)176177 def testExitThrows(self):178 class ExitThrows(object):179 def __enter__(self):180 return181 def __exit__(self, *args):182 raise RuntimeError(42)183 def shouldThrow():184 with ExitThrows():185 pass186 self.assertRaises(RuntimeError, shouldThrow)187188class ContextmanagerAssertionMixin(object):189 TEST_EXCEPTION = RuntimeError("test exception")190191 def assertInWithManagerInvariants(self, mock_manager):192 self.assertTrue(mock_manager.enter_called)193 self.assertFalse(mock_manager.exit_called)194 self.assertEqual(mock_manager.exit_args, None)195196 def assertAfterWithManagerInvariants(self, mock_manager, exit_args):197 self.assertTrue(mock_manager.enter_called)198 self.assertTrue(mock_manager.exit_called)199 self.assertEqual(mock_manager.exit_args, exit_args)200201 def assertAfterWithManagerInvariantsNoError(self, mock_manager):202 self.assertAfterWithManagerInvariants(mock_manager,203 (None, None, None))204205 def assertInWithGeneratorInvariants(self, mock_generator):206 self.assertTrue(mock_generator.yielded)207 self.assertFalse(mock_generator.stopped)208209 def assertAfterWithGeneratorInvariantsNoError(self, mock_generator):210 self.assertTrue(mock_generator.yielded)211 self.assertTrue(mock_generator.stopped)212213 def raiseTestException(self):214 raise self.TEST_EXCEPTION215216 def assertAfterWithManagerInvariantsWithError(self, mock_manager):217 self.assertTrue(mock_manager.enter_called)218 self.assertTrue(mock_manager.exit_called)219 self.assertEqual(mock_manager.exit_args[0], RuntimeError)220 self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)221222 def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):223 self.assertTrue(mock_generator.yielded)224 self.assertTrue(mock_generator.stopped)225226227class NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):228 def testInlineGeneratorSyntax(self):229 with mock_contextmanager_generator():230 pass231232 def testUnboundGenerator(self):233 mock = mock_contextmanager_generator()234 with mock:235 pass236 self.assertAfterWithManagerInvariantsNoError(mock)237238 def testInlineGeneratorBoundSyntax(self):239 with mock_contextmanager_generator() as foo:240 self.assertInWithGeneratorInvariants(foo)241 # FIXME: In the future, we'll try to keep the bound names from leaking242 self.assertAfterWithGeneratorInvariantsNoError(foo)243244 def testInlineGeneratorBoundToExistingVariable(self):245 foo = None246 with mock_contextmanager_generator() as foo:247 self.assertInWithGeneratorInvariants(foo)248 self.assertAfterWithGeneratorInvariantsNoError(foo)249250 def testInlineGeneratorBoundToDottedVariable(self):251 with mock_contextmanager_generator() as self.foo:252 self.assertInWithGeneratorInvariants(self.foo)253 self.assertAfterWithGeneratorInvariantsNoError(self.foo)254255 def testBoundGenerator(self):256 mock = mock_contextmanager_generator()257 with mock as foo:258 self.assertInWithGeneratorInvariants(foo)259 self.assertInWithManagerInvariants(mock)260 self.assertAfterWithGeneratorInvariantsNoError(foo)261 self.assertAfterWithManagerInvariantsNoError(mock)262263 def testNestedSingleStatements(self):264 mock_a = mock_contextmanager_generator()265 with mock_a as foo:266 mock_b = mock_contextmanager_generator()267 with mock_b as bar:268 self.assertInWithManagerInvariants(mock_a)269 self.assertInWithManagerInvariants(mock_b)270 self.assertInWithGeneratorInvariants(foo)271 self.assertInWithGeneratorInvariants(bar)272 self.assertAfterWithManagerInvariantsNoError(mock_b)273 self.assertAfterWithGeneratorInvariantsNoError(bar)274 self.assertInWithManagerInvariants(mock_a)275 self.assertInWithGeneratorInvariants(foo)276 self.assertAfterWithManagerInvariantsNoError(mock_a)277 self.assertAfterWithGeneratorInvariantsNoError(foo)278279280class NestedNonexceptionalTestCase(unittest.TestCase,281 ContextmanagerAssertionMixin):282 def testSingleArgInlineGeneratorSyntax(self):283 with Nested(mock_contextmanager_generator()):284 pass285286 def testSingleArgUnbound(self):287 mock_contextmanager = mock_contextmanager_generator()288 mock_nested = MockNested(mock_contextmanager)289 with mock_nested:290 self.assertInWithManagerInvariants(mock_contextmanager)291 self.assertInWithManagerInvariants(mock_nested)292 self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)293 self.assertAfterWithManagerInvariantsNoError(mock_nested)294295 def testSingleArgBoundToNonTuple(self):296 m = mock_contextmanager_generator()297 # This will bind all the arguments to nested() into a single list298 # assigned to foo.299 with Nested(m) as foo:300 self.assertInWithManagerInvariants(m)301 self.assertAfterWithManagerInvariantsNoError(m)302303 def testSingleArgBoundToSingleElementParenthesizedList(self):304 m = mock_contextmanager_generator()305 # This will bind all the arguments to nested() into a single list306 # assigned to foo.307 with Nested(m) as (foo):308 self.assertInWithManagerInvariants(m)309 self.assertAfterWithManagerInvariantsNoError(m)310311 def testSingleArgBoundToMultipleElementTupleError(self):312 def shouldThrowValueError():313 with Nested(mock_contextmanager_generator()) as (foo, bar):314 pass315 self.assertRaises(ValueError, shouldThrowValueError)316317 def testSingleArgUnbound(self):318 mock_contextmanager = mock_contextmanager_generator()319 mock_nested = MockNested(mock_contextmanager)320 with mock_nested:321 self.assertInWithManagerInvariants(mock_contextmanager)322 self.assertInWithManagerInvariants(mock_nested)323 self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)324 self.assertAfterWithManagerInvariantsNoError(mock_nested)325326 def testMultipleArgUnbound(self):327 m = mock_contextmanager_generator()328 n = mock_contextmanager_generator()329 o = mock_contextmanager_generator()330 mock_nested = MockNested(m, n, o)331 with mock_nested:332 self.assertInWithManagerInvariants(m)333 self.assertInWithManagerInvariants(n)334 self.assertInWithManagerInvariants(o)335 self.assertInWithManagerInvariants(mock_nested)336 self.assertAfterWithManagerInvariantsNoError(m)337 self.assertAfterWithManagerInvariantsNoError(n)338 self.assertAfterWithManagerInvariantsNoError(o)339 self.assertAfterWithManagerInvariantsNoError(mock_nested)340341 def testMultipleArgBound(self):342 mock_nested = MockNested(mock_contextmanager_generator(),343 mock_contextmanager_generator(), mock_contextmanager_generator())344 with mock_nested as (m, n, o):345 self.assertInWithGeneratorInvariants(m)346 self.assertInWithGeneratorInvariants(n)347 self.assertInWithGeneratorInvariants(o)348 self.assertInWithManagerInvariants(mock_nested)349 self.assertAfterWithGeneratorInvariantsNoError(m)350 self.assertAfterWithGeneratorInvariantsNoError(n)351 self.assertAfterWithGeneratorInvariantsNoError(o)352 self.assertAfterWithManagerInvariantsNoError(mock_nested)353354355class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):356 def testSingleResource(self):357 cm = mock_contextmanager_generator()358 def shouldThrow():359 with cm as self.resource:360 self.assertInWithManagerInvariants(cm)361 self.assertInWithGeneratorInvariants(self.resource)362 self.raiseTestException()363 self.assertRaises(RuntimeError, shouldThrow)364 self.assertAfterWithManagerInvariantsWithError(cm)365 self.assertAfterWithGeneratorInvariantsWithError(self.resource)366367 def testNestedSingleStatements(self):368 mock_a = mock_contextmanager_generator()369 mock_b = mock_contextmanager_generator()370 def shouldThrow():371 with mock_a as self.foo:372 with mock_b as self.bar:373 self.assertInWithManagerInvariants(mock_a)374 self.assertInWithManagerInvariants(mock_b)375 self.assertInWithGeneratorInvariants(self.foo)376 self.assertInWithGeneratorInvariants(self.bar)377 self.raiseTestException()378 self.assertRaises(RuntimeError, shouldThrow)379 self.assertAfterWithManagerInvariantsWithError(mock_a)380 self.assertAfterWithManagerInvariantsWithError(mock_b)381 self.assertAfterWithGeneratorInvariantsWithError(self.foo)382 self.assertAfterWithGeneratorInvariantsWithError(self.bar)383384 def testMultipleResourcesInSingleStatement(self):385 cm_a = mock_contextmanager_generator()386 cm_b = mock_contextmanager_generator()387 mock_nested = MockNested(cm_a, cm_b)388 def shouldThrow():389 with mock_nested as (self.resource_a, self.resource_b):390 self.assertInWithManagerInvariants(cm_a)391 self.assertInWithManagerInvariants(cm_b)392 self.assertInWithManagerInvariants(mock_nested)393 self.assertInWithGeneratorInvariants(self.resource_a)394 self.assertInWithGeneratorInvariants(self.resource_b)395 self.raiseTestException()396 self.assertRaises(RuntimeError, shouldThrow)397 self.assertAfterWithManagerInvariantsWithError(cm_a)398 self.assertAfterWithManagerInvariantsWithError(cm_b)399 self.assertAfterWithManagerInvariantsWithError(mock_nested)400 self.assertAfterWithGeneratorInvariantsWithError(self.resource_a)401 self.assertAfterWithGeneratorInvariantsWithError(self.resource_b)402403 def testNestedExceptionBeforeInnerStatement(self):404 mock_a = mock_contextmanager_generator()405 mock_b = mock_contextmanager_generator()406 self.bar = None407 def shouldThrow():408 with mock_a as self.foo:409 self.assertInWithManagerInvariants(mock_a)410 self.assertInWithGeneratorInvariants(self.foo)411 self.raiseTestException()412 with mock_b as self.bar:413 pass414 self.assertRaises(RuntimeError, shouldThrow)415 self.assertAfterWithManagerInvariantsWithError(mock_a)416 self.assertAfterWithGeneratorInvariantsWithError(self.foo)417418 # The inner statement stuff should never have been touched419 self.assertEqual(self.bar, None)420 self.assertFalse(mock_b.enter_called)421 self.assertFalse(mock_b.exit_called)422 self.assertEqual(mock_b.exit_args, None)423424 def testNestedExceptionAfterInnerStatement(self):425 mock_a = mock_contextmanager_generator()426 mock_b = mock_contextmanager_generator()427 def shouldThrow():428 with mock_a as self.foo:429 with mock_b as self.bar:430 self.assertInWithManagerInvariants(mock_a)431 self.assertInWithManagerInvariants(mock_b)432 self.assertInWithGeneratorInvariants(self.foo)433 self.assertInWithGeneratorInvariants(self.bar)434 self.raiseTestException()435 self.assertRaises(RuntimeError, shouldThrow)436 self.assertAfterWithManagerInvariantsWithError(mock_a)437 self.assertAfterWithManagerInvariantsNoError(mock_b)438 self.assertAfterWithGeneratorInvariantsWithError(self.foo)439 self.assertAfterWithGeneratorInvariantsNoError(self.bar)440441 def testRaisedStopIteration1(self):442 # From bug 1462485443 @contextmanager444 def cm():445 yield446447 def shouldThrow():448 with cm():449 raise StopIteration("from with")450451 self.assertRaises(StopIteration, shouldThrow)452453 def testRaisedStopIteration2(self):454 # From bug 1462485455 class cm(object):456 def __enter__(self):457 pass458 def __exit__(self, type, value, traceback):459 pass460461 def shouldThrow():462 with cm():463 raise StopIteration("from with")464465 self.assertRaises(StopIteration, shouldThrow)466467 def testRaisedStopIteration3(self):468 # Another variant where the exception hasn't been instantiated469 # From bug 1705170470 @contextmanager471 def cm():472 yield473474 def shouldThrow():475 with cm():476 raise iter([]).next()477478 self.assertRaises(StopIteration, shouldThrow)479480 def testRaisedGeneratorExit1(self):481 # From bug 1462485482 @contextmanager483 def cm():484 yield485486 def shouldThrow():487 with cm():488 raise GeneratorExit("from with")489490 self.assertRaises(GeneratorExit, shouldThrow)491492 def testRaisedGeneratorExit2(self):493 # From bug 1462485494 class cm (object):495 def __enter__(self):496 pass497 def __exit__(self, type, value, traceback):498 pass499500 def shouldThrow():501 with cm():502 raise GeneratorExit("from with")503504 self.assertRaises(GeneratorExit, shouldThrow)505506507class NonLocalFlowControlTestCase(unittest.TestCase):508509 def testWithBreak(self):510 counter = 0511 while True:512 counter += 1513 with mock_contextmanager_generator():514 counter += 10515 break516 counter += 100 # Not reached517 self.assertEqual(counter, 11)518519 def testWithContinue(self):520 counter = 0521 while True:522 counter += 1523 if counter > 2:524 break525 with mock_contextmanager_generator():526 counter += 10527 continue528 counter += 100 # Not reached529 self.assertEqual(counter, 12)530531 def testWithReturn(self):532 def foo():533 counter = 0534 while True:535 counter += 1536 with mock_contextmanager_generator():537 counter += 10538 return counter539 counter += 100 # Not reached540 self.assertEqual(foo(), 11)541542 def testWithYield(self):543 def gen():544 with mock_contextmanager_generator():545 yield 12546 yield 13547 x = list(gen())548 self.assertEqual(x, [12, 13])549550 def testWithRaise(self):551 counter = 0552 try:553 counter += 1554 with mock_contextmanager_generator():555 counter += 10556 raise RuntimeError557 counter += 100 # Not reached558 except RuntimeError:559 self.assertEqual(counter, 11)560 else:561 self.fail("Didn't raise RuntimeError")562563564class AssignmentTargetTestCase(unittest.TestCase):565566 def testSingleComplexTarget(self):567 targets = {1: [0, 1, 2]}568 with mock_contextmanager_generator() as targets[1][0]:569 self.assertEqual(targets.keys(), [1])570 self.assertEqual(targets[1][0].__class__, MockResource)571 with mock_contextmanager_generator() as targets.values()[0][1]:572 self.assertEqual(targets.keys(), [1])573 self.assertEqual(targets[1][1].__class__, MockResource)574 with mock_contextmanager_generator() as targets[2]:575 keys = targets.keys()576 keys.sort()577 self.assertEqual(keys, [1, 2])578 class C: pass579 blah = C()580 with mock_contextmanager_generator() as blah.foo:581 self.assertEqual(hasattr(blah, "foo"), True)582583 def testMultipleComplexTargets(self):584 class C:585 def __enter__(self): return 1, 2, 3586 def __exit__(self, t, v, tb): pass587 targets = {1: [0, 1, 2]}588 with C() as (targets[1][0], targets[1][1], targets[1][2]):589 self.assertEqual(targets, {1: [1, 2, 3]})590 with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]):591 self.assertEqual(targets, {1: [3, 2, 1]})592 with C() as (targets[1], targets[2], targets[3]):593 self.assertEqual(targets, {1: 1, 2: 2, 3: 3})594 class B: pass595 blah = B()596 with C() as (blah.one, blah.two, blah.three):597 self.assertEqual(blah.one, 1)598 self.assertEqual(blah.two, 2)599 self.assertEqual(blah.three, 3)600601602class ExitSwallowsExceptionTestCase(unittest.TestCase):603604 def testExitTrueSwallowsException(self):605 class AfricanSwallow:606 def __enter__(self): pass607 def __exit__(self, t, v, tb): return True608 try:609 with AfricanSwallow():610 1/0611 except ZeroDivisionError:612 self.fail("ZeroDivisionError should have been swallowed")613614 def testExitFalseDoesntSwallowException(self):615 class EuropeanSwallow:616 def __enter__(self): pass617 def __exit__(self, t, v, tb): return False618 try:619 with EuropeanSwallow():620 1/0621 except ZeroDivisionError:622 pass623 else:624 self.fail("ZeroDivisionError should have been raised")625626627def test_main():628 run_unittest(FailureTestCase, NonexceptionalTestCase,629 NestedNonexceptionalTestCase, ExceptionalTestCase,630 NonLocalFlowControlTestCase,631 AssignmentTargetTestCase,632 ExitSwallowsExceptionTestCase)633634635if __name__ == '__main__': ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful