1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
# Copyright (C) 2007-2009 Michael Foord
# E-mail: fuzzyman AT voidspace DOT org DOT uk
# http://www.voidspace.org.uk/python/mock/
from __future__ import with_statement
import os
import sys
this_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if not this_dir in sys.path:
sys.path.insert(0, this_dir)
from testcase import TestCase
from testutils import RunTests
if __name__ == '__main__':
sys.modules['testwith'] = sys.modules['__main__']
if 'testwith' in sys.modules:
# Fix for running tests under Wing
import tests
import testwith
tests.testwith = testwith
from mock import Mock, patch, patch_object, sentinel
something = sentinel.Something
something_else = sentinel.SomethingElse
class WithTest(TestCase):
def testWithStatement(self):
with patch('tests.testwith.something', sentinel.Something2):
self.assertEquals(something, sentinel.Something2, "unpatched")
self.assertEquals(something, sentinel.Something)
def testWithStatementException(self):
try:
with patch('tests.testwith.something', sentinel.Something2):
self.assertEquals(something, sentinel.Something2, "unpatched")
raise Exception('pow')
except Exception:
pass
else:
self.fail("patch swallowed exception")
self.assertEquals(something, sentinel.Something)
def testWithStatementAs(self):
with patch('tests.testwith.something') as mock_something:
self.assertEquals(something, mock_something, "unpatched")
self.assertTrue(isinstance(mock_something, Mock), "patching wrong type")
self.assertEquals(something, sentinel.Something)
def testPatchObjectWithStatementAs(self):
mock = Mock()
original = mock.something
with patch_object(mock, 'something') as mock_something:
self.assertNotEquals(mock.something, original, "unpatched")
self.assertEquals(mock.something, original)
def testWithStatementNested(self):
from contextlib import nested
with nested(patch('tests.testwith.something'),
patch('tests.testwith.something_else')) as (mock_something, mock_something_else):
self.assertEquals(something, mock_something, "unpatched")
self.assertEquals(something_else, mock_something_else, "unpatched")
self.assertEquals(something, sentinel.Something)
self.assertEquals(something_else, sentinel.SomethingElse)
def testWithStatementSpecified(self):
with patch('tests.testwith.something', sentinel.Patched) as mock_something:
self.assertEquals(something, mock_something, "unpatched")
self.assertEquals(mock_something, sentinel.Patched, "wrong patch")
self.assertEquals(something, sentinel.Something)
if __name__ == '__main__':
RunTests(WithTest)
|