Integration Testing
Integration testing, also known as assembly testing or joint testing. On the basis of unit testing, all modules are assembled into subsystems or systems according to the design requirements (e.g., according to the structure diagram) for integration testing.
The practice shows that although some modules can work independently, it is not guaranteed to work properly. Some of the problems that are partially reflected are likely to be exposed in the global sense.
Excerpt from Baidu Encyclopedia
ASP. NET Core Integration test
Create a new ASP. NET Core Webapi Project:
Modify your own ValuesController
:
public class ValuesController : Controller{ public int Add([FromQuery]int a,[FromQuery] int b) { return a + b; }}
Test with Postman:
After testing our API there is no problem.
Add a Xunit test project
Add a NuGet Microsoft.AspNetCore.TestHost
package:
To add a reference to a WEBAPI project to a test project:
Add the following test code:
private readonly HttpClient _client;public UnitTest1(){ var builder = new WebHostBuilder().UseStartup<Startup>(); var testServer = new TestServer(builder); _client = testServer.CreateClient();}[Fact]public async Task Test1(){ var result = await _client.GetAsync("Values/Add?a=1&b=2"); result.EnsureSuccessStatusCode(); var data = await result.Content.ReadAsStringAsync(); Assert.Equal("3", data);}
Run it:
As you can see, a simple integration test has been passed.
Document reference
Official Document Https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing
Demo:https://github.com/stulzq/blogdemos/tree/master/integrationtests
ASP. NET Core Integration test