The best coding strategy for unit testing

I have a solution in which a lot of code is missing. I need to reorganize this code to split up in order to start creating unit tests. What is the best strategy? At first I think that I should push the business logic away from accessing data from business objects in order to get some organization first and then turn around from there. Since many classes do not support one responsible principle, it is difficult to start testing them.

Are there any other recommendations or recommendations for using an outdated solution and getting its form for code preparation and unit testing?

+4
source share
7 answers
+10
source

One of the most important things to do and the best ways to approach legacy code are flaws. This is a process that you will continue to do with any code base into which you also introduce unit testing. Whenever a defect is reported, write a unit test that will display the defect. You will quickly find this code that breaks on a regular basis (i.e. "Oh, yay. The plugh () method in the xyzzy class is broken again!) Will start to break less and less.

In fact, just start doing it. You will not have huge coverage in an outdated application in one night. Start by hitting a code that is more prone to breakage and start branching. Make sure that any new development in the code has a higher code coverage.

Remember that the TDD mantra is red / green / refactoring, and you might want to look at refactoring tools to help you complete some of the tedious tasks that go with it. JetBrain ReSharper is popular and my personal choice.

+4
source

Start by creating tests. Update the code needed to support the tests.

+1
source

I suggest that you first create tests for existing code until you have good enough coverage. You need to check your code to make sure you don't break anything when you reorganize. Of course, you will want to do this in parts by writing tests for the module, and then reworking it before moving on to the next one. Once you have good reach, you can decide whether to continue refactoring to make the code more verified. From your description, I suspect it will be.

+1
source

These are general guidelines that I find useful for unit testing:

1) Define boundary objects (Win / WebForms, CustomControls, etc.).

2) Definition of management objects (business-level objects)

3) Write Unit tests only for public methods of control objects called by boundary objects. Thus, you will be sure that you are using the basic functional aspects of your application.

In your case, if business rules are closely related to boundary objects, you have problems - in my opinion, you should try to reorganize your things, focusing on hot spots, based on the functional requirements of your application.

The likelihood of this is obvisally highly dependent on the particular case.

0
source
0
source

You need to check your code to make sure you don't break anything when you reorganize.

0
source

All Articles