Working with Live Unit Testing in Visual Studio 2017

Microsoft has just released another edition of its flagship development platform—Visual Studio 2017. Unit testing is the process of testing units of your code to ensure that they comply with the acceptance criteria. It is a process that helps you to reduce or eliminate bugs earlier in the software development life cycle. Live Unit Testing is a new feature that has been introduced in Visual Studio 2017. This article presents a discussion on what Live Unit Testing is all about and how to work with it in Visual Studio 2017.

What is Visual Studio 2017 Live Unit Testing?

Okay; what is Live Unit Testing—what's there to care about? Support for Unit Testing was there in Visual Studio for quite some time. Okay; relax! Your queries would soon be answered. Let's now understand what Visual Studio Live Unit Testing is all about.

Till now, you have had to run your unit tests manually or automatically after each build. With this new feature around, you no longer need to wait till the build gets generated. You need not even save your source code file—as soon as the code changes, the associated unit tests would start running in the Visual Studio 2017 IDE.

Configuring Live Unit Testing

Live Unit Testing is integrated with the Test Explorer and can be configured using Tools->Options->Settings in the Visual Studio 2017 IDE. You can start, stop, or pause Live Unit Testing easily while you are working in the IDE. You also can view the code coverage details and navigate easily to the test that has failed.

Here's what Microsoft states 1: "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, in real-time. In addition to giving feedback on the impact that your changes had on the existing tests, you also get immediate feedback on whether the new code you added is already covered by one or more existing tests."

Note that this feature is available as part of the Enterprise Edition of Visual Studio 2017, and can be used for your C# or VB.NET projects. Also, note that Visual Studio 2017 Live Unit Testing is supported in the NUnit, xUnit, and MSTest frameworks. Support for Live Unit Testing in .NET Core is not yet available but is there in the roadmap.

Working with Visual Studio 2017 Live Unit Testing

To work with Live Unit Testing in Visual Studio 2017, follow these steps.

  1. Open Visual Studio 2017 IDE.
  2. Click File -> New -> Project.
  3. Create a new Unit testing project.
  4. Name and save the project.

This would create a new unit testing project in the name you specified. Next, double-click the "UnitTest1.cs" file in the Solution Explorer window. Now, replace the TestMethod1 unit test method with the following code.

[TestMethod]
   public void TestMethod1()
   {
      Assert.AreEqual("Hello", "World");
   }

Now, click Test -> Live Unit Testing ->. Start in the menu bar to start Live Unit Testing. And, that's all you would need to do! This would start Live Unit Testing and you would observe that this test fails because the strings don't match. You can change your code—Live Unit Testing continues to run in the background and displays the results in the Output Window. You also can pause, stop, or restart this feature as and when needed. Figure 1 illustrates Live Unit Testing in action—our unit test fails because the strings don't match.

Live Unit Testing in action!
Figure 1: Live Unit Testing in action!

Let's now write another unit test method—one that would pass. Refer to the test method code that follows.

[TestMethod]
   public void TestMethod2()
   {
      Assert.AreEqual("Hello World", "Hello World");
   }

As soon as you restart Live Unit Testing (assuming that it was stopped while you were writing the unit test method), the tests are both executed—one of them fails and the other succeeds. Here's how the output looks when both the unit test methods have been executed.

Live Unit Testing in action—one of the tests pass and the other fails
Figure 2: Live Unit Testing in action—one of the tests pass and the other fails

Summary

Live Unit Testing boosts developer productivity by enabling you to know the outcome of your code changes right at the time you are writing your code in Visual Studio 2017 IDE. This article presented a glimpse of one of the most striking new features in Visual Studio 2017, Live Unit Testing, why is it helpful, and how we can work with it.

You can learn more on Live Unit Testing in Visual Studio 2017 from this article.

Reference

1https://blogs.msdn.microsoft.com/visualstudio/2016/11/18/live-unit-testing-visual-studio-2017-rc/



About the Author

Joydip Kanjilal

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.Net, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He has more than 18 years of industry experience in IT, with more than 14 years in Microsoft .Net and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of eight books and more than 300 articles. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb’s Conference 2014 in Bangalore. He is also a regular speaker at SSWUG Virtual Conference. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal.

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date