Unit Testing is a feature in software development where each unit/component/module is tested as soon as it is written. Unit Testing is an automated testing feature and has support in Visual Studio for MS Test framework as well as other third party frameworks like NUnit.
In previous articles, I have discussed in details about the unit testing feature in Visual Studio, IntelliTest with Visual Studio and have taken overview of other automated tests like Web Test, Ordered Test, Load Tests.
In Visual Studio 2017(Enterprise Edition), Microsoft has introduced yet another very interesting feature called Live Unit Testing. This feature improves upon traditional unit testing to provide automation testing to achieve rapid development.
Editorial Note: As of this writing, Live Unit Testing currently does not work with .NET Core or .NET Standard projects. It only works with projects that are built upon the .NET Framework.
Live Unit Testing - Overview
Live Unit Testing helps in creating better quality code and also helps in finding code coverage. It encourages Test Driven Development (TDD).
Live Unit Testing gives the developer real time information directly while editing or writing code. This feedback is given immediately while a developer is fixing a bug. It gives impact of changes encountered on the existing tests.
Live Unit Tests also lets a developer know right away if he/she is breaking part of code or not. It runs the impacted unit tests in the background the moment you start editing code. The results for test execution and code coverage are shown in real time.
This feature is available in Visual Studio 2017 Enterprise Edition and for the projects using C# or VB.NET which are targeting .NET framework. It can be used with 3-unit testing frameworks namely MSTest, xUnit and NUnit.
Live Unit Testing Walkthrough
Let us see a walkthrough of creating Live Unit Testing.
Step 1. Create a new test project using Visual Studio 2017 and provide a name to it. As usual, you get a class with TestClass as the attribute and the method with TestMethod as attribute in it.
Step 2. In order to enable live unit testing select Test > Live Unit Testing > Start. The reference to NuGet package for MS Testing framework gets added automatically.
Step 3. The moment you start writing code, the feature kicks in and you start getting real time results. I have written two test methods directly as follows. (As expected in TDD, these test methods do not have any corresponding methods to test).
The first test method fails (red color cross) as the two strings are not matching whereas in the second test method, the strings are matching so we see a green colored tick mark.
Step 4. Let us start writing code for this test method in the class library and find out how the Live Unit Testing helps us.
Add a project to the same solution of type class library and give it a name.
Step 5. I will change the test method so as to use the class library. Let us write the stub for the method in the class library.
The above method shows the stub written in class library.
The test method calls the proper code but as it does not have implementation, the test still fails.
Step 6. Let us add the implementation for the method and see how the test automatically passes.
The above figure shows the method implementation as well as the test method and ensures that the test has passed.
Now that we have seen the approach for TDD, let us first add code to the class library and then create the test methods.
Step 7. Let us add some more code in the class library. I have written two methods as follows:
Observe a blue bar appearing on the left and when you hover mouse over it, you will see that this code is not yet covered by any tests.
Step 8. In order to add Test Methods for the complete class, right click on it and select Create Unit Tests.
I have added the methods to the existing test project
Step 9. The tests methods are added and as we have added code for Assert Failure, both the test methods will fail as follows:
Step 10. Let us write code for the test method and find out how the tests passes.
Providing Live Unit Testing support is very straightforward, as we have already seen. In case we need to stop or pause this support, we can select Test > Live Unit Testing and we have the options to pause, stop or restart the feature.
Exclude when not required
What if there are some methods for which we don’t want the Live Testing feature enabled?
We have Include and Exclude options. Right click on the method for which you do not need to include the feature, select Live Tests and select Exclude.
You can include later if required.
Integrating Live Unit Testing with Test Explorer
Live Unit Testing feature is automatically integrated with Test Explorer (our original place to run test methods).
In order to find out how effective the code is, we can use Code Coverage feature which is available as a part of Visual Studio.
Select Test > Code Coverage > All Tests and the code coverage will be shown in Code Coverage Window.
The result looks as follows:
The blue colored lines indicate the parts of the code were executed during running the test, while red indicates it has not.
Sometimes when you add a class library to the existing solution and you start writing code in it, the IDE may give an error as there is problem with NuGet package.
In case this happens, just restore the package.
There are some configurations you can do like pausing Live Unit Testing if the battery is low or during build and debug. These options can be set from Tools > Options > Live Unit Testing tab. Here’s how it looks:
Live Unit Testing and NUnit Testing
Live Unit Testing also supports NUnit Testing framework. You can refer NUnit Testing articles for details. I will add NUnit test for the already existing concatenation method.
Right click on the Unit Test project and select manage NuGet Package and type nunit in the search box. Add packages for NUnit and NunitDataAdapter. Find the references added to packages.config
Add another unit test and add the code for testing concatenation method.
Observe we need to use Nunit.Framework. Observe the method fails as I have not added a space in the Concat method.
Let us fix the bug and we see the test method passed immediately while editing the code itself.
How very handy!
In this article, I discussed how Live Unit Testing works for MSTest and NUnit framework. We can run unit tests while writing code and get immediate feedback. The feature immediately provides information whether the test passed or failed or if any method does not have the corresponding test method.