Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I bother with unit testing if I can just use integration tests?

Tags:

unit-testing

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?

like image 387
CodeGrue Avatar asked Apr 09 '10 17:04

CodeGrue


People also ask

Why integration tests are better than unit tests?

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.

Should I write unit tests or integration tests?

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.

Can we do integration testing without unit testing?

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.


2 Answers

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:

  • Unit testing helps you reduce the scope where to look when there's an error. - Let's include classes C, D, E, ..., Z in this scenario. If you have only integration test and it fails, where do you start looking? If you don't have unit tests, you would need to look everywhere inside each of those classes AND the "wiring" between those (which is a narrower scope).If you had unit tests, then you'd just need to check the wiring. In this case, it would also be a bad thing not having some smaller integration tests, like testing A, B, C and D only (so you already know if the "wiring" between those are in fact working).
  • With unit tests, you fail faster. This is even more true if you TDD. You probably write your tests after you created all of class A and B. When you run your test, the way A and B works is not as fresh in your mind (maybe you wrote them the week before - hopefully you don't start testing only when the product is "finished"). You must then remember what you were thinking when you wrote those. Also, unit test are faster, so you're more likely to run them more frequently (perhaps run them automatically every time you save?)
  • Unit tests provide a better documentation how your class should behave. If you're a "normal programmer", you probably hate writing documentation. This forces you to write documentation while programming, and forces the documentation to never be obsolete (if it is, your tests fail). And it also helps when you need to change somebody else's code.

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 =).

like image 72
9 revs Avatar answered Oct 08 '22 20:10

9 revs


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.

like image 10
Otávio Décio Avatar answered Oct 08 '22 18:10

Otávio Décio