A less technical view of unit testing would be driving a car around a lab, race track, or some other controlled environment. Integration testing would be the equivalent of driving the car outside in the real world. So testing self driving Google Cars around Austin would be integration testing for Google Cars.
At first glance it can be difficult to distinguish between unit and integration testing. There are tools for both that sound like they do exactly the same thing and even after some discussion the line stays blurry. On the project I’m working on integration tests model the users anticipate behavior which includes opening the website, registering, logging in, sending messages, and logging out. While the unit tests focus on a single piece of functionality. For example does the AngularJS controller process a users credentials correctly, does the method getFullName() correctly return “John Doe”, and does it perform the correct AJAX REST requests.
Unit testing uses mocking to simulate a back-end or a service. The developer assumes the back-end functions correctly and the unit tests will run in the console without ever actually hitting a Database or a REST service. These tests raise confidence in the code, but does not fully test the real world conditions. What happens if the user is using Internet Explorer where the Angular $digest method doesn’t function correctly.
Integration tests actually open the website, for example navigate to Google.com, type in “Hello World”, and click “Google Search”. This test will take into consideration that the back-end might not function correctly because it uses the browser from a users perspective. It will follow through the story as a regular user would using the browser of choice- Firefox, Internet Explorer, Safari, or Chrome.
There’s plenty of tools like Protractor, Jasmine, and JUnit for unit testing code.
Then there’s Selenium for integration testing.
If integration testing tests the real world why would we ever use unit testing? The testing suite we have consists of about 700 unit tests and finishes in about 2 minutes. We also have about 30 integration tests which take about 25 minutes. This time to test ratio is pretty high. In integration testing there will be overhead with every single step of the way, loading the browser, file/resource transfer, page load times, and even element interaction.
When to use integration testing? Because of the time required to run integration tests, it’s more efficient to initially focus on code coverage and unit tests. As the application matures and focus shifts to reliability integration testing needs to begin.