Introduction to Software Testing on Visual Studio 2019
Tags: dotnet, visualstudio, testing, csharp
Published: 2019-12-17 15:10:32

# 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](https://www.nuget.org/packages/MSTest.TestFramework/). 

2. NUnit Test Project (.NET Core)- A **Unit Test** project based on [NUnit](https://github.com/nunit/nunit). NUnit is a part of the [.NET Foundation](https://www.dotnetfoundation.org).

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](https://xunit.net) that target frameworks .net core. It is also part of the [.NET Foundation](https://www.dotnetfoundation.org).

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.

```csharp
[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());
}
```

#### 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".

```csharp
[DataTestMethod]
[DataRow("p1")]
[DataRow("p2")]
[DataRow("p3")]
public void RandomDataTest(string p)
{
    Assert.AreEqual(p, "p3");
}
```

## 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](https://res.cloudinary.com/dvb8jkgm9/image/upload/v1574941216/TestExplorer_vir1jb.png)

## 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](https://res.cloudinary.com/dvb8jkgm9/image/upload/v1574942702/Playlist_fhmijh.png)


## 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]
(https://res.cloudinary.com/dvb8jkgm9/image/upload/v1575420814/CodeCoverage_mtackj.png)

## 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]
(https://res.cloudinary.com/dvb8jkgm9/image/upload/v1575635121/Live_Unit_Testing_etlvx8.png)

## 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.