Is it worth it to add unit tests to legacy code?

Is it worth it to add unit tests to legacy code?

The book The Art of Unit Testing has some good advice on this. It also recommends the book Working Effectively with Legacy Code; I haven’t read the latter yet, but it’s on my stack. EDIT: But yes, even minimal code coverage was worthwhile. It gave me confidence and a safety net for refactoring the code.

How to run unit tests in C #?

You start with a C# project that is under development, create tests that exercise its code, run the tests, and examine the results. Then you change the project code and rerun the tests. The [TestClass] attribute is required on any class that contains unit test methods that you want to run in Test Explorer.

When do assertions fail in a unit test?

In such tests, written after the facts, assertions verify the current behavior of the code. Unlike unit tests, they do not prove that the code is correct, they are just pinning down (characterizing) the current behavior of the code. The tests will fail if you modify the external behavior of the code.

How are unit tests used in characterization tests?

A unit test framework such those of the xUnit family can be used to write characterization tests. In such tests, written after the facts, assertions verify the current behavior of the code. Unlike unit tests, they do not prove that the code is correct, they are just pinning down (characterizing) the current behavior of the code.

How to generate unit tests for your code?

Right-click on a method and choose Run IntelliTest to generate unit tests for the code in your method. IntelliTest runs your code many times with different inputs. Each run is represented in the table showing the input test data and the resulting output or exception.

What do you need to know about working with legacy code?

So you need a good attitude (including a good sense of humor) as well. Working with good colleagues also helps. But definitely read this book and use its lessons in practice. “Code without tests is bad code. It doesn’t matter how well-written it is; it doesn’t matter how pretty or object-oriented or well-encapsulated it is.