Unit tests allow to validate the behavior of your code. When you write code, you want to get feedback from your tests as soon as possible. It is where the new feature of Visual Studio 2017 takes place. The live unit testing feature runs test in background as soon as you save a file and display the result directly in the code editor. Of course you don't want to run thousands of tests in background every time you change a single line of code, but only the test impacted by your changes. Live unit testing knows which part of the code each test cover, so it can run only the needed tests. Thus, you get a feedback very quickly. Let's see how to use live unit testing.
You can enable live unit testing automatically when you open the solution in the options window:
Let's use a simple method to test:
public static string Nullify(string str, bool trim)
if (str == null)
str = str.Trim();
After enabling live unit testing, Visual Studio adds a blue dash in front of each line of code of the method. This means the statement is not tested.
Let's add a test and save the file. The info will change to green check for the covered statements. The blue dash stays for uncovered lines.
Now, let's add a test that fails. A red check is added in front of the lines of code. This indicator override other indicators. By clicking on a red cross, you can see the details of each test that covers the selected line.
Now, you can easily debug the failing test by clicking on the red cross:
And you can see the details by hovering a specific test:
Live unit testing is very nice! You can quickly test your code and see as soon as you save the file where which tests success or fail. You can also know which part of your code is covered by tests and which tests cover them. The only drawback is that this functionality is only available in the enterprise edition of Visual Studio 2017 😦