How to approach unit testing in a large project

We have a project that is starting to get big, and we need to start applying Unit Tests when we start refactoring. What is the best way to apply unit tests to an existing project? I'm (somewhat) used to doing this from scratch, where I write tests along with the first lines of code forward. I'm not sure how to start when the functionality is already installed. Should I start writing a test for each method from the repository? Or should I start with the controllers down?

update: to clarify the size of the project. I’m not quite sure how to describe it, except to say there are 8 controllers and about 167 files with the extension .cs, all this was done in 7 months of development.

+4
source share
3 answers

As you seem to know, modifying testing into an existing project is not easy. Your test writing method is the best way. Your problem is both the process and the technology- tests that everyone needs, or no one will use them.

The recommendation that I have heard and agree with is that you should not try to wrap tests around your existing code base right away. You will never end. Start by testing your process- fix so that each bug fix passes the test. This will begin to test your existing code over time. Of course, there should always be tests in the new code. In the end, you get coverage at a reasonable percentage, but it will take time.

One good book that I recommended to me is Effective Work With Deprecated Code by Michael S. Feathers. The title does not actually demonstrate this, but testing in the existing code base is the main theme of the book.

+6
source

There are many approaches to installing tests around the existing code base. Unit tests are not necessarily the most productive way to run. If you have written a large amount of code, you can think about functional and integration tests before you work to the level of unit tests. Those tests of a higher level will help you gain widespread confidence that your product continues to work, while you make changes to improve the structure and modernize unit tests.

One of the methods that non-standard organizations use that I recommend in your situation is this: ask someone other than the author of the source code to write unit tests for this section. This gives you some level of cross-training and sanity testing, and also helps to ensure that you do not keep assumptions that will hurt your code as a whole.

Other than that, I will remember the recommendation for Michael Persian's book.

+4
source

For an inherited project with a decent codebase, unit testing may not be justified due to budgetary constraints, etc. Based on my reading on this issue, I would suggest:

  1. Each error that occurs in a QA, Release, or Production environment is a candidate for writing unit test cases along with a bug fix.

  2. Use the version control system to find out which sections / files of your code base change more often than others. Bring these sections / files under the cover of the unit test.

  3. When developing a new story, a meaningful test block should be written against them.

  4. Continue to monitor the scope of unit tests to observe any downtrend in a specific area of ​​the code base. In this area it is necessary to scale up and check whether the coverage of unit tests loses its effectiveness or not.

PS: I added Michael Feathers' book to my reading list, thanks for the suggestion.

0
source

All Articles