Adding unit tests to legacy code

With the release of MS one version of D365FO automatic testing is becoming more important.

These are some methods and tips on how to add a test harness to your legacy code.

1. Identify change points
Start by looking at the class you want to add a test harness to, consider what needs to change in order to implement unit tests on it.

2. Find test points
Test the public methods, not private methods. This way it's easy to make changes to the internal workings of the class in the future without affected what other classes depend on.

3. Break dependencies
Add interfaces to the classes that the class you are work on depends on. Modify the class you are working on so that it depends on the interfaces that the dependencies implement instead of the classes and add the possibility to inject dependencies. For example by allowing injection of dependencies on the class creation.

4. Write tests
Implement tests on the class, make sure they pass.

5. Make changes and refactor
Make changes and ensure that the tests pass.

Always consider the SOLID principles when you refactor the code.

Reference, Working Effectively with Legacy Code by Michael C. Feathers

Comments

Popular posts from this blog

How to disable auto enabled flight key using KillSwitch

Technical intro to Feature management in D365FO

Continuous integration and deployment Power platform FinOps tweaking