|
| 1 | +## About |
| 2 | + |
| 3 | +`Microsoft.AspNetCore.Mvc.Testing` provides support for writing integration tests for ASP.NET Core apps that utilize MVC or Minimal APIs. |
| 4 | + |
| 5 | +## Key Features |
| 6 | + |
| 7 | +* Copies the dependencies file (`.deps.json`) from the System Under Test (SUT) into the test project's `bin` directory |
| 8 | +* Sets the [content root](https://learn.microsoft.com/aspnet/core/fundamentals/#content-root) to the SUT's project root so that static files are found during test execution |
| 9 | +* Provides the [`WebApplicationFactory`](https://learn.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1) class to streamline bootstrapping the SUT with [`TestServer`](https://learn.microsoft.com/dotnet/api/microsoft.aspnetcore.testhost.testserver) |
| 10 | + |
| 11 | +## How to Use |
| 12 | + |
| 13 | +To use `Microsoft.AspNetCore.Mvc.Testing`, follow these steps: |
| 14 | + |
| 15 | +### Installation |
| 16 | + |
| 17 | +To install the package, run the following command from the directory containing the test project file: |
| 18 | + |
| 19 | +```shell |
| 20 | +dotnet add package Microsoft.AspNetCore.Mvc.Testing |
| 21 | +``` |
| 22 | + |
| 23 | +### Configuration |
| 24 | + |
| 25 | +To configure the test app, follow these steps: |
| 26 | + |
| 27 | +1. Specify the Web SDK in the test project file (`<Project Sdk="Microsoft.NET.Sdk.Web">`). |
| 28 | +2. Add references to the following packages: |
| 29 | + * `xunit` |
| 30 | + * `xunit.runner.visualstudio` |
| 31 | + * `Microsoft.NET.Test.Sdk` |
| 32 | +3. Add a test class to the test project: |
| 33 | + ```csharp |
| 34 | + public class BasicTests |
| 35 | + : IClassFixture<WebApplicationFactory<Program>> |
| 36 | + { |
| 37 | + private readonly WebApplicationFactory<Program> _factory; |
| 38 | + |
| 39 | + public BasicTests(WebApplicationFactory<Program> factory) |
| 40 | + { |
| 41 | + _factory = factory; |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [InlineData("/")] |
| 46 | + [InlineData("/Index")] |
| 47 | + [InlineData("/About")] |
| 48 | + [InlineData("/Privacy")] |
| 49 | + [InlineData("/Contact")] |
| 50 | + public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url) |
| 51 | + { |
| 52 | + // Arrange |
| 53 | + var client = _factory.CreateClient(); |
| 54 | + |
| 55 | + // Act |
| 56 | + var response = await client.GetAsync(url); |
| 57 | + |
| 58 | + // Assert |
| 59 | + response.EnsureSuccessStatusCode(); // Status Code 200-299 |
| 60 | + Assert.Equal("text/html; charset=utf-8", |
| 61 | + response.Content.Headers.ContentType.ToString()); |
| 62 | + } |
| 63 | + } |
| 64 | + ``` |
| 65 | + |
| 66 | +## Additional Documentation |
| 67 | + |
| 68 | +For additional documentation and examples, refer to the [official documentation](https://learn.microsoft.com/aspnet/core/test/integration-tests) on integration testing in ASP.NET Core. |
| 69 | +
|
| 70 | +## Feedback & Contributing |
| 71 | + |
| 72 | +`Microsoft.AspNetCore.Mvc.Testing` is released as open-source under the [MIT license](https://licenses.nuget.org/MIT). Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/aspnetcore). |
0 commit comments