Unit Tests Vs Integration Tests

Unit testing

A unit test tests small pieces of code or individual functions alone and isolated. A unit tests should essentially just give the function that is tested some inputs, and then check what the function outputs is correct. The idea behind Unit testing is to test each part of the program and show that the individual parts are correct. Unit test does not verify whether code works with external dependencies correctly.

Integration testing

An integration test tests how parts of the system work together. Integration tests are similar to unit tests, but there’s one big difference: while unit tests are isolated from other components, integration tests are not. The idea behind Integration testing is to combine modules in the application and test them as a group to see that they are working fine. Integration tests verifies that code works with external dependencies correctly.


Let’s consider this simple example. Say we have 3 functions in our program: summultiply, and combine.

function sum(a, b) {
  return a + b;
}

function multiply(a, b) {
 return a * b; 
}

function combine(a, b) { 
 return sum(a, b) + multiply(a, b);
}

Unit tests forsumand multiply functions would be:

it('should add numbers correctly', () => {
  expect(sum(1,2)).toBe(3);
}
it('should multiply numbers correctly', () => {
  expect(multiply(1,2)).toBe(2);
}

For combine function unit testing we need to reassign sum and multiply functions to get rid of external dependencies  – unit tests should have no dependencies on code outside the unit tested.

function sum() {
 return 1 + 2;
 }
 function multiply() {
 return 1 * 2;
 }
it('should combine functions sum and multiply correctly', () => {
  expect(combine(1,2)).toBe(5);
}

But integration test for combine function would be:

it('should combine functions sum and multiply correctly', () => {
  expect(combine(1,2)).toBe(5);
}

Integration testing is useful for situations where unit testing is not enough. For example if we need to ensure that two separate systems – like a database and your app – work together correctly.

Integration tests are often slower than unit tests because of the added complexity. They also might need some additional set up or configuration, such as the setting up of a test database, which makes writing and maintaining them harder than unit tests. Usually there are fewer integration tests than unit tests in a test suite, because integration tests are mainly used to test how different modules are interacting with each other as parts of an overall system.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s