Boxed.DotnetNewTest:.NET模板测试自动化的终极解决方案
【免费下载链接】Framework.NET Core Extensions and Helper NuGet packages.项目地址: https://gitcode.com/gh_mirrors/framework8/Framework
Boxed.DotnetNewTest是一个专为dotnet new项目模板打造的单元测试框架,它能帮助开发者轻松实现模板测试的自动化,显著提升.NET项目模板的质量与可靠性。
🌟 核心功能:一站式模板测试体验
Boxed.DotnetNewTest提供了完整的模板测试生命周期管理,主要功能包括:
1️⃣ 模板安装与项目创建
- 从指定目录安装基于dotnet new的项目模板
- 在临时目录中快速创建模板项目,避免干扰开发环境
2️⃣ 构建流程自动化
- 自动执行
dotnet restore命令,验证依赖项配置 - 运行
dotnet build和dotnet publish命令,检查项目构建完整性
3️⃣ 应用测试支持
- 对ASP.NET Core项目模板提供
dotnet run支持 - 内置
HttpClient,可直接调用应用接口进行功能测试
🚀 快速上手:简单三步实现模板测试
第一步:安装Boxed.DotnetNewTest
通过NuGet安装Boxed.DotnetNewTest包,为你的测试项目添加依赖:
Install-Package Boxed.DotnetNewTest第二步:编写测试代码
创建测试类,使用Boxed.DotnetNewTest的API编写测试用例。以下是一个基本示例:
public class ApiTemplateTest { public ApiTemplateTest() => DotnetNew.Install<ApiTemplateTest>("ApiTemplate.sln").Wait(); [Fact] public async Task Run_DefaultArguments_IsSuccessful() { using (var tempDirectory = TempDirectory.NewTempDirectory()) { var project = await tempDirectory.DotnetNew("api", "DefaultArguments"); await project.DotnetRestore(); await project.DotnetBuild(); await project.DotnetRun( @"Source\DefaultArguments", async (httpClient, httpsClient) => { var httpResponse = await httpsClient.GetAsync("status"); Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); }); } } }第三步:运行测试
使用你喜欢的测试运行器执行测试,Boxed.DotnetNewTest会自动处理模板安装、项目创建、构建和测试的全过程。
💡 高级用法:参数化测试与自定义配置
参数化测试
通过[Theory]和[InlineData]特性,可以轻松实现多参数组合测试:
[Theory] [InlineData("StatusEndpointOn", "status-endpoint=true")] [InlineData("StatusEndpointOff", "status-endpoint=false")] public async Task RestoreAndBuild_CustomArguments_IsSuccessful(string name, params string[] arguments) { using (var tempDirectory = TempDirectory.NewTempDirectory()) { var dictionary = arguments .Select(x => x.Split('=', StringSplitOptions.RemoveEmptyEntries)) .ToDictionary(x => x.First(), x => x.Last()); var project = await tempDirectory.DotnetNew("api", name, dictionary); await project.DotnetRestore(); await project.DotnetBuild(); } }临时目录管理
Boxed.DotnetNewTest提供了灵活的临时目录管理功能,确保测试环境的隔离与清洁:
TempDirectory.NewTempDirectory():创建标准临时目录TempDirectory.NewShortTempDirectory():创建短路径临时目录,解决MSBuild 256字符路径限制问题
📦 项目结构与核心组件
Boxed.DotnetNewTest的核心代码位于Source/Boxed.DotnetNewTest/目录,主要组件包括:
- DotnetNew:模板安装与项目创建的核心类
- TempDirectory:临时目录管理,实现
IAsyncDisposable接口 - Project:项目对象,提供构建、运行等操作方法
- ProjectExtensions:扩展方法类,提供
DotnetRestore、DotnetBuild等便捷方法
📚 官方文档与资源
- 详细文档:Documentation/Boxed.DotnetNewTest.md
- 源代码:Source/Boxed.DotnetNewTest/
- 测试示例:Tests/
🔧 如何贡献
如果你对Boxed.DotnetNewTest感兴趣,欢迎通过以下方式贡献:
- 克隆仓库:
git clone https://gitcode.com/gh_mirrors/framework8/Framework - 创建分支:
git checkout -b feature/your-feature - 提交更改:
git commit -m "Add your feature" - 推送分支:
git push origin feature/your-feature - 创建Pull Request
Boxed.DotnetNewTest为.NET模板开发提供了强大的测试支持,让模板质量保障变得简单高效。无论是个人开发者还是企业团队,都能从中受益,打造更可靠的.NET项目模板。
【免费下载链接】Framework.NET Core Extensions and Helper NuGet packages.项目地址: https://gitcode.com/gh_mirrors/framework8/Framework
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考