Live Unit Testing
Live Unit Testing automatically runs the impacted unit tests in the background as you edit code, and visualizes the results and code coverage, live in the editor.
This feature is available in the Enterprise edition of Visual Studio 2017 and for C# and VB projects only. The Live Unit Test framework runs in three test frameworks, as given below with supporting version.
- xUnit version > 2.0
- NUnit version > 3.5.0 and NUnit3TestAdapter > 3.5.1
- MSTest : TestFramework Version > 1.0.5-preview and MSTest.TestAdapter version > 1.1.4-preview
In this article, I am using MSTest Framework to implement the LUT.
Prerequisites
- Install Live Unit Testing Component. For this, run the VS Setup -> Individual components -> Development activities -> Live Unit Testing -> Install it.
- Create a sample console application ( Ex : UnitTestSample).
- Install the MS Test framework from NuGet Package into your sample project (that is created in step 2).
Go to Tools -> NuGet Package Manager -> Package Manager -> Console
Execute the below commands one by one.
a .Install-Package MSTest.TestFramework -Pre
b. Install-Package MSTest.TestAdapter -Pre
- Write the below sample code to test into Live Unit Testing feature.
- public static class MyMath {
- public static int Add(int value1, int value2) {
- return value1 + value2;
- }
-
- public static int Sub(int value1, int value2) {
- return value1 - value2;
- }
- }
- First, we have to create the Unit Test project in order to execute the LUT feature.
Right click "MyMath" class & create a Unit Test Project.
- In Unit Test function, write the below sample code. If the number is equal to "5", the Unit Test will pass; otherwise it will fail.
- public class MyMathTests {
- [TestMethod()]
- public void AddTest() {
- var result = MyMath.Add(2, 5);
- Assert.IsTrue(5 == result);
- }
-
- [TestMethod()]
- public void SubTest() {
- Assert.Fail();
- }
- }
- Start Live unit Testing.
Goto Test Menu -> Live Unit Testing -> Start .
Live Unit Test starts running in the background.
- Once LUT starts running, the status will be updated into the Visual Studio editor, a few seconds. (In background, the Live Unit Testing runs the code and updates the status).
- Edit the source code. Automatically, Live Unit Testing executes the code and updates the status in the VS Editor.
Hope you understood how to implement the Live Unit Test feature.