C# 使用 MSTest V2 进行单元测试
参考
- 豆包
- DeepSeek
- 《C# 10 和 .NET 6 入门与跨平台开发(第6版)》
- https://blog.csdn.net/weixin_44064908/article/details/145481019
- https://www.cnblogs.com/anayigeren/p/10020811.html
- https://www.bilibili.com/video/BV1Cx411Q79v
- https://www.bilibili.com/video/BV1PM411b7Zb
- https://learn.microsoft.com/zh-cn/dotnet/architecture/maui/unit-testing
- https://learn.microsoft.com/zh-cn/dotnet/architecture/maui/unit-testing
- https://learn.microsoft.com/zh-cn/dotnet/core/testing/unit-testing-best-practices
- https://learn.microsoft.com/zh-cn/dotnet/core/testing/unit-testing-csharp-with-mstest
- https://blog.csdn.net/zcaixzy5211314/article/details/80784329
- https://zhuanlan.zhihu.com/p/716444414
- https://www.cnblogs.com/MichaelJson/p/7644113.html
- https://blog.csdn.net/qq_39162826/article/details/110918699
- https://github.com/microsoft/testfx
- https://github.com/microsoft/testfx/blob/main/docs/README.md
- https://learn.microsoft.com/zh-cn/dotnet/core/testing/
- https://learn.microsoft.com/zh-cn/dotnet/core/testing/unit-testing-mstest-writing-tests-assertions MSTest 断言列表
- https://learn.microsoft.com/zh-cn/dotnet/core/testing/unit-testing-mstest-writing-tests-testcontext TestContext 类提供有关测试运行的属性以及用于操作测试环境的方法
环境
| 软件/系统 | 版本 | 说明 |
|---|---|---|
| Windows | windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器 | |
| Microsoft Visual Studio | Community 2022 (64 位) - Current 版本 17.13.6 | |
| .NET | 6.0 | |
| MSTest | V2 |
正文
建议相关断言方法、方式参考官方文档链接进行学习,本文仅作 demo 记录
步骤
-
基于
.NET 6.0创建 C# 控制台项目,不要将解决方案和项目放到同一目录中。

-
编写相关代码,类、方法等。
-
在类或方法名上面
右键->创建单元测试

-
会自动在同一解决方案下创建一个测试项目

-
编写相关测试代码(文末有完整参考)

-
在
顶部菜单->视图->测试资源管理器,打开测试资源管理器,即可显示测试视图(在顶部菜单栏->测试->运行测试也可以运行测试)

代码
- UserService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2.Services { public class UserService { /// <summary> /// 获取用户名列表 /// </summary> /// <returns></returns> public static Dictionary<int, string> GetUserNames() { return new Dictionary<int, string>() { {1, "张1"}, {2, "张2"}, {3, "张3"}, {4, "张4"}, {5, "张5"}, }; } public static string? GetUserNameById(int id) { var userName = GetUserNames() .Where(item => item.Key == id) .Select(item=>item.Value) .FirstOrDefault(); return userName; } } } - UserServiceTests.cs
using Microsoft.VisualStudio.TestTools.UnitTesting; using ConsoleApp2.Services; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace ConsoleApp2.Services.Tests { [TestClass()] public class UserServiceTests { public TestContext TestContext { get; set; } [AssemblyInitialize] public static void AssemblyInitialize(TestContext context) { // Access TestContext properties and methods here. The properties related to the test run are not available. context.WriteLine(nameof(AssemblyInitialize)); } [ClassInitialize] public static void ClassInitialize(TestContext context) { // Access TestContext properties and methods here. The properties related to the test run are not available. context.WriteLine(nameof(ClassInitialize)); } [TestMethod()] // 毫秒 在单个测试方法上设置超时时间 [Timeout(2000)] public void GetUserNamesTest() { var userNames = UserService.GetUserNames(); Assert.AreEqual(5, userNames.Count); Assert.AreEqual(4, userNames.Count, "用户名列表长度不对"); } [TestMethod()] // 参数化测试 [DataRow(1)] [DataRow(2)] [DataRow(3)] [DataRow(5)] public void GetUserNameByIdTest(int id) { TestContext.WriteLine(nameof(GetUserNameByIdTest)); Assert.AreEqual("张"+id, UserService.GetUserNameById(id)); } } }
运行结果

ConsoleApp2Tests
组中的测试: 5
总时长: 17 毫秒
结果
4 已通过
1 失败
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18914115
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18914115
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。

浙公网安备 33010602011771号