Presenter-First Unittest with multiple statements

I am trying to use the Presenter-First approach to a new project. I am with unittest below. Am I using a bad testing technique by including so many statements in this test? If so, is there a problem with my test approach or implementation of presenter.setOverview? In other words, should the setOverview method call self.setSalesQty and not self.view.setSalesQty? In this case, I will have a separate test for presenter.setSalesQty, and the testSetOverview test will no longer need to worry about it.

def testSetOverview(self): # set up mock objects p = PropertyMock() type(self.mock_model).descriptions = p self.mock_model.getData.side_effect = [5, 10] self.mock_model.getDescription.side_effect = 'Description' # get required variables end = dt.date.today() start = dt.date(year=end.year, month=1, day=1) pn = 'abcd' # call presenter method self.presenter.setOverview(pn) # test to make sure proper calls were made model_getData_calls = [call(pn=pn, start=start, end=end, data=self.mock_model.SHIPPED_QUANTITY), call(pn=pn, start=start, end=end, data=self.mock_model.PRICE_PAID)] self.mock_model.getData.assert_has_calls(model_getData_calls, any_order=True) assert self.mock_model.getDescription.called self.mock_view.setSalesQty.assert_called_with(val=5) self.mock_view.setSalesDols.assert_called_with(val=10) self.mock_view.setDescription.assert_called_with(val='Description') 
+5
source share
1 answer

So, when writing unit tests, you would like to test one specific thing. Because when you write more code and the test fails, it will be much easier for you to understand what happened in the unit test. It is possible that with the statements that you have made so far, you test one behavior or functionality of the code, then the statements are in order.

To make an example, you have two functions below list_counter dependent on word_count . Therefore, when testing list_counter you can make two statements to make sure that the two components in list_counter are correct. But it would probably be wiser to test word_count separately.

 def word_count(word): return len(word) def list_counter(listing=None): total = 0 for l in listing: total += word_count(l) return (len(listing), total) 

It is difficult to comment on your case in more detail, since I do not have access to how the model looks. self.mock_view also appears out of nowhere.

+1
source

Source: https://habr.com/ru/post/1236844/


All Articles