Ok, I know I am going out on a limb making a statement like that, so my question is for everyone to convince me I am wrong. Take this scenario:
I have method A, which calls method B, and they are in different layers.
So I unit test B, which delivers null as a result. So I test that null is returned, and the unit test passes. Nice.
Then I unit test A, which expects an empty string to be returned from B. So I mock the layer B is in, an empty string is return, the test passes. Nice again. (Assume I don't realize the relationship of A and B, or that maybe two differente people are building these methods)
My concern is that we don't find the real problem until we test A and B togther, i.e. Integration Testing. Since an integration test provides coverage over the unit test area, it seems like a waste of effort to build all these unit tests that really don't tell us anything (or very much) meaningful.
Why am I wrong?
While unit tests always take results from a single unit, such as a function call, integration tests may aggregate results from various parts and sources. In an integration test, there is no need to mock away parts of the application. You can replace external systems, but the application works in an integrated way.
Generally speaking, unit tests are cheaper. They're easier to write—unless you're trying to add them to an existing app—which means developers don't spend quite as much time writing them. They're also cheaper to run: they don't usually require you to do special things for your environment or obtain external resources.
Integration tests can not be used with TDD approach. Integration tests are slow and can not be executed very often. In most cases integration tests do not indicate the source of the problem. it's more difficult to create test environment with integration tests.
Here's an article on test categorization with some arguments
I won't mention the benefits of testing as a whole, once we're just comparing unit tests vs. functional tests:
In the ideal world, when a test fails, you won't need more than 2 minutes to know what to look (with no need of debugging). The idea of having tests of all sizes is just a guideline to achieve this goal, rather than spending hours/days/weeks debugging =).
Unit tests are not for testing what should be tested in integration - they are complementary sets of tests. Unit tests guarantee that a given unit of code on its own performs what it was designed to do, nothing else. Integration tests make sure all your units work well together to perform what the overall requirements asked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With