Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing-- fundamental goal?

Me and my co-workers had a bit of a disagreement last night about unit testing in our PHP/MySQL application. Half of us argued that when unit testing a function within a class, you should mock everything outside of that class and its parents. The other half of us argued that you SHOULDN'T mock anything that is a direct dependancy of the class either.

The specific example was our logging mechanism, which happened through a static Logging class, and we had a number of Logging::log() calls in various locations throughout our application. The first half of us said the Logging mechanism should be faked (mocked) because it would be tested in the Logging unit tests. The second half of us argued that we should include the original Logging class in our unit test so that if we make a change to our logging interface, we'll be able to see if it creates problems in other parts of the application due to failing to update the call interface.

So I guess the fundamental question is-- do unit tests serve to test the functionality of a single unit in a closed environment, or show the consequences of changes to a single unit in a larger environment? If it's one of these, how do you accomplish the other?

like image 923
David Avatar asked May 04 '10 01:05

David


1 Answers

You and your colleagues have stumbled upon the difference between unit tests and integration tests. Mocking everything would be done for the former; not mocking dependencies would be done for the latter.

Of course, where you draw the line for granularity is sort of subjective, too - but at the finest level of detail for unit testing, you shouldn't be worrying about anything outside the specific subject of each test.

like image 193
Amber Avatar answered Sep 19 '22 13:09

Amber