diff --git a/ConfigureStudy.Test/ConfigTest.cs b/ConfigureStudy.Test/ConfigTest.cs new file mode 100644 index 0000000..e3ddc12 --- /dev/null +++ b/ConfigureStudy.Test/ConfigTest.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Net.Http; + +using Microsoft.AspNetCore.TestHost; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +namespace ConfigureStudy.Test +{ + [Collection("FullFixture")] + public class ConfigTest:IDisposable + { + private TestServer testServer; + private ConfigFixture config; + + public ConfigTest(TestServerFixture testServerFixture, ConfigFixture config) + { + this.testServer = testServerFixture.TestServer; + this.config = config; + } + + [Fact] + public void DatabaseConnect_Test() + { + var con = config.GetConnectionStringFromJsonConfig(); + Assert.Equal("Server=.\\2008; Database=MyDatabase; User ID=sa; Password=gly-bicijinlian; Trusted_Connection=False;", con); + } + + [Fact] + public async Task TestServer_Test() + { + var client = testServer.CreateClient(); + var responseMessage = await client.GetAsync("/api/values"); + + Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode); + } + + public void Dispose() + { + + } + } +} diff --git a/ConfigureStudy.Test/ConfigureStudy.Test.csproj b/ConfigureStudy.Test/ConfigureStudy.Test.csproj new file mode 100644 index 0000000..a0052a6 --- /dev/null +++ b/ConfigureStudy.Test/ConfigureStudy.Test.csproj @@ -0,0 +1,46 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/ConfigureStudy.Test/DatabaseConfig.json b/ConfigureStudy.Test/DatabaseConfig.json new file mode 100644 index 0000000..4003d24 --- /dev/null +++ b/ConfigureStudy.Test/DatabaseConfig.json @@ -0,0 +1,4 @@ +{ + "DatabaseType": "SQL Server", + "ConnectionString": "Server=.\\2008; Database=MyDatabase; User ID=sa; Password=gly-bicijinlian; Trusted_Connection=False;" +} \ No newline at end of file diff --git a/ConfigureStudy.Test/TestFixture/ConfigFixture.cs b/ConfigureStudy.Test/TestFixture/ConfigFixture.cs new file mode 100644 index 0000000..568c5ea --- /dev/null +++ b/ConfigureStudy.Test/TestFixture/ConfigFixture.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Extensions; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.Json; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +namespace ConfigureStudy.Test +{ + public class ConfigFixture : IDisposable + { + public IConfiguration Configuration; + + public ConfigFixture() + { + IConfiguration configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddJsonFile("DatabaseConfig.json") + .Build(); + this.Configuration = configuration; + } + + public string GetConnectionStringFromJsonConfig() + { + return Configuration.GetValue("ConnectionString"); + } + + public void Dispose() + { + + } + } +} diff --git a/ConfigureStudy.Test/TestFixture/ConfigSetup.cs b/ConfigureStudy.Test/TestFixture/ConfigSetup.cs new file mode 100644 index 0000000..8701b9e --- /dev/null +++ b/ConfigureStudy.Test/TestFixture/ConfigSetup.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +namespace ConfigureStudy.Test +{ + /// + /// 测试集范围,共享配置设置 + /// + [CollectionDefinition(name:"Configure")] + public class ConfigSetup:ICollectionFixture + { + //此接口只做标记,不编写代码 + } +} diff --git a/ConfigureStudy.Test/TestFixture/FullFixtureSetup.cs b/ConfigureStudy.Test/TestFixture/FullFixtureSetup.cs new file mode 100644 index 0000000..cc7019c --- /dev/null +++ b/ConfigureStudy.Test/TestFixture/FullFixtureSetup.cs @@ -0,0 +1,34 @@ + +using System; +using System.Collections.Generic; +using System.Text; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +using ConfigureStudy.Test; + +namespace ConfigureStudy.Test +{ + /// + /// 测试程序集设置 夹具 + /// 在整个测试程序集的所有类共享 + /// 生命周期为整个测试集一个测试周期 + /// + [CollectionDefinition("FullFixture")] + public class FullFixtureSetup : //只在类上做接口标识 + ICollectionFixture, + ICollectionFixture + { + //类内,不做任何代码实现。 + //使用步骤: + // 1、创建测试夹具类:如本例的 ConfigFixture 和 TestServerFixture + // 2、标识测试夹具类:本类 + // 3、测试类中使用: + // a 标注[Collection("名称与设置类CollectionDefinition特性设置的名字保持相同")]特性 + // b 构造函数时,添加“测试夹具类的”参数,构造函数内把参数赋值给私有变量 + // c 测试方法内使用 + } +} diff --git a/ConfigureStudy.Test/TestFixture/TestServerFixture.cs b/ConfigureStudy.Test/TestFixture/TestServerFixture.cs new file mode 100644 index 0000000..746f4d3 --- /dev/null +++ b/ConfigureStudy.Test/TestFixture/TestServerFixture.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using System; +using System.Collections.Generic; +using System.Text; +using ConfigureStudy.WebApp; + +namespace ConfigureStudy.Test +{ + public class TestServerFixture : IDisposable + { + public TestServer TestServer; + + /// + /// 设置测试内存服务器 TestServer + /// + public TestServerFixture() + { + IWebHostBuilder webHostBuilder = new WebHostBuilder(); + + //WebHostBuilderExtensions + webHostBuilder.UseStartup(); + + TestServer = new TestServer(webHostBuilder); + } + + public void Dispose() + { + TestServer = null; + } + } +} diff --git a/ConfigureStudy.Test/TestFixture/TestServerFixtureSetup.cs b/ConfigureStudy.Test/TestFixture/TestServerFixtureSetup.cs new file mode 100644 index 0000000..9a45966 --- /dev/null +++ b/ConfigureStudy.Test/TestFixture/TestServerFixtureSetup.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +namespace ConfigureStudy.Test +{ + /// + /// 测试服务器,全局共享设置 + /// + [CollectionDefinition(name:"GlobalTestServer")] + public class TestServerFixtureSetup : ICollectionFixture + { + + } +} diff --git a/ConfigureStudy.Test/appsettings.json b/ConfigureStudy.Test/appsettings.json new file mode 100644 index 0000000..afb797c --- /dev/null +++ b/ConfigureStudy.Test/appsettings.json @@ -0,0 +1,10 @@ +{ + "exclude": [ + "**/bin", + "**/bower_components", + "**/jspm_packages", + "**/node_modules", + "**/obj", + "**/platforms" + ] +} \ No newline at end of file diff --git a/ConfigureStudy.WebApp/ConfigureStudy.WebApp.csproj b/ConfigureStudy.WebApp/ConfigureStudy.WebApp.csproj new file mode 100644 index 0000000..901dd0f --- /dev/null +++ b/ConfigureStudy.WebApp/ConfigureStudy.WebApp.csproj @@ -0,0 +1,18 @@ + + + + netcoreapp2.1 + + + + + + + + + + + + + + diff --git a/ConfigureStudy.WebApp/Controllers/ValuesController.cs b/ConfigureStudy.WebApp/Controllers/ValuesController.cs new file mode 100644 index 0000000..706fa77 --- /dev/null +++ b/ConfigureStudy.WebApp/Controllers/ValuesController.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace ConfigureStudy.WebApp.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValuesController : ControllerBase + { + // GET api/values + [HttpGet] + public ActionResult> Get() + { + return new string[] { "value1", "value2" }; + } + + // GET api/values/5 + [HttpGet("{id}")] + public ActionResult Get(int id) + { + return "value"; + } + + // POST api/values + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api/values/5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/ConfigureStudy.WebApp/Program.cs b/ConfigureStudy.WebApp/Program.cs new file mode 100644 index 0000000..cbaa64c --- /dev/null +++ b/ConfigureStudy.WebApp/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace ConfigureStudy.WebApp +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git a/ConfigureStudy.WebApp/Properties/launchSettings.json b/ConfigureStudy.WebApp/Properties/launchSettings.json new file mode 100644 index 0000000..ae3a022 --- /dev/null +++ b/ConfigureStudy.WebApp/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:3841", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "ConfigureStudy.WebApp": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/values", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/ConfigureStudy.WebApp/Startup.cs b/ConfigureStudy.WebApp/Startup.cs new file mode 100644 index 0000000..f67772c --- /dev/null +++ b/ConfigureStudy.WebApp/Startup.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace ConfigureStudy.WebApp +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseMvc(); + } + } +} diff --git a/ConfigureStudy.WebApp/appsettings.Development.json b/ConfigureStudy.WebApp/appsettings.Development.json new file mode 100644 index 0000000..e203e94 --- /dev/null +++ b/ConfigureStudy.WebApp/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/ConfigureStudy.WebApp/appsettings.json b/ConfigureStudy.WebApp/appsettings.json new file mode 100644 index 0000000..def9159 --- /dev/null +++ b/ConfigureStudy.WebApp/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/ConfigureStudy.WebAppTest/ConfigureStudy.WebAppTest.csproj b/ConfigureStudy.WebAppTest/ConfigureStudy.WebAppTest.csproj new file mode 100644 index 0000000..4908c47 --- /dev/null +++ b/ConfigureStudy.WebAppTest/ConfigureStudy.WebAppTest.csproj @@ -0,0 +1,27 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + + diff --git a/ConfigureStudy.WebAppTest/GlobalFixture/TestServerFixture.cs b/ConfigureStudy.WebAppTest/GlobalFixture/TestServerFixture.cs new file mode 100644 index 0000000..497bc52 --- /dev/null +++ b/ConfigureStudy.WebAppTest/GlobalFixture/TestServerFixture.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using System; +using System.Collections.Generic; +using System.Text; +using ConfigureStudy.WebApp; + +namespace WebApiStudy.IntegrationTest +{ + public class TestServerFixture : IDisposable + { + public TestServer TestServer; + + /// + /// 设置测试内存服务器 TestServer + /// + public TestServerFixture() + { + TestServer = new TestServer(new WebHostBuilder().UseStartup()); + } + + public void Dispose() + { + TestServer = null; + } + } +} diff --git a/ConfigureStudy.WebAppTest/GlobalFixture/TestServerFixtureSetup.cs b/ConfigureStudy.WebAppTest/GlobalFixture/TestServerFixtureSetup.cs new file mode 100644 index 0000000..ef5dcd7 --- /dev/null +++ b/ConfigureStudy.WebAppTest/GlobalFixture/TestServerFixtureSetup.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Xunit; +using Xunit.Abstractions; +using Xunit.Extensions; +using Xunit.Sdk; + +namespace WebApiStudy.IntegrationTest +{ + /// + /// 测试服务器,全局共享设置 + /// + [CollectionDefinition(name:"GlobalTestServer")] + public class TestServerFixtureSetup : ICollectionFixture + { + + } +} diff --git a/ConfigureStudy.WebAppTest/TestDemo.cs b/ConfigureStudy.WebAppTest/TestDemo.cs new file mode 100644 index 0000000..d3068ac --- /dev/null +++ b/ConfigureStudy.WebAppTest/TestDemo.cs @@ -0,0 +1,36 @@ +using System; + +using Microsoft.AspNetCore.TestHost; + +using Xunit; + +using WebApiStudy.IntegrationTest; +using System.Net; + +namespace ConfigureStudy.WebAppTest +{ + [Collection(name:"GlobalTestServer")] + public class TestDemo:IDisposable + { + private TestServer testServer; + + public TestDemo(TestServerFixture testServer) + { + this.testServer = testServer.TestServer; + } + + [Fact] + public void Test() + { + var client = testServer.CreateClient(); + var apiResponse= client.GetAsync("/api/values").Result; + + Assert.True(apiResponse.StatusCode==HttpStatusCode.OK); + } + + public void Dispose() + { + + } + } +} diff --git a/NetCoreConfigureStudy.sln b/NetCoreConfigureStudy.sln new file mode 100644 index 0000000..aa5773d --- /dev/null +++ b/NetCoreConfigureStudy.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigureStudy.WebApp", "ConfigureStudy.WebApp\ConfigureStudy.WebApp.csproj", "{7EE58B78-7A08-4C39-BB60-2FC210F206A4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigureStudy.WebAppTest", "ConfigureStudy.WebAppTest\ConfigureStudy.WebAppTest.csproj", "{FF73E629-5B1D-4192-9E7A-C1C52F20A597}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigureStudy.Test", "ConfigureStudy.Test\ConfigureStudy.Test.csproj", "{84D88BC9-85EE-4AFC-A2B0-AC7E6575CAF4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7EE58B78-7A08-4C39-BB60-2FC210F206A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7EE58B78-7A08-4C39-BB60-2FC210F206A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7EE58B78-7A08-4C39-BB60-2FC210F206A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7EE58B78-7A08-4C39-BB60-2FC210F206A4}.Release|Any CPU.Build.0 = Release|Any CPU + {FF73E629-5B1D-4192-9E7A-C1C52F20A597}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF73E629-5B1D-4192-9E7A-C1C52F20A597}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF73E629-5B1D-4192-9E7A-C1C52F20A597}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF73E629-5B1D-4192-9E7A-C1C52F20A597}.Release|Any CPU.Build.0 = Release|Any CPU + {84D88BC9-85EE-4AFC-A2B0-AC7E6575CAF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {84D88BC9-85EE-4AFC-A2B0-AC7E6575CAF4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {84D88BC9-85EE-4AFC-A2B0-AC7E6575CAF4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {84D88BC9-85EE-4AFC-A2B0-AC7E6575CAF4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {30750FB2-0FF5-4BED-8F14-EFAAC8F46C46} + EndGlobalSection +EndGlobal