-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide
Microsoft.AspNetCore.Mvc.Testing
package README (#57736)
Co-authored-by: Martin Costello <[email protected]>
- Loading branch information
1 parent
587b0dd
commit 736936f
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
## About | ||
|
||
`Microsoft.AspNetCore.Mvc.Testing` provides support for writing integration tests for ASP.NET Core apps that utilize MVC or Minimal APIs. | ||
|
||
## Key Features | ||
|
||
* Copies the dependencies file (`.deps.json`) from the System Under Test (SUT) into the test project's `bin` directory | ||
* 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 | ||
* 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) | ||
|
||
## How to Use | ||
|
||
To use `Microsoft.AspNetCore.Mvc.Testing`, follow these steps: | ||
|
||
### Installation | ||
|
||
To install the package, run the following command from the directory containing the test project file: | ||
|
||
```shell | ||
dotnet add package Microsoft.AspNetCore.Mvc.Testing | ||
``` | ||
|
||
### Configuration | ||
|
||
To configure the test app, follow these steps: | ||
|
||
1. Specify the Web SDK in the test project file (`<Project Sdk="Microsoft.NET.Sdk.Web">`). | ||
2. Add references to the following packages: | ||
* `xunit` | ||
* `xunit.runner.visualstudio` | ||
* `Microsoft.NET.Test.Sdk` | ||
3. Add a test class to the test project: | ||
```csharp | ||
public 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()); | ||
} | ||
} | ||
``` | ||
|
||
## Additional Documentation | ||
|
||
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. | ||
## Feedback & Contributing | ||
|
||
`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). |