def test1(x, mock_func): #exemplo usando o patch como decorator #usa example.func modificada example.func(x) mock_func.assert_called_with(x) #exemplo usando o patch como objeto de contexto with patch('example.func') as mocker: #usa example.func modificada example.func(x) mocker.assert_called_with(x) #exemplo usanod o patch para modificar os objetos manualmente @patch('example.func1') @patch('example.func2') @patch('example.func3') def test1(mock_func3, mock_func2, mock_func1): pass #exemplo usando patch como objeto de contexto dentro de uma function def test2(): with patch('example.patch1') as mock1, \ patch('example.patch2') as mock2, \ patch('example.patch3') as mock3: pass
def test1(x, mock_func, mock_func1, mock_func2): example.func(x) example.func1() example.func2() # print('x=',x) mock_func.assert_called_with(x) mock_func1.assert_called_with() mock_func2.assert_called_with() print('in test1')
def test1(x, mock_func): example.func(x) # Uses patched example.func mock_func.assert_called_with(x)
#!/usr/bin/env python # coding: utf-8 from unittest.mock import patch import example @patch('example.func') def test1(x, mock_func): example.func(x) # Uses patched example.func mock_func.assert_called_with(x) with patch('example.func') as mock_func: example.func(x) # Uses patched example.func mock_func.assert_called_with(x) p = patch('example.func') mock_func = p.start() example.func(x) mock_func.assert_called_with(x) p.stop() @patch('example.func1') @patch('example.func2') @patch('example.func3') def test1(mock1, mock2, mock3): ... def test2(): with patch('example.patch1') as mock1, patch('example.patch2') as mock2, patch('example.patch3') as mock3: x = 42 with patch('__main__.x'): print(x)
def test1(x): with patch('example.func') as mock_func: example.func(x) mock_func.assert_called_with(x)
import example example.func(1000000)
def test(x, mock_func): # Uses patched example.func example.func(x) mock_func.assert_called_with(x)
def test3(self): p = patch('example.func') mock_func = p.start() example.func(3) mock_func.assert_called_with(3) p.stop()
def test2(self): with patch('example.func') as mock_func: example.func(3) # Uses patched example.func mock_func.assert_called_with(3)
def test1(self, mock_func): example.func(3) mock_func.assert_called_with(3)
def test_func(): assert func(2) == 4
def test1(x): p = patch('example.func') mock_func = p.start() example.func(x) mock_func.assert_called_with(x) p.stop()
def test_func(x, mock_func): example.func(x) mock_func.assert_called_with(x)
def test1(x, mock_func): example.func(x) # print('x=',x) mock_func.assert_called_with(x) print('in test1')
def test_func(): assert func(4) == 4
def test_answer(): assert func() == 42
# @description: 单元测试中需要给指定的对象打补丁,用来断言它们在测试中的期望行为 # from unittest.mock import patch import example @patch('example.func') def test(x, mock_func): # Uses patched example.func example.func(x) mock_func.assert_called_with(x) with patch('example.func') as mock_func: # Uses patched example.func example.func(x) mock_func.assert_called_with(x) def main(): p = patch('example.com') mock_func = p.start() example.func(x) mock_func.assert_called_with(x) p.stop() if __name__ == '__main__': main()
It’s a little unusual, but patch() can be used as a decorator, a context manager, or stand-alone. For example, here’s an example of how it’s used as a decorator: """ from unittest.mock import patch import example @patch('example.func') def test1(x, mock_func): example.func(x) # Uses patched example.func mock_func.assert_called_with(x) """ It can also be used as a context manager: """ with patch('example.func') as mock_func: example.func(x) # Uses patched example.func mock_func.assert_called_with(x) """ Last, but not least, you can use it to patch things manually: """ p = patch('example.func') mock_func = p.start() example.func(x) mock_func.assert_called_with(x) p.stop() """ If necessary, you can stack decorators and context managers to patch multiple objects. For example: @patch('example.func1')
def main(): p = patch('example.com') mock_func = p.start() example.func(x) mock_func.assert_called_with(x) p.stop()