Live Unit Testing with .NET and Visual Studio

A few days ago I was looking for something related to unit tests in the Microsoft documentation. I came to the tab titled ‘Live unit testing’ and I decided to check what is it. It is quite interesting, because why I have to click to run unit tests when I made small changes in code? We have the live reload functionality in frontend or some mobile technologies to see changes in the interface, so the developer don’t have manually click F5 to refresh the page. The same situation is on backend side. When I’m working on changes in business logic I want to have continuous response that my changes don’t break other parts of service.

First drawback

Excited I began to delve into this part of the documentation, but my excitement did not last too long. It turned out that this functionality is available only in Visual Studio Enterprise… (offtop) To be honest for several weeks I use a different IDE - Rider.

Demo

After virtual machine creation I started to prepare some demo codes. I created FooProject console app in .NET Core 3.1 with class BarService and FooProject.UnitTests NUnit Test project. I defined in BarService method which will check if string pushed into method is a palindrome.

Solution explorer

Then I created a couple of tests (I added FluentAssertion package - thanks to this package assertions are more readable, I can recommend it!).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
using FluentAssertions;
using FooService;
using NUnit.Framework;

namespace FooProject.UnitTests
{
    public class BarServiceTests
    {
        [TestCase("Sore was I ere I saw Eros.", true)]
        [TestCase("Sore was I ere I saw Eros.   ", true)]
        [TestCase("   Sore was I ere I saw Eros.", true)]
        public void StringIsPalindrom_ReturnsAsDefinedInCases(string stringToTest, bool expectedResult)
        {
            BarService barService = new BarService();

            bool result = barService.StringIsPalindrom(stringToTest);
            result.Should().Be(expectedResult);
        }
    }
}

Live reload in action

Let’s start work. Live Unit Testing Window you can find in Test menu.

Test menu

When you click Play button the solution will be checked and VS will find tests. For now I don’t have any implementation of StringIsPalindrom method, so all of my tests failed.

All test failed

Below you can find screencast with filled StringIsPalindrom method. When I comment one line (and due to this two of three test failed) and I clicked Ctrl + S tests were reloaded. What is more on the right side of code you have marked which line of codes are covered by tests and information if tests passed or failed.

Live tests

Configuration and other restrictions

We saw that it so interesting feature, but you have to have an enterprise licence for VS. Another restricion is minimal version test libraries. In my case NUnit has to be at least 3.5.0.

There are a lot of possibilities of configuration. For example you can restrict number of processes which are generated by live reload or turn off live unit testing when your battery is low. What is more if you have many of tests in project and you don’t want to check all of them in live reload you can exclude selected tests or all of tests from project. More information in details you can find in docs.

Newsletter

Thank you for visiting my website. I hope you enjoyed content which I prepared and learned something valuable from it. If you want to be informed about my next entries or occasionally get a message with a collection of some interesting links, please subscribe to my newsletter. I will be extremely pleased if you do this and join my community!

  • By clicking button below you agree to send you news from my blog, about my products and services. Above data are stored in Mailchimp and I do not share them to anyone. More info you can find in privacy policy.

comments powered by Disqus