.netcore continuous integration testing chapter of the .net core 2.1 integration testing project

Series catalog

From .net to .net core later, Microsoft worked very hard every year to one or two large versions of the .net core frequency in evolution, last year have released .net core 2.1 and 2.2, where 2.1 is the version supported by long-term, continuous fast updates on the one hand to quickly make up for the missing api compared .net framework, it also brings a lot of radical change, resulting in many api, especially compared to more traditional framework of the new api continue to adjust, some api on a version can also use the results to support the next version will not upgrade to 2.1 later, Microsoft changed the package name integration testing server and change the 2.0 interface for higher-level package, reducing the difficulty of using the configuration but as of this writing VisualStudio tool support is still not very good, csproj need to modify the file manually. However, this work is also a one-off. Configuring good to ok friends.

See here may be some comrades worry that a lot of things before learning the interface changes are white blind, it is not true, just create TestServer way changed, is the most HttpClient object we actually used in the test, it does not change .

.Net introduce the following lower core 2.1 How to build a memory integrated test server.
First, we create a new .net core mvc 2.1 project, and create a Xunit unit test project, refer to this newly created mvc project, on creating as before , not repeat them here.

Download the following two packages:Microsoft.AspNetCore.App和Microsoft.AspNetCore.Mvc.Testing

We create a file called netcoremvc21test class, its code is as follows

  public class netcoremvc21: IClassFixture<WebApplicationFactory<CoreMvc21.Startup>>
    {
        //private readonly WebApplicationFactory<CoreMvc21.Startup> _factory;
        private HttpClient client;
        public netcoremvc21(WebApplicationFactory<CoreMvc21.Startup> factory)
        {
            this.client = factory.CreateClient();
        }
        [Fact]
        public async Task GetTest()
        {
            var response = await client.GetAsync("/Home/Hello");
            response.EnsureSuccessStatusCode();
            var responseStr = await response.Content.ReadAsStringAsync();
            Assert.Equal("Hello,world", responseStr);
        }
    }
}

Our project was created to achieve the IClassFixture generic interface, we talked about this in front of the role of the interface, not repeat them here, inside it is a generic parameter WebApplicationFactorygeneric object that Microsoft is good, we do not need to own created, this parameter is a generic object TEntryPoint objects, in fact, specified program startup file (here we provide is mvc project's startup files, this project called CoreMvc21). it is actually to create a memory test server approach to wraps, similar to our previous package, reducing the amount of hand-written code, and provides a best practice, we had mentioned earlier, if Xunit not familiar with creating unmanaged objects in the constructor non-serious performance problems.

No big difference between the test code and below the foregoing, an object is a request by httpclient configuration.

Test interface data is no problem, we look at this is not configured ContentRoot, the program can not find the page correctly

Test code as follows

        [Fact]
        public async Task GetTest()
        {
            var response = await client.GetAsync("/Home/index");
            response.EnsureSuccessStatusCode();
            var responseStr = await response.Content.ReadAsStringAsync();
            Assert.Contains("myCarousel", responseStr);
        }

The code above test is passed, that is, we do not need extra configuration, the basic function can work normally. The current premise is above the normal running of the project is conventionally configured, if your project resource file and not in the same directory next, the more it does not work out, then we can be inherited WebApplicationFactory<TEntryPoint>, and 2.0 front .net core configuration is basically similar to customize the configuration.

Some colleagues may be in doubt, where the working environment is not configured, it is not the Development environment, the answer yes.

As of posting time, .net core has reached the 3.0 preview 7 because of busy work, combined with new technologies and new frameworks are not as passionate previously, I have not tried .net core above 3.0. Method applies only to .net core 2.1 and 2.2 in two versions (not available for version 2.0, version 2.0 About this series of integration tests are also described, interested friends can read it).

Guess you like

Origin www.cnblogs.com/tylerzhou/p/11375098.html