Support for writing functional tests for MVC applications. This package was built from the source code at https://github.com/dotnet/dotnet/tree/87bc0b04e21d786669142109a5128c95618b75ed
$ dotnet add package Microsoft.AspNetCore.Mvc.TestingMicrosoft.AspNetCore.Mvc.Testing provides support for writing integration tests for ASP.NET Core apps that utilize MVC or Minimal APIs.
.deps.json) from the System Under Test (SUT) into the test project's bin directoryWebApplicationFactory class to streamline bootstrapping the SUT with TestServerTo use Microsoft.AspNetCore.Mvc.Testing, follow these steps:
To install the package, run the following command from the directory containing the test project file:
dotnet add package Microsoft.AspNetCore.Mvc.Testing
To configure the test app, follow these steps:
<Project Sdk="Microsoft.NET.Sdk.Web">).xunitxunit.runner.visualstudioMicrosoft.NET.Test.Sdkpublic class BasicTests
: IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
public BasicTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
}
[Theory]
[InlineData("/")]
[InlineData("/Index")]
[InlineData("/About")]
[InlineData("/Privacy")]
[InlineData("/Contact")]
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
{
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetAsync(url);
// Assert
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.Equal("text/html; charset=utf-8",
response.Content.Headers.ContentType.ToString());
}
}
For additional documentation and examples, refer to the official documentation on integration testing in ASP.NET Core.
Microsoft.AspNetCore.Mvc.Testing is released as open-source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.