ASP. NET Core Integration test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.