What are the pros and cons of automated Unit Tests vs automated Integration tests?

前端 未结 12 1922
遥遥无期
遥遥无期 2020-12-12 20:35

Recently we have been adding automated tests to our existing java applications.

What we have

The majority of these tests are integration tes

12条回答
  •  隐瞒了意图╮
    2020-12-12 21:07

    We have 4 different types of tests in our project:

    1. Unit tests with mocking where necessary
    2. DB tests that act similar to unit tests but touch db & clean up afterwards
    3. Our logic is exposed through REST, so we have tests that do HTTP
    4. Webapp tests using WatiN that actually use IE instance and go over major functionality

    I like unit tests. They run really fast (100-1000x faster than #4 tests). They are type safe, so refactoring is quite easy (with good IDE).

    Main problem is how much work is required to do them properly. You have to mock everything: Db access, network access, other components. You have to decorate unmockable classes, getting a zillion mostly useless classes. You have to use DI so that your components are not tightly coupled and therefore not testable (note that using DI is not actually a downside :)

    I like tests #2. They do use the database and will report database errors, constraint violations and invalid columns. I think we get valuable testing using this.

    #3 and especially #4 are more problematic. They require some subset of production environment on build server. You have to build, deploy and have the app running. You have to have a clean DB every time. But in the end, it pays off. Watin tests require constant work, but you also get constant testing. We run tests on every commit and it is very easy to see when we break something.

    So, back to your question. Unit tests are fast (which is very important, build time should be less than, say, 10 minutes) and the are easy to refactor. Much easier than rewriting whole watin thing if your design changes. If you use a nice editor with good find usages command (e.g. IDEA or VS.NET + Resharper), you can always find where your code is being tested.

    With REST/HTTP tests, you get a good a good validation that your system actually works. But tests are slow to run, so it is hard to have a complete validation at this level. I assume your methods accept multiple parametres or possibly XML input. To check each node in XML or each parameter, it would take tens or hundreds of calls. You can do that with unit tests, but you cannot do that with REST calls, when each can take a big fraction of a second.

    Our unit tests check special boundary conditions far more often than #3 tests. They (#3) check that main functionality is working and that's it. This seems to work pretty well for us.

提交回复
热议问题