C# 使用 MSTest V2 进行单元测试

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.13.6
.NET 6.0
MSTest V2

正文

建议相关断言方法、方式参考官方文档链接进行学习,本文仅作 demo 记录

步骤

  1. 基于 .NET 6.0 创建 C# 控制台项目,不要将解决方案和项目放到同一目录中。
    image

  2. 编写相关代码,类、方法等。

  3. 在类或方法名上面右键->创建单元测试
    image

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

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

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

代码

  1. 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;
    		}
    	}
    }
    
    
  2. 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));
    		}
    	}
    }
    

运行结果

image

ConsoleApp2Tests
  组中的测试: 5
   总时长: 17 毫秒

结果
   4 已通过
   1 失败
posted @ 2025-06-06 15:11  夏秋初  阅读(89)  评论(0)    收藏  举报