What is the difference between Unit Tests and Integration Tests?

What is the difference between Unit Tests and Integration Tests?

A lot of people are confused about the difference between the various types of tests that you can write. There is a lot of overlap in definitions, terminology and abstractions so we are going to keep things simple here and just define the two most basic types of test, Unit tests and Integration tests.

Unit Tests

First off, Unit Tests. Unit tests are the basic building blocks of tests. They are written to test a single unit of work. Usually on the function/method or class level. They are usually pretty self contained so they don't interact with other parts of the system. We might stub out other models and any services we might use.

So, to give an example, say we are writing an Ecommerce site and we need to write an Order class. Our Order class might have a method

def add_to_order(sku)

which adds a sku to an Order.  A Unit test would test this Order class and specifically test the add_to_order() method to make sure that when it was called with a given Sku the sku was added to the order. So that is a basic example.

So what is an Integration test?

An Integration tests makes sure that all our individual pieces work together, that they integrate if you will. So let's work with a simple example from our Ecommerce store.

Say we have a an inventory management system that processes orders. We move the order through its lifecycle from created through to shipped. We might write an InventoryService class to manage these state changes. We would write unit tests for our Inventory Service to verify individual pieces of functionality, but we would write an Integration test to make sure that our service places nice with actual Orders.

So our Integration test would create actual Orders in our test db (instead of stubbing or mocking them) and test the complete order management lifecycle.

This would verify that changes to the functionality of our component pieces (tested by the Unit tests) don't end up breaking the system as a whole when they are combined.

Unit tests test the functionality of individual components while Integration tests test how those units fit together. Integration tests test the explicit and implicit interfaces between components in your application.