In ASP.NET core 2.1, setting up testing project got much easier with the release of nuget package. In this post we are going to setup a functional test project. functional Microsoft.AspNetCore.Mvc.Testing Prerequisite To follow this tutorial you should have- and .NET Core 2.1 RC1 SDK or v15.7 Preview 1 or newer, installed on your VS Code Microsoft Visual Studio 2017 system Create test project Create a folder and name it , because why not 😜. Open window inside the folder (Shift + anywhere inside the folder and select ) and create a solution: HelloWorld PowerShell right click Open PowerShell window here dotnet new sln Now create a basic project inside directory and project inside directory: MVC src xunit tests dotnet new mvc -o .\src\HelloWorld.Mvc dotnet new xunit -o .\tests\HelloWorld.FunctionalTests Add those two project to the : solution dotnet sln add .\src\HelloWorld.Mvc\HelloWorld.Mvc.csproj dotnet sln add .\tests\HelloWorld.FunctionalTests\HelloWorld.FunctionalTests.csproj Reference the project form the project: MVC FunctionalTests dotnet add .\tests\HelloWorld.FunctionalTests\HelloWorld.FunctionalTests.csproj reference .\src\HelloWorld.Mvc\HelloWorld.Mvc.csproj Write functional Test Add to the functional test project: Microsoft.AspNetCore.Mvc.Testing dotnet add .\tests\HelloWorld.FunctionalTests\HelloWorld.FunctionalTests.csproj package Microsoft.AspNetCore.Mvc.Testing -v 2.1.0-rc1-final Now open the project in or and create a new class inside project and name it VS Code Visual Studio 2017 15.7 Preview 1 or newer HelloWorld.FunctionalTests HomePageShould.cs using HelloWorld.Mvc;using Microsoft.AspNetCore.Mvc.Testing;using System.Net;using System.Net.Http;using System.Threading.Tasks;using Xunit; namespace HelloWorld.FunctionalTests{ public class HomePageShould : IClassFixture<WebApplicationFactory<Startup>> { private readonly HttpClient _client; public HomePageShould(WebApplicationFactory<Startup> factory) { _client = factory.CreateClient(); } [Fact] public async Task ReturnHttpStatusCodeOk() { var response = await _client.GetAsync("/"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } }} Run the test Now run the . It should fail with the error message test Message: System.IO.FileNotFoundException : Could not load file or assembly ‘Microsoft.AspNetCore, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’. The system cannot find the file specified. To resolve it, add nuget package to the project Microsoft.AspNetCore.App test dotnet add .\tests\HelloWorld.FunctionalTests\HelloWorld.FunctionalTests.csproj package Microsoft.AspNetCore.App -v 2.1.0-rc1-final Now run the test from > > (Visual Studio 2017) or from - Test Run All Tests PowerShell dotnet test .\tests\HelloWorld.FunctionalTests\HelloWorld.FunctionalTests.csproj You should see the green tick of happiness.
Share Your Thoughts