Skip to content

Latest commit

History

History
111 lines (83 loc) · 2.37 KB

File metadata and controls

111 lines (83 loc) · 2.37 KB

Getting Started

This guide helps you set up Markop Test in a new or existing solution.

Prerequisites

  • .NET SDK 8.0 or newer
  • An ASP.NET application project to test
  • xUnit test infrastructure

1) Create a Test Project

dotnet new classlib -n IntegrationTest
dotnet sln add IntegrationTest

You can also start with dotnet new xunit -n IntegrationTest if you prefer an xUnit template.

2) Reference Your Application Project

From the test project directory:

dotnet add reference ../src/WebAPI/WebAPI.csproj

Use the correct project path for your solution.

3) Install Markop Test

dotnet add package MarkopTest

4) Create an App Factory

Choose a factory based on the test type. Example for integration tests:

usingSystem;usingSystem.Net.Http;usingSystem.Threading.Tasks;usingMarkopTest.IntegrationTest;usingMicrosoft.Extensions.DependencyInjection;usingWebAPI;usingXunit.Abstractions;publicclassAppFactory:IntegrationTestFactory<Startup,dynamic>{publicAppFactory(ITestOutputHelperoutputHelper,HttpClientdefaultClient=null):base(outputHelper,defaultClient){}protectedoverridestringGetUrl(stringurl,stringcontrollerName,stringtestMethodName){return"/api/v1/"+url;}protectedoverrideTaskInitializer(IServiceProviderhostServices){returnTask.CompletedTask;}protectedoverridevoidConfigureTestServices(IServiceCollectionservices){// Replace services for test environment (DB, cache, auth, etc.)}protectedoverrideTask<bool>ValidateResponse(HttpResponseMessageresponse,dynamicoptions){returnTask.FromResult(response.IsSuccessStatusCode);}}

5) Write a Test Class

usingMarkopTest.Attributes;usingXunit;usingXunit.Abstractions;[Endpoint("[controller]/[action]")]publicclassAccountTests:AppFactory{publicAccountTests(ITestOutputHelperoutputHelper):base(outputHelper){}[MarkopTest.Attributes.Theory][InlineData("user@example.com","password")]publicvoidSignIn(stringlogin,stringpassword){PostJson(new{Login=login,Password=password});}}

6) Run Tests

dotnet test

Or run a specific project:

dotnet test sample/test/IntegrationTest/IntegrationTest.csproj