unittest patch
¶
patch
THE FILE IT'S USED IN, not where it's defined- the file only knows about what files are exported
- Min 7:15 of video
- Lisa Roach - Demystifying the Patch Function - PyCon 2018
- 3 ways of patching
@patch("path.where.fn_is_used.fn", autospec=True, spec_set=True)
class TestStuff(ApiTestCase):
def test_stuff(self, mock_my_function):
You can also use your own function
@patch("function.path", MagicMock())
@patch("function.path", return_value=True)
@patch("function.path", my_mock_function)
[patch
Arguments]¶
Lisa Roach - Demystifying the Patch Function - PyCon 2018
Min 19:20
spec
: doesn't know about attributes of attributes- should always at least
spec
- should always at least
autospec
doesn't know about dynamically created attributes- example: class attributes
- work around: set the attribute that exists
- can be dangerous because it can trigger code on introspection
- slows down tests 🐌
spec_set=True
prevent setting properties that don't existnew_callable=MagicMock
is the defaultnew_callable=PropertyMock
kwargs
- `return_value="lisa"
name="lisa"
patch.object
patch.dict
-
patch.multiple
-@patch("path.where.fn1")
-@patch("path.where.fn2")
-@patch("path.where.fn3")
class TestStuff(ApiTestCase):
+ def setUp(self):
+ mock_fn1 = patch("path.where_fn1").start()
+ mock_fn2 = patch("path.where_fn2").start()
+ mock_fn3 = patch("path.where_fn3").start()
+ self.addCleanup(patch.stopall)
- def test_stuff(self, mock_fn1, mock_fn2, mock_fn3):
+ def test_stuff(self):
...
- def test_stuff2(self, mock_fn1, mock_fn2, mock_fn3):
+ def test_stuff2(self):
...
- You need
mock_check_output
because the decorator adds it- what does it do???????????
patch for as little as possible, especially for
or use setUp() and tearDown()
Last update:
2023-04-24