Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration testing - can it be done right?

Tags:

unit-testing

I used TDD as a development style on some projects in the past two years, but I always get stuck on the same point: how can I test the integration of the various parts of my program?

What I am currently doing is writing a testcase per class (this is my rule of thumb: a "unit" is a class, and each class has one or more testcases). I try to resolve dependencies by using mocks and stubs and this works really well as each class can be tested independently. After some coding, all important classes are tested. I then "wire" them together using an IoC container. And here I am stuck: How to test if the wiring was successfull and the objects interact the way I want?

An example: Think of a web application. There is a controller class which takes an array of ids, uses a repository to fetch the records based on these ids and then iterates over the records and writes them as a string to an outfile.

To make it simple, there would be three classes: Controller, Repository, OutfileWriter. Each of them is tested in isolation.

What I would do in order to test the "real" application: making the http request (either manually or automated) with some ids from the database and then look in the filesystem if the file was written. Of course this process could be automated, but still: doesn´t that duplicate the test-logic? Is this what is called an "integration test"? In a book i recently read about Unit Testing it seemed to me that integration testing was more of an anti-pattern?

like image 267
Max Avatar asked May 19 '10 14:05

Max


People also ask

Can integration testing be done manually?

Manual and Automated Integration Testing In most cases, integration testing doesn't require any specific tools. A QA team often runs these tests manually. Usually, it happens in parallel with the development – that's probably the most efficient timing.

What is the major problem during integration testing?

Often at the time of module development, user requirements change and these new requirements may not be unit tested. This instigates issues. Issues like data formatting, error trapping, hardware interfaces, and third-party service interfaces are sometimes missed during unit testing.

Is integration testing good?

Integration testing is especially important for large and complex software projects because there are more interactions between different pieces of the codebase, but it is used across a wide variety of projects. On the software testing pyramid, unit tests sit at the very bottom.


2 Answers

IMO, and I have no literature to back me on this, but the key difference between our various forms of testing is scope,

  • Unit testing is testing isolated pieces of functionality [typically a method or stateful class]
  • Integration testing is testing the interaction of two or more dependent pieces [typically a service and consumer, or even a database connection, or connection to some other remote service]
  • System integration testing is testing of a system end to end [a special case of integration testing]

If you are familiar with unit testing, then it should come as no surprise that there is no such thing as a perfect or 'magic-bullet' test. Integration and system integration testing is very much like unit testing, in that each is a suite of tests set to verify a certain kind of behavior.

For each test, you set the scope which then dictates the input and expected output. You then execute the test, and evaluate the actual to the expected.

In practice, you may have a good idea how the system works, and so writing typical positive and negative path tests will come naturally. However, for any application of sufficient complexity, it is unreasonable to expect total coverage of every possible scenario.

Unfortunately, this means unexpected scenarios will crop up in Quality Assurance [QA], PreProduction [PP], and Production [Prod] cycles. At which point, your attempts to replicate these scenarios in dev should make their way into your integration and system integration suites as automated tests.

Hope this helps, :)


ps: pet-peeve #1: managers or devs calling integration and system integration tests "unit tests" simply because nUnit or MsTest was used to automate it ...

like image 50
johnny g Avatar answered Sep 23 '22 18:09

johnny g


What you describe is indeed integration testing (more or less). And no, it is not an antipattern, but a necessary part of the sw development lifecycle.

Any reasonably complicated program is more than the sum of its parts. So however well you unit test it, you still have not much clue about whether the whole system is going to work as expected.

There are several aspects of why it is so:

  • unit tests are performed in an isolated environment, so they can't say anything about how the parts of the program are working together in real life
  • the "unit tester hat" easily limits one's view, so there are whole classes of factors which the developers simply don't recognize as something that needs to be tested*
  • even if they do, there are things which can't be reasonably tested in unit tests - e.g. how do you test whether your app server survives under high load, or if the DB connection goes down in the middle of a request?

* One example I just read from Luke Hohmann's book Beyond Software Architecture: in an app which applied strong antipiracy defense by creating and maintaining a "snapshot" of the IDs of HW components in the actual machine, the developers had the code very well covered with unit tests. Then QA managed to crash the app in 10 minutes by trying it out on a machine without a network card. As it turned out, since the developers were working on Macs, they took it for granted that the machine has a network card whose MAC address can be incorporated into the snapshot...

like image 37
Péter Török Avatar answered Sep 23 '22 18:09

Péter Török