From 61f18bcfd493ad809f3f37dee782e0becaee4af6 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sun, 27 Dec 2020 17:03:26 +0800 Subject: [PATCH] WebAppFactory --- .../IntegrationTest/CustomWebAppFactory.cs | 58 +++++++++++++++++++ .../DefaultWebApplicationFactoryTest.cs | 45 ++++++++++++++ .../IntegrationTest/IntegrationSetup.cs | 21 +++++++ .../IntegrationTest/TestServerManger.cs | 13 +++++ XUnitDIStudy.Test/TestConst.cs | 13 +++++ 5 files changed, 150 insertions(+) create mode 100644 XUnitDIStudy.Test/IntegrationTest/CustomWebAppFactory.cs create mode 100644 XUnitDIStudy.Test/IntegrationTest/DefaultWebApplicationFactoryTest.cs create mode 100644 XUnitDIStudy.Test/IntegrationTest/IntegrationSetup.cs create mode 100644 XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs create mode 100644 XUnitDIStudy.Test/TestConst.cs diff --git a/XUnitDIStudy.Test/IntegrationTest/CustomWebAppFactory.cs b/XUnitDIStudy.Test/IntegrationTest/CustomWebAppFactory.cs new file mode 100644 index 0000000..6b084b3 --- /dev/null +++ b/XUnitDIStudy.Test/IntegrationTest/CustomWebAppFactory.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace XUnitDIStudy.Test.IntegrationTest +{ + /// + /// 自定义WebApp工厂 + /// + public class CustomWebAppFactory : WebApplicationFactory where TStartup : class + { + protected override IHostBuilder CreateHostBuilder() + { + return base + .CreateHostBuilder() + .ConfigureHostConfiguration(config => + { + config.AddEnvironmentVariables("ASPNETCORE"); + }); + } + + protected override IWebHostBuilder CreateWebHostBuilder() + { + return base + .CreateWebHostBuilder() + .UseEnvironment("Testing"); + } + + protected override TestServer CreateServer(IWebHostBuilder builder) + { + return base.CreateServer(builder); + } + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + //Startup.ConfigureServices之后执行 + builder.ConfigureServices(services => + { + + }); + + //后执行 + builder.ConfigureTestServices(services => + { + + }); + + } + } +} diff --git a/XUnitDIStudy.Test/IntegrationTest/DefaultWebApplicationFactoryTest.cs b/XUnitDIStudy.Test/IntegrationTest/DefaultWebApplicationFactoryTest.cs new file mode 100644 index 0000000..2da6fe4 --- /dev/null +++ b/XUnitDIStudy.Test/IntegrationTest/DefaultWebApplicationFactoryTest.cs @@ -0,0 +1,45 @@ +using Microsoft.AspNetCore.Mvc.Testing; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +using XUnitDIStudy.Model; + +namespace XUnitDIStudy.Test.IntegrationTest +{ + [Collection(TestConst.IntegrationCollectionDefinitionKey)] + public class DefaultWebApplicationFactoryTest + { + private readonly WebApplicationFactory _factory; + public DefaultWebApplicationFactoryTest(WebApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task TestAsync() + { + // Arrange + var client = _factory.CreateClient(); + + // Act + var response = await client.GetAsync("/Default/GetAll"); + + // Assert + response.EnsureSuccessStatusCode(); // Status Code 200-299 + + List result = System.Text.Json.JsonSerializer.Deserialize>(response.Content.ReadAsStringAsync().Result); + + Assert.NotNull(result); + Assert.True(result.Count > 0); + } + } +} diff --git a/XUnitDIStudy.Test/IntegrationTest/IntegrationSetup.cs b/XUnitDIStudy.Test/IntegrationTest/IntegrationSetup.cs new file mode 100644 index 0000000..32d9f39 --- /dev/null +++ b/XUnitDIStudy.Test/IntegrationTest/IntegrationSetup.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.Mvc.Testing; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +namespace XUnitDIStudy.Test.IntegrationTest +{ + [CollectionDefinition(TestConst.IntegrationCollectionDefinitionKey)] + public class IntegrationSetup:ICollectionFixture> + { + + } +} diff --git a/XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs b/XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs new file mode 100644 index 0000000..4de1251 --- /dev/null +++ b/XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace XUnitDIStudy.Test.IntegrationTest +{ + public class TestServerManger + { + + } +} diff --git a/XUnitDIStudy.Test/TestConst.cs b/XUnitDIStudy.Test/TestConst.cs new file mode 100644 index 0000000..b62bd47 --- /dev/null +++ b/XUnitDIStudy.Test/TestConst.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace XUnitDIStudy.Test +{ + public class TestConst + { + public const string IntegrationCollectionDefinitionKey = "IntegrationTest"; + } +}