Live Unit Testing in Visual Studio 2017 Enterprise

Live unit testing is only available in the Enterprise editions of Visual Studio 2017 Version 15.3.0 or later

I used to hate writing tests for my projects. But not so long ago, I was forced to change my perspective on that once I start managing big enterprise level applications. As a newbie in the world of TDD (Test Driven Development) what I expect from any IDE is,

Quickly show which parts of my application code is covered by unit testing and which are not.

Yesterday, I saw the .NET Core 2.0 Released! video and noticed that Microsoft has shipped Visual Studio 2017 Enterprise (Version 15.3.0) with a newly added feature called Live Unit Testing. As the name suggests, using this feature, you can see if a specific unit of your code is covered by any unit test or not; directly in your code editor.

As shown above, with live unit testing enabled; I can now see that only GetExpertGeeks method of my repository is covered by my unit test. So, how do you enable this cool feature? Simply click on Test from the top menu bar and from the context menu go to Live Unit Testing > Start to enable live unit testing in your codebase.

You have to have a unit test project setup first; otherwise starting live unit testing may feel like nothing is happening.

All of the code snippets shown in the post is coming from a project; available in the following git repository:

HTTPS://GITHUB.COM/FIYAZBINHASAN/FAKINGISNOTMOCKING

You can also see which test method(s) is/are covering a code block from a popup dialog; which can be accessed by clicking on a green tick. Similarly, the blue bar represents that some code blocks are not yet covered by any unit test.

So, let's write a unit test for the Get method of Repository.cs and see live unit testing in action. To accommodate the new test method, I've modified my GeekRepositoryTest.cs a little bit and it looks like the following:

public class GeekRepositoryTest : IClassFixture<DatabaseFixture>
{
    Repository repository;
    public GeekRepositoryTest(DatabaseFixture fixture)
    {
        repository = new Repository(fixture.ApplicationDbContext);
    }

    [Fact]
    public void GetExpertGeeks()
    {
        var expertGeeks = repository.GetExpertGeeks();
        Assert.Equal(2, expertGeeks.Count());
    }

    [Fact]
    public void Get()
    {
        var allGeeks = repository.Get();
        Assert.Equal(4, allGeeks.Count());
    }
}

public class DatabaseFixture
{
    public DatabaseFixture()
    {
        var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
        builder.UseInMemoryDatabase();
        var options = builder.Options;

        ApplicationDbContext = new ApplicationDbContext(options);

        var geeks = new List<Geek>
            {
                new Geek { Id= 1, Name = "Mr. Anderson", Expertise = "Machine Learning", Rating = 4},
                new Geek { Id= 2, Name = "Fiyaz Hasan", Expertise = "ASP.NET Core", Rating = 3.52M },
                new Geek { Id= 3, Name = "Jon Doe", Expertise = "Python", Rating = 3 },
                new Geek { Id= 4, Name = "Jane DOe", Expertise = "Data Science", Rating = 2.52M }
            };

        ApplicationDbContext.AddRange(geeks);
        ApplicationDbContext.SaveChanges();
    }
    public ApplicationDbContext ApplicationDbContext { get; private set; }
}

N.B: For unit testing, I'm using xUnit in my solution.

Once you add a new unit test, live unit test will intelligently analyze your codebase and decorate code blocks that falls under the unit test with a passing or failure flag (green tick for passing tests and red cross for failing tests).

Again, you can navigate to the failing test (if any) directly from the popup dialog.

Live unit testing feature only works with project that uses one of the following test frameworks:

  • xUnit.Net
  • NUnit
  • MSTest

Live unit testing falls under the static analysis tool category since it is giving you a sense of passing/failing tests without building or explicitly running the tests first.

Static analysis is a way of analysis your codebase without executing it.

Live unit testing is integrated with the test explorer to give you a WYSIWYG (what you see is what you get) feeling.

And that's it. To know more about Live Unit Testing, you can read this MSDN blog. Let me know your experiences with this new feature.