From 6d9caceeaa54cd4ca2f36ee78bc86f5a8c1526ea Mon Sep 17 00:00:00 2001 From: wanggaofeng <15601716045@163.com> Date: Wed, 13 Mar 2024 21:03:15 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XUnitDIStudy.Dal/Class1.cs | 9 +++ XUnitDIStudy.Dal/XUnitDIStudy.Dal.csproj | 8 ++ XUnitDIStudy.IntegrationTest/BasicTest.cs | 42 ++++++++++ .../CustomAttribute.cs | 23 ++++++ XUnitDIStudy.IntegrationTest/GlobalUsing.cs | 10 +++ .../Properties/launchSettings.json | 12 +++ XUnitDIStudy.IntegrationTest/UseXUnit.cs | 27 +++++++ .../XUnitDIStudy.IntegrationTest.csproj | 35 ++++++++ XUnitDIStudy.IntegrationTest/学习.md | 81 +++++++++++++++++++ .../IntegrationTest/TestServerManger.cs | 13 --- XUnitDIStudy.sln | 44 ++++++++-- 11 files changed, 286 insertions(+), 18 deletions(-) create mode 100644 XUnitDIStudy.Dal/Class1.cs create mode 100644 XUnitDIStudy.Dal/XUnitDIStudy.Dal.csproj create mode 100644 XUnitDIStudy.IntegrationTest/BasicTest.cs create mode 100644 XUnitDIStudy.IntegrationTest/CustomAttribute.cs create mode 100644 XUnitDIStudy.IntegrationTest/GlobalUsing.cs create mode 100644 XUnitDIStudy.IntegrationTest/Properties/launchSettings.json create mode 100644 XUnitDIStudy.IntegrationTest/UseXUnit.cs create mode 100644 XUnitDIStudy.IntegrationTest/XUnitDIStudy.IntegrationTest.csproj create mode 100644 XUnitDIStudy.IntegrationTest/学习.md delete mode 100644 XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs diff --git a/XUnitDIStudy.Dal/Class1.cs b/XUnitDIStudy.Dal/Class1.cs new file mode 100644 index 0000000..17ce876 --- /dev/null +++ b/XUnitDIStudy.Dal/Class1.cs @@ -0,0 +1,9 @@ +using System; + +namespace XUnitDIStudy.Dal +{ + public class Class1 + { + + } +} diff --git a/XUnitDIStudy.Dal/XUnitDIStudy.Dal.csproj b/XUnitDIStudy.Dal/XUnitDIStudy.Dal.csproj new file mode 100644 index 0000000..b4b43f4 --- /dev/null +++ b/XUnitDIStudy.Dal/XUnitDIStudy.Dal.csproj @@ -0,0 +1,8 @@ + + + + netstandard2.1 + enable + + + diff --git a/XUnitDIStudy.IntegrationTest/BasicTest.cs b/XUnitDIStudy.IntegrationTest/BasicTest.cs new file mode 100644 index 0000000..844a2af --- /dev/null +++ b/XUnitDIStudy.IntegrationTest/BasicTest.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Mvc.Testing; + +using Xunit.Abstractions; + +using XUnitDIStudy.Model; + +namespace XUnitDIStudy.IntegrationTest +{ + + public class BasicTest:IDisposable,IClassFixture> + { + private readonly ITestOutputHelper _output; + private readonly WebApplicationFactory _factory; + + public BasicTest(ITestOutputHelper outputHelper,WebApplicationFactory factory) + { + _output = outputHelper; + _factory = factory; + } + + [Fact,Custom] + public async void Client_Test() + { + var httpClient = _factory.CreateClient(); + + var respone = await httpClient.GetAsync("Default/GetAll"); + + respone.EnsureSuccessStatusCode(); + + var students = await respone.Content.ReadFromJsonAsync>(); + + Assert.NotNull(students); + Assert.NotEmpty(students); + Assert.True(students.Count>0); + } + + public void Dispose() + { + + } + } +} \ No newline at end of file diff --git a/XUnitDIStudy.IntegrationTest/CustomAttribute.cs b/XUnitDIStudy.IntegrationTest/CustomAttribute.cs new file mode 100644 index 0000000..43f2d7c --- /dev/null +++ b/XUnitDIStudy.IntegrationTest/CustomAttribute.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace XUnitDIStudy.IntegrationTest +{ + //[AttributeUsage(validOn: AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)] + public class CustomAttribute: BeforeAfterTestAttribute + { + public override void Before(MethodInfo methodUnderTest) + { + + } + + public override void After(MethodInfo methodUnderTest) + { + + } + } +} diff --git a/XUnitDIStudy.IntegrationTest/GlobalUsing.cs b/XUnitDIStudy.IntegrationTest/GlobalUsing.cs new file mode 100644 index 0000000..7ed1bd2 --- /dev/null +++ b/XUnitDIStudy.IntegrationTest/GlobalUsing.cs @@ -0,0 +1,10 @@ +global using System; +global using System.Collections.Generic; +global using System.Linq; +global using System.Text; +global using System.Threading.Tasks; + +global using Xunit; +global using Xunit.Sdk; +global using Xunit.Extensions; +global using Xunit.Abstractions; \ No newline at end of file diff --git a/XUnitDIStudy.IntegrationTest/Properties/launchSettings.json b/XUnitDIStudy.IntegrationTest/Properties/launchSettings.json new file mode 100644 index 0000000..0bd0e77 --- /dev/null +++ b/XUnitDIStudy.IntegrationTest/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "XUnitDIStudy.IntegrationTest": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:53390;http://localhost:53391" + } + } +} \ No newline at end of file diff --git a/XUnitDIStudy.IntegrationTest/UseXUnit.cs b/XUnitDIStudy.IntegrationTest/UseXUnit.cs new file mode 100644 index 0000000..28dbb68 --- /dev/null +++ b/XUnitDIStudy.IntegrationTest/UseXUnit.cs @@ -0,0 +1,27 @@ +using Xunit.Abstractions; + +namespace XUnitDIStudy.IntegrationTest +{ + + public class UseXUnit:IDisposable + { + private readonly ITestOutputHelper _output; + public UseXUnit(ITestOutputHelper outputHelper) + { + _output = outputHelper; + } + + [Fact,Custom] + public void UseXunit_Test() + { + _output.WriteLine("ʹ xUnit ԪԿܣ"); + + Assert.True(true,"ʹ xUnit ԪԿܣ"); + } + + public void Dispose() + { + + } + } +} \ No newline at end of file diff --git a/XUnitDIStudy.IntegrationTest/XUnitDIStudy.IntegrationTest.csproj b/XUnitDIStudy.IntegrationTest/XUnitDIStudy.IntegrationTest.csproj new file mode 100644 index 0000000..c14b719 --- /dev/null +++ b/XUnitDIStudy.IntegrationTest/XUnitDIStudy.IntegrationTest.csproj @@ -0,0 +1,35 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/XUnitDIStudy.IntegrationTest/学习.md b/XUnitDIStudy.IntegrationTest/学习.md new file mode 100644 index 0000000..81c783d --- /dev/null +++ b/XUnitDIStudy.IntegrationTest/学习.md @@ -0,0 +1,81 @@ +使用 xUnit.net 进行 ASP.NET Core 中的集成测试 +========================================= +## 集成测试简介 +与单元测试相比,集成测试可在更广泛的级别上评估应用的组件。 单元测试用于测试独立软件组件,如单独的类方法。 集成测试确认两个或更多应用组件一起工作以生成预期结果,可能包括完整处理请求所需的每个组件。 + +这些更广泛的测试用于测试应用的基础结构和整个框架,通常包括以下组件: + ++ 数据库 ++ 文件系统 ++ 网络设备 ++ 请求-响应管道 + +单元测试使用称为 fake 或 mock 对象的制造组件,而不是基础结构组件。 + +与单元测试相比,集成测试: + ++ 使用应用在生产环境中使用的实际组件。 ++ 需要进行更多代码和数据处理。 ++ 需要更长时间来运行。 + +因此,将集成测试的使用限制为最重要的基础结构方案。 如果可以使用单元测试或集成测试来测试行为,请选择单元测试。 + +在集成测试的讨论中,测试的项目经常称为“测试中的系统”,简称“SUT”。 本文中使用“SUT”来指代要测试的 ASP.NET Core 应用。 + +请勿为通过数据库和文件系统进行的数据和文件访问的每个排列编写集成测试。 无论应用中有多少位置与数据库和文件系统交互,一组集中式读取、写入、更新和删除集成测试通常能够充分测试数据库和文件系统组件。 将单元测试用于与这些组件交互的方法逻辑的例程测试。 在单元测试中,使用基础结构 fake 或 mock 会导致更快地执行测试。 + +## ASP.NET Core 集成测试 +ASP.NET Core 中的集成测试需要以下内容: + ++ 测试项目用于包含和执行测试。 测试项目具有对 SUT 的引用。 ++ 测试项目为 SUT 创建测试 Web 主机,并使用测试服务器客户端处理 SUT 的请求和响应。 ++ 测试运行程序用于执行测试并报告测试结果。 + +集成测试后跟一系列事件,包括常规“排列”、“操作”和“断言”测试步骤: + ++ 已配置 SUT 的 Web 主机。 ++ 创建测试服务器客户端以向应用提交请求。 ++ 执行“排列”测试步骤:测试应用会准备请求。 ++ 执行“操作”测试步骤:客户端提交请求并接收响应。 ++ 执行“断言”测试步骤:实际响应基于预期响应验证为通过或失败。 ++ 该过程会一直继续,直到执行了所有测试。 ++ 报告测试结果。 + +通常,测试 Web 主机的配置与用于测试运行的应用常规 Web 主机不同。 例如,可以将不同的数据库或不同的应用设置用于测试。 + +基础结构组件(如测试 Web 主机和内存中测试服务器 (TestServer))由 Microsoft.AspNetCore.Mvc.Testing 包提供或管理。 使用此包可简化测试创建和执行。 + +Microsoft.AspNetCore.Mvc.Testing 包处理以下任务: + ++ 将依赖项文件 (.deps) 从 SUT 复制到测试项目的 bin 目录中。 ++ 将内容根目录设置为 SUT 的项目根目录,以便可在执行测试时找到静态文件和页面/视图。 ++ 提供 WebApplicationFactory 类,以简化 SUT 在 TestServer 中的启动过程。 + +单元测试文档介绍如何设置测试项目和测试运行程序,以及有关如何运行测试的详细说明与有关如何命名测试和测试类的建议。 + +将单元测试与集成测试分隔到不同的项目中。 分隔测试: + ++ 有助于确保不会意外地将基础结构测试组件包含在单元测试中。 ++ 允许控制运行的测试集。 + +Razor Pages 应用与 MVC 应用的测试配置之间几乎没有任何区别。 唯一的区别在于测试的命名方式。 在 Razor Pages 应用中,页面终结点的测试通常以页面模型类命名(例如,IndexPageTests 用于为索引页面测试组件集成)。 在 MVC 应用中,测试通常按控制器类进行组织,并以它们所测试的控制器来命令(例如 HomeControllerTests 用于为 Home 控制器测试组件集成)。 + +## 测试应用先决条件 + +测试项目必须: + ++ 引用 Microsoft.AspNetCore.Mvc.Testing 包。 ++ 在项目文件中指定 Web SDK ()。 + +可以在示例应用中查看这些先决条件。 检查 tests/RazorPagesProject.Tests/RazorPagesProject.Tests.csproj 文件。 示例应用使用 xUnit 测试框架和 AngleSharp 分析程序库,因此示例应用还引用: + ++ AngleSharp ++ xunit ++ xunit.runner.visualstudio + +在使用 xunit.runner.visualstudio 版本 2.4.2 或更高版本的应用中,测试项目必须引用 Microsoft.NET.Test.Sdk 包。 + +还在测试中使用了 Entity Framework Core。 请参阅 GitHub 中的项目文件。 + +## SUT 环境 +如果未设置 SUT 的环境,则环境会默认为“开发” \ No newline at end of file diff --git a/XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs b/XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs deleted file mode 100644 index 4de1251..0000000 --- a/XUnitDIStudy.Test/IntegrationTest/TestServerManger.cs +++ /dev/null @@ -1,13 +0,0 @@ -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.sln b/XUnitDIStudy.sln index bf1b5b9..b42f3e2 100644 --- a/XUnitDIStudy.sln +++ b/XUnitDIStudy.sln @@ -1,15 +1,33 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30804.86 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34707.107 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.Service", "XUnitDIStudy.Service\XUnitDIStudy.Service.csproj", "{12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitDIStudy.Service", "XUnitDIStudy.Service\XUnitDIStudy.Service.csproj", "{12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitDIStudy.WebApp", "XUnitDIStudy.WebApp\XUnitDIStudy.WebApp.csproj", "{4E18B97C-D40D-449E-AB78-F97B5D4913F3}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitDIStudy.Test", "XUnitDIStudy.Test\XUnitDIStudy.Test.csproj", "{4D1A2657-8B91-49CD-97C3-F34FD3D6DCB8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.Model", "XUnitDIStudy.Model\XUnitDIStudy.Model.csproj", "{706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitDIStudy.Model", "XUnitDIStudy.Model\XUnitDIStudy.Model.csproj", "{706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "基础应用", "基础应用", "{7EF80348-41C2-4BAF-B52D-263AE6A832E3}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "集成测试", "集成测试", "{5F42BFB8-BCD2-4E2C-BD50-75534ACC8AF2}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DI", "DI", "{9384C32E-E383-4625-B48D-707D7BE538F0}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "xUnit扩展", "xUnit扩展", "{30D4CC98-D94C-4894-A3D4-0BD9B26FB0EA}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "高级应用", "高级应用", "{81AEDC11-96DD-4234-B5C3-1337E4EFE5D7}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mock", "Mock", "{28CFAFF7-4327-4AD1-9704-68B2D18DD11A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.Dal", "XUnitDIStudy.Dal\XUnitDIStudy.Dal.csproj", "{BEE104E2-2330-4EA1-9F98-E6F93DBB5841}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "辅助项目", "辅助项目", "{1F4F4EC9-93B4-47B2-895B-6D7B1974B27E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.IntegrationTest", "XUnitDIStudy.IntegrationTest\XUnitDIStudy.IntegrationTest.csproj", "{B7156A81-71CA-438A-BF18-9C01B7D77FB3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,10 +51,26 @@ Global {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}.Debug|Any CPU.Build.0 = Debug|Any CPU {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}.Release|Any CPU.ActiveCfg = Release|Any CPU {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}.Release|Any CPU.Build.0 = Release|Any CPU + {BEE104E2-2330-4EA1-9F98-E6F93DBB5841}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEE104E2-2330-4EA1-9F98-E6F93DBB5841}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEE104E2-2330-4EA1-9F98-E6F93DBB5841}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEE104E2-2330-4EA1-9F98-E6F93DBB5841}.Release|Any CPU.Build.0 = Release|Any CPU + {B7156A81-71CA-438A-BF18-9C01B7D77FB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7156A81-71CA-438A-BF18-9C01B7D77FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7156A81-71CA-438A-BF18-9C01B7D77FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7156A81-71CA-438A-BF18-9C01B7D77FB3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F} = {1F4F4EC9-93B4-47B2-895B-6D7B1974B27E} + {4E18B97C-D40D-449E-AB78-F97B5D4913F3} = {1F4F4EC9-93B4-47B2-895B-6D7B1974B27E} + {4D1A2657-8B91-49CD-97C3-F34FD3D6DCB8} = {7EF80348-41C2-4BAF-B52D-263AE6A832E3} + {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB} = {1F4F4EC9-93B4-47B2-895B-6D7B1974B27E} + {BEE104E2-2330-4EA1-9F98-E6F93DBB5841} = {1F4F4EC9-93B4-47B2-895B-6D7B1974B27E} + {B7156A81-71CA-438A-BF18-9C01B7D77FB3} = {5F42BFB8-BCD2-4E2C-BD50-75534ACC8AF2} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F7A69734-95DF-437A-92F2-D8805ED1A10D} EndGlobalSection