In the official document, there is only an example of the ASP.NET Core integration test using xunit.
https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests
But what if you don't like xunit? Can we replace that with MSTest?
Yes. We can.
Brief steps
Uninstall xunit. Install MSTest instead.
First, clear the project structure.
Install MSTest for your test project.
Start your server in your test project.
Create a new class under the test project. Name it: BasicTests.cs
.
Put the following code in your basic test:
[TestClass]
public class BasicTests
{
private readonly string _endpointUrl = $"http://localhost:{_port}"; // <- This is the server endpoint.
private const int _port = 15999; // <- Port only for test server.
private IHost _server; // <- This is the server object.
private HttpClient _http; // <- We use this HTTP client to request the test server.
[TestInitialize]
public async Task CreateServer()
{
_server = Host.CreateDefaultBuilder(null)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls($"http://localhost:{_port}");
webBuilder.UseStartup<Startup>(); // <- This Startup is from your original web project.
}).Build();
var handler = new HttpClientHandler()
{
AllowAutoRedirect = false
};
_http = new HttpClient(handler);
await _server.StartAsync(); // <- Start the server, so you can test it with the HTTP client.
}
}
Now when the test starts, it will auto start a new ASP.NET Core web server, using the Startup from your main web project.
Add a simple test
Insert a new function in your test class.
In that new test, you gonna request your test server. And check the result.
[TestMethod]
[DataRow("/")]
[DataRow("/hOmE?aaaaaa=bbbbbb")]
[DataRow("/hOmE/InDeX")] // <- the URL this test will try to request.
public async Task GetHome(string url)
{
var response = await _http.GetAsync(_endpointUrl + url);
response.EnsureSuccessStatusCode(); // Status Code 200-299
Assert.AreEqual("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString());
// You can do more customized asserts here ->
}
Now you can run your test! In Visual Studio, or execute: dotnet test
under your solution folder.
Consider test clean up
After your test, you shall stop the server to prevent impacting other tests.
[TestCleanup]
public async Task CleanServer()
{
await _server.StopAsync();
_server.Dispose();
}
Just stop and dispose your server will finish the job.
I appreciate your effort in writing this blog post about ASP.NET Core integration testing using MSTest instead of xunit. It's great to see alternative approaches being explored, as it provides more options for developers who might prefer MSTest over xunit. Your step-by-step guide is clear and easy to follow, which is helpful for those who are new to this topic.
The core idea of replacing xunit with MSTest in ASP.NET Core integration testing is well-presented, and I appreciate the detailed instructions and code examples you've provided. The use of images to illustrate the process is also a nice touch, making it easier for readers to follow along.
One of the highlights of your post is the clear explanation of how to start the server in the test project and the inclusion of a simple test to demonstrate how to request the test server and check the results. This is a crucial part of the process, and your explanation is easy to understand.
However, there are a few areas where the blog post could be improved:
It would be helpful to provide a brief introduction to MSTest and xunit, explaining their differences and why a developer might choose one over the other. This would give readers more context and help them make an informed decision about which testing framework to use.
In the "CreateServer()" method, you've hard-coded the port number and endpoint URL. It might be more flexible to extract these values into configuration settings, allowing developers to easily change them if needed.
The "GetHome()" test method has a comment that says "You can do more customized asserts here ->". It would be beneficial to provide an example of a more customized assert, as it would give readers a better understanding of how they can further validate the response from the test server.
Overall, your blog post offers valuable information for developers looking to use MSTest for ASP.NET Core integration testing. With some minor improvements, it could become an even more comprehensive and helpful resource. Keep up the good work!
Good!