Unit Testing - Separation of Interface From Implementation

Separation of Interface From Implementation

Because some classes may have references to other classes, testing a class can frequently spill over into testing another class. A common example of this is classes that depend on a database: in order to test the class, the tester often writes code that interacts with the database. This is a mistake, because a unit test should usually not go outside of its own class boundary, and especially should not cross such process/network boundaries because this can introduce unacceptable performance problems to the unit test-suite. Crossing such unit boundaries turns unit tests into integration tests, and when test cases fail, makes it less clear which component is causing the failure. See also Fakes, mocks and integration tests

Instead, the software developer should create an abstract interface around the database queries, and then implement that interface with their own mock object. By abstracting this necessary attachment from the code (temporarily reducing the net effective coupling), the independent unit can be more thoroughly tested than may have been previously achieved. This results in a higher quality unit that is also more maintainable.

Read more about this topic:  Unit Testing

Famous quotes containing the word separation:

    Last evening attended Croghan Lodge International Order of Odd Fellows. Election of officers. Chosen Noble Grand. These social organizations have a number of good results. All who attend are educated in self-government. This in a marked way. They bind society together. The well-to-do and the poor should be brought together as much as possible. The separation into classes—castes—is our danger. It is the danger of all civilizations.
    Rutherford Birchard Hayes (1822–1893)