using self in python @patch decorator

2019-05-06 21:14发布

问题:

I'm trying to use python's mock.patch to implement unit tests with nose.

class A:

    def setUp(self):
        self.b = 8 #contrived example

    @patch.object('module.class', 'function', lambda x: self.b)
    def testOne(self):
        # do test #

Here, patch complains that it doesnt know self (which is correct). What is best way to get this kind of functionality in a clean fashion?

I know I can use a global variable, or that I can mock it within the test (but that involves me cleaning up the objects at the end of the test).

回答1:

You cannot use self on method decorator because you are in the class definition and the object doesn't exist. If you really want to access to self and not just use some static values you can consider follow approach: totest is a module in my python path and fn is the method that I would patch, moreover I'm using a fixed return_value instead a function for a more readable example

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.b = 8 #contrived example

    def testOne(self):
        with patch('totest.fn', return_value=self.b) as m:
            self.assertEqual(self.b, m())
            self.assertTrue(m.called)

    @patch("totest.fn")
    def testTwo(self,m):
        m.return_value = self.b
        self.assertEqual(self.b, m())
        self.assertTrue(m.called)

In testOne() I use patch as a context and I will have the full access to self. In testTwo() (that is my standard way) I set up my mock m at the start of the test and then use it.

Finally I used patch() instead of patch.object() because I don't really understand why you need patch.object() but you can change it as you like.