Integrating Mock And Patch In A Python Unit Test
I have a class with a few methods that I am writing unit test cases for. For minimum reproducible example, I am attaching 3 of the methods from that class: Class that I am testing
Solution 1:
Ok, trying to give you an idea. In the tests, you have created a fake addCookie
method for your tests, but you only use it to check how addCookie
has been called. So, for example, your test 3 and 4 you could rewrite:
deftest_03_set_nonce(self):
request = mock.Mock()
self.web_session.set_nonce(request)
# we only need to know that it was called once
request.addCookie.assert_called_once()
deftest_04_get_valid_nonce(self):
request = mock.Mock()
web_session = WebViewLincSession()
web_session.set_nonce(request)
web_session.set_nonce(request)
# check that addCookie it has been called twice
self.assertEqual(2, request.addCookie.call_count)
valid_nonce = web_session.get_valid_nonce()
... # the rest is not dependent on mocks
In other tests, you may have also to check the arguments used in the calls. You always have to define what you are actually want to test, and then setup your mocks so that only that functionality is tested.
Note also that in some cases it may make sense to use extra mock classes like you have done - there is nothing wrong with that, if that works best for you.
Post a Comment for "Integrating Mock And Patch In A Python Unit Test"