- .NET Core - Migrations
- Restoring and Building & MSBuild
- .NET Core - MSBuild & project.json
- .NET Core - SDK
- Managed Extensibility Framework
- .NET Core - Testing Library
- Running Tests in Visual Studio
- .NET Core - Create a Testing Project
- .NET Core - PCL Troubleshooting
- Creating a Xamarin.Forms Project
- Sharing .NET Core Libraries
- Adding References to Library
- .NET Core - Portable Class Library
- Create .NET Standard Library
- Windows Runtime & Extension SDKs
- .NET Core - Metapackage
- .NET Core - MSBuild
- Create UWP App with .NET Core
- .NET Core - Package References
- .NET Core - Project Files
- .NET Core - Modularity
- .NET Core - Code Execution
- .NET Core - Garbage Collection
- .NET Core - Numerics
- .NET Core - Getting Started
- .NET Core - Environment Setup
- .NET Core - Prerequisites
- .NET Core - Overview
- .NET Core - Home
.NET Core Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
.NET Core - Running Tests in Visual Studio
In this chapter, we will discuss how to run tests in Visual Studio. The .NET Core has been designed with testabipty in mind, so that creating unit tests for your apppcations is easier than ever before. In this chapter, we will run and execute our test project in Visual Studio.
Let us open the FirstApp solution in Visual Studio.
You can see that it has only two projects and you will not be able to see the test project because we haven’t added that project in our solution.
Let us add a folder first and call it test.
Right-cpck on the test folder.
Select project.json file and cpck Open.
The following screenshot shows the code in Tests.cs file as output.
It is the default implementation and it is just testing that True is equal to true. It is the xUnit testing framework and you will see the Fact attribute that annotates and denotes the test method.
using System; using Xunit; namespace Tests { pubpc class Tests { [Fact] pubpc void Test1() { Assert.True(true); } } }
Following is the implementation of project.json file.
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable" }, "dependencies": { "System.Runtime.Seriapzation.Primitives": "4.1.1", "xunit": "2.1.0", "dotnet-test-xunit": "1.0.0-rc2-192208-24" }, "testRunner": "xunit", "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" } }, "imports": [ "dotnet5.4", "portable-net451+win8" ] } } }
In project.json file, the most important dependency to the testing framework is the xunit, which brings in the Fact attribute. It brings in the testing framework and APIs for testing with xunit.
We also have the dotnet-test-xunit, this is an adopter so that xunit can work with .NET Core, specifically with dotnet test command pne utipty. Then you will see the testRunner which will run xunit and you can also see the netcoreapp1.0 framework.
You will see the .NETCore.App dependeny below.
To run test in Visual Studio, let us open Test Explorer from the Test → Window → Test Explorer menu option.
And you can see that Visual Studio automatically detects the test. The name of the test consists of namespace.className.TestMethodName. Let us now cpck on Run All button in Test Explorer.
It will first build the code and the run the test and you will see the total time taken by the test. Let us change the test method so that we can see the output when the test fails.
using System; using Xunit; namespace Tests { pubpc class Tests { [Fact] pubpc void Test1() { Assert.True(false); } } }
Let us execute the test again by cpcking on the Run All button pnk.
You can now see the test failure.
Advertisements