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

前端 未结 12 1898
遥遥无期
遥遥无期 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:08

    The thing that distinguishes Unit tests and Integration tests is the number of parts required for the test to run.

    Unit tests (theoretically) require very (or no) other parts to run. Integration tests (theoretically) require lots (or all) other parts to run.

    Integration tests test behaviour AND the infrastructure. Unit tests generally only test behaviour.

    So, unit tests are good for testing some stuff, integration tests for other stuff.

    So, why unit test?

    For instance, it is very hard to test boundary conditions when integration testing. Example: a back end function expects a positive integer or 0, the front end does not allow entry of a negative integer, how do you ensure that the back end function behaves correctly when you pass a negative integer to it? Maybe the correct behaviour is to throw an exception. This is very hard to do with an integration test.

    So, for this, you need a unit test (of the function).

    Also, unit tests help eliminate problems found during integration tests. In your example above, there are a lot of points of failure for a single HTTP call:

    the call from the HTTP client the servlet validation the call from the servlet to the business layer the business layer validation the database read (hibernate) the data transformation by the business layer the database write (hibernate) the data transformation -> XML the XSLT transformation -> HTML the transmission of the HTML -> client

    For your integration tests to work, you need ALL of these processes to work correctly. For a Unit test of the servlet validation, you need only one. The servlet validation (which can be independent of everything else). A problem in one layer becomes easier to track down.

    You need both Unit tests AND integration tests.

提交回复
热议问题