다음을 통해 공유


Adding Unit Testing to an Existing Application

Incorporating unit testing into an application that was developed without much thought as to how to make it unit testable is a common experience for developers.  This article explores some simple approaches to adding unit testing to an application without resorting to a complete code re-write. 

Begin to introduce Interfaces to separate different parts of the solution, and to make each component separately testable.  For example, you might have a component that reads a file from the file system (e.g. ExistingAppDataReader) and creates a new application object.

You can define an interface to cover the aspects of the System.IO.File class that your class uses and provide a way of switching between either an implementation that is a wrapper of the underlying System.IO.File class and one that is amendable to Stubbing or Mocking.

Another approach that is suitable for implementing complex logic is to create intermediary "engine" classes that take a series of inputs (representing the state of the system at present) and have processing methods that return a list of actions (categorised into Create, Remains, Update and Delete).  That way the complex aspect of the process (doing business logic, determining what changes and additions to the application data need to be made) can be covered in unit tests, but the more mundane code for persisting those application data changes can be passed to (presumably) pre-existing and reliable code.