November 14, 2018 08:41 by
Peter
Visual Studio 2017 v15.3 or higher versions support Live Unit Testing. This feature executes the already-written test cases automatically as a programmer makes the code changes. Live Unit Testing helps a developer to do the following:
- It executes all the impacted Test Cases as a programmer modifies the code and makes sure changes do not break Unit Test Cases.
- Through the Visual Studio editor, the programmer sees the code coverage in a realistic view.
- It also indicates which line is covered or not using Live Unit Testing.
- Live Unit Testing is supported in both .NET Framework and .NET Core.
Enabling
Live Unit Testing is only available in Visual Studio 2017 with the Enterprise edition. We can enable the Live Unit Testing by going to the following menus - Test > Live Unit Testing > Start.
After enabling the Live Unit Testing for a project, Live Unit Testing shows options as Start, Pause, Stop and Reset.
Configuration
The default configuration can be changed from the Visual Studio Menu Tool > Options > Live Unit Testing > General. We can configure the Live Unit Testing for the Battery and Memory usages, Testcase Timeout, and Logging Level can be set as needed.
Setup Project Solution
- Open the Visual Studio 2017.
- Create one Windows Library Project as “ClassLibrary1” and Change the “Class1.cs” to the “AreaCalculator.cs”.
- Right click on the solution and add a Unit Test Project as “UnitTestProject1” and change the “UnitTest1.cs” to the “AreaCalculatorTest.cs”
- Add the Reference of the “ClassLibrary1” project to the “UnitTestProject1”.
- Add the using statement “using ClassLibrary1;” to the “AreaCalculatorTest.cs” in the using statement section.
- Build the Solution.
Demo
Open the AreaCalculator.cs and add the code as per the below screenshot.
Open the AreaCalculatorTest.cs and paste the below code. I have not added the Test Method for the triangle’s area calculation and incorrectly calculated the Test Method of the square’s area, just to show some behavior of Live Unit Testing.
Replace the below code in the AreaCalculatorTest.cs file
Now start the Live Unit Testing from the Test menu, then an automatic background process triggers and we see an icon in-line with code. Description of each icon is mentioned here
- Green Tick Mark represents that this Method is covered for the unit testing.
- Cross Mark represents that this method is covered for the unit testing, but the test case failed during execution.
- Blue Negative Mark represent that this method is not covered for the unit testing so a test case needs to be created.
Below screenshots represent each icon of the Live Unit Testing
Icon’s in AreaCalculator.cs
Icon’s in AreaCalculatorTest.cs
So, with the help of Live Unit Testing of Visual Studio, a developer will know about the impact of the changes he is making, and if it's covered for unit testing or not. This is an awesome feature of Visual Studio 2017