DEV Community

Caio Cesar
Caio Cesar

Posted on • Updated on

Introduction to Software Testing on Visual Studio 2019

Introduction

This introduction post will show and briefly describe the available options for software testing using C# in Visual Studio (VS) 2019 Enterprise 16.3.9.

Project Types

As of this post there are 6 distinct test projects within Visual Studio 2019:

  1. MS Test Project (.NET Core)- A Unit Test project based on Microsoft Test Framework Library.

  2. NUnit Test Project (.NET Core)- A Unit Test project based on NUnit. NUnit is a part of the .NET Foundation.

  3. Unit Test Project (.NET Framework)- A Unit Test project that targets .NET Framework, makes use of Microsoft Test Platform.

  4. xUnit Test Project (.NET Core)- A Unit Test open source project based on xUnit that target frameworks .net core. It is also part of the .NET Foundation.

  5. Web Driver Test for Edge (.NET Core)- A Unit Test project based on .NET Core and Selenium Web Driver, for testing the navigation of a website.

  6. Web Driver Test for Edge (.NET Framework)- A Unit Test project based on .NET Framework and Selenium Web Driver, for testing the navigation of a website.

Unit Test Examples on MS Test Project

A Unit Test is when a segment is tested on a program, within a class unit tests usually tests a single method.

Assertion Type

Within the visual studio test projects, the use of assertions is the most common form of validating an expected value of a unit test. The following code shows an example of some of the assertion methods within the testing tool framework.

[TestMethod]
public void AssertionExamples()
{
    Assert.AreEqual("expected", "actual");
    Assert.AreNotEqual("a", "a");
    Assert.IsTrue(false);
    Assert.IsFalse(true);
    Assert.IsNull("");
    Assert.IsNotNull(null);
    Assert.IsInstanceOfType("object", Type.GetType("type"));
    Assert.IsNotInstanceOfType("object", Type.GetType("type"));
    Assert.ThrowsException<NotImplementedException>(() => new object());
}
Enter fullscreen mode Exit fullscreen mode

Data Test Method

It is also possible to implement data driven test in the following code we have a test, one test that will fail for the "p1" and "p2" data but pass for the "p3".

[DataTestMethod]
[DataRow("p1")]
[DataRow("p2")]
[DataRow("p3")]
public void RandomDataTest(string p)
{
    Assert.AreEqual(p, "p3");
}
Enter fullscreen mode Exit fullscreen mode

Visual Studio Test Explorer

The test explorer within Visual Studio assists the user on running, grouping, filtering, create, save, debug etc. The path to this feature is in

Test -> Test Explorer, as shown in the figure below or using the (crtl + e, t) shortcut.

VS2019 - Test Explorer

Tests Playlist

It's also possible to create playlists of selected tests for a subset of unit test that can be arranged in groups of categories. To create a playlist, you can select multiple tests in the test explorer, right click the selected test and click on add to playlist.

A playlist can be saved to a external file with the extension (*.playlist), after saved you can remove and add new methods to the playlist, to run a saved playlist open test explorer and click on the open playlist icon as shown on the figure below.

VS2019 - PLaylist

Code Coverage

Within Visual Studio it's possible to verify what fractions of the project code is indeed tested with unit tests. This feature is called code coverage and its available through the Test Explorer. Note that code coverage is only available on Visual Studio Enterprise edition.

To get started with code coverage first open the Test Explorer, within test explorer select 1 or more tests then right click the tests and select analyze code coverage, the code coverage results should pop-up with information on which projects has been tested and the covered blocks percentage of the given projects.

VS2019 - Code Coverage

Live Unit Testing

Live Unit Testing is also only available in Visual Studio Enterprise Edition, this feature provides information about code coverage in real-time. If the developer updates a code block the live unit testing feature can detect these changes and display test information, in real time.

As shown in the figure bellow live unit testing provides information, next to each method we can view icons describing if the method has failed or passed.

VS2019 - Live Unit Testing

Overview

MSTest, NUnit and xUnit are all efficient for creating unit tests in the Visual Studio environment. You can create Test Methods, work with data driven tests and use Test explorer with all these frameworks. Within Visual Studio tools it is possible to create Playlist analyze Code Coverage and perform Live Unit Testing.

Top comments (0)