I have been reading about unit tests and functional tests for a while now.
If I write exhaustive functional tests, won't they in-turn cover the units underneath as well, which in-turn make the unit tests redundant?
We follow agile and we write the functional tests using WebDriver as soon as we are done with the 'slice' of the functionality, which is typically 2-4 weeks of sprint time.
Perhaps, but not necessarily.
Functional tests can be thought of as "Black Box" tests whereby you are looking at a specific function (regardless if that's a single method, module, system, etc) and checking that for a given input, you get a given output.
However, if the functional test fails all you can say is that the system is faulty; it doesn't necessarily give you any indication of what part of the system is to blame. Of course you will go off and diagnose the problem but you don't know up front what the problem is.
e.g
In the above case, you don't know if it's because the UserService's
GetUserByID()
function is faulty - perhaps it did not callrepo.GetUserByID
and just returned null, perhaps it did get aUser
fromrepo
but accidentally then returned an uninitialized temp variable, etc - or perhaps it's because the dependency (repo
) itself is faulty. In any case, you'll have to debug into the issue.Unit tests, on the other hand, are more like "White Box" tests whereby you have effectively taken the cover off the system and are testing how the system behaves in order to do what you want it to do.
e.g. (code below may not compile and is just for demo purposes)
In this case you are explicitly verifying that
sut.GetUserByID()
behaves in the expected way - that it invokes therepo.GetUserByID()
and returns resultant object, without mangling it or changing it.If this unit test passes, but the functional test fails, you can say with confidence that the problem does not lie with the
UserService
, but with theUserRepository
class, without even looking at the code. That may or may not save you time, it really depends on how complex your functional units are.