using self in python @patch decorator

时间秒杀一切 提交于 2019-12-07 09:12:02

问题


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.



来源:https://stackoverflow.com/questions/26979285/using-self-in-python-patch-decorator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!