简体   繁体   中英

Python mock method attributed “called” is still False after method is called

I'm testing that a method gets called with the Python mock library. The outer method is this:

def get_abc():
    get_a()
    get_b()
    get_c(False)

The test case is like this:

@mock.patch('myclass.get_a')
@mock.patch('myclass.get_b')
@mock.patch('myclass.get_c')
def test_inner_methods(self, mock_meth_1, mock_meth_2, mock_meth_3):
    o = Outerclass(config_file=cfg)
    o._get_abc()
    self.assertTrue(mock_meth_1.called)
    mock_meth_1.assert_called_with(False)

When I follow in debug, get_c() is called successfully but the called attribute of mock_meth_1 never changes. Do I need to do more to properly mock the method?

You patched myclass.get_c twice, so I don't know how it will behave, but it's probably not what you meant to do. Switch one of them to myclass.get_a and you'll probably be fine.

You might also find mock_meth1.assert_called() easier than self.assertTrue(mock_meth_1.called) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM