示例#1
0
import sys

if sys.version_info[:2] >= (2, 5):
    from tests._testwith import *
else:
    from tests.support import unittest2

    class TestWith(unittest2.TestCase):

        @unittest2.skip('tests using with statement skipped on Python 2.4')
        def testWith(self):
            pass


if __name__ == '__main__':
    unittest2.main()
示例#2
0
            self.assertEqual(something, mock_something, "restored with wrong instance")

        self.assertEqual(something, sentinel.Something, "not restored")

    def testWithStatementImbricated(self):
        with patch('tests._testwith.something') as mock_something:
            self.assertEqual(something, mock_something, "unpatched")

            with patch('tests._testwith.something_else') as mock_something_else:
                self.assertEqual(something_else, mock_something_else, "unpatched")

        self.assertEqual(something, sentinel.Something)
        self.assertEqual(something_else, sentinel.SomethingElse)

    def testDictContextManager(self):
        foo = {}
        with patch.dict(foo, {'a': 'b'}):
            self.assertEqual(foo, {'a': 'b'})
        self.assertEqual(foo, {})

        with self.assertRaises(NameError):
            with patch.dict(foo, {'a': 'b'}):
                self.assertEqual(foo, {'a': 'b'})
                raise NameError('Konrad')

        self.assertEqual(foo, {})


if __name__ == '__main__':
    unittest2.main()