[ create a new paste ] login | about

Link: http://codepad.org/0pURCZml    [ raw code | output | fork ]

Python, pasted on Apr 16:
import unittest
from coverage import coverage

cov = coverage(timid = True, branch = True)
cov.start()

class SUT(object):
    
    def problematicMethod(self):
        """"""

        try:
            with open(r"afile.txt", "r") as fp:
                fp.read()
        except IOError as e:
            print e
        finally:
            print "finally in the finally"

        return True

class CM_stub_open(object):
    def __init__(self):
        self._open = None

    def __enter__(self):
        self._open = __builtins__.open
        return self

    def __exit__(self, _t, _v, _tb):
        __builtins__.open = self._open

    def stub_ioerror(self):
        def stub(*args, **kwargs):
            raise IOError("an IO Error")
        __builtins__.open = stub
    def stub_windowserror(self):
        def stub(*args, **kwargs):
            raise WindowsError("a Windows Error")
        __builtins__.open = stub

class CM_stub_open_read(object):
    def __init__(self):
        self._open = None

    def __enter__(self):
        self._open = __builtins__.open
        return self

    def __exit__(self, _t, _v, _tb):
        __builtins__.open = self._open

    def stub_ioerror(self):
        class Stub(object):
            def __init__(self, *args, **kwargs): pass
            def __enter__(self):                 return self
            def __exit__(self, _t, _v, _tb):     pass
            def read(self, *args, **kwargs):
                raise IOError("an IO Error")
        __builtins__.open = Stub

    def stub_windowserror(self):
        class Stub(object):
            def __init__(self, *args, **kwargs): pass
            def __enter__(self):                 return self
            def __exit__(self, _t, _v, _tb):     pass
            def read(self, *args, **kwargs):
                raise WindowsError("a Windows Error")
        __builtins__.open = Stub

class Test(unittest.TestCase):

    def test_01_normal(self):
        testObject = SUT()
        self.assertTrue(testObject.problematicMethod())

    def test_02(self):
        testObject = SUT()
        with CM_stub_open() as stub:
            stub.stub_ioerror()
            self.assertTrue(testObject.problematicMethod())

    def test_03(self):
        testObject = SUT()
        with CM_stub_open() as stub:
            stub.stub_windowserror()
            self.assertRaises(WindowsError, testObject.problematicMethod)

    def test_04(self):
        testObject = SUT()
        with CM_stub_open_read() as stub:
            stub.stub_ioerror()
            self.assertTrue(testObject.problematicMethod())

    def test_05(self):
        testObject = SUT()
        with CM_stub_open_read() as stub:
            stub.stub_windowserror()
            self.assertRaises(WindowsError, testObject.problematicMethod)

try:
    unittest.main()
finally:
    cov.stop()
    cov.html_report()


Output:
1
2
3
4
5
t.py:13: Warning: 'with' will become a reserved keyword in Python 2.6
  Line 13
    with open(r"afile.txt", "r") as fp:
            ^
SyntaxError: invalid syntax


Create a new paste based on this one


Comments: