关于 mstest-skill
mstest-skill 是使用 MSTest 测试 .NET 代码的参考技能,面向 .NET 测试开发者。内容涵盖测试生命周期、数据驱动测试、断言、集成测试、测试数据、日志和 CI/CD 模式。
主要功能
- 01基础与数据驱动测试模式
- 02断言、分类和超时设置
- 03测试及程序集生命周期钩子
- 04使用 WebApplicationFactory 测试 API 集成
- 05测试数据、日志和 CI/CD 指引
适用场景
- 01为 .NET 代码编写参数化测试
- 02按分类和生命周期组织测试
- 03使用内存数据库构建 API 集成测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill mstest-skill安装后在 Claude Code 中输入/mstest-skill确认已加载。
附带文件reference/advanced-patterns.mdreference/playbook.md
详细介绍
MSTest 测试技能
核心模式
基本测试
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CalculatorTests
{
private Calculator _calc;
[TestInitialize]
public void SetUp() => _calc = new Calculator();
[TestMethod]
public void Add_TwoNumbers_ReturnsSum()
{
Assert.AreEqual(5, _calc.Add(2, 3));
}
[TestMethod]
[ExpectedException(typeof(DivideByZeroException))]
public void Divide_ByZero_Throws()
{
_calc.Divide(10, 0);
}
}
数据驱动测试
[DataTestMethod]
[DataRow(2, 3, 5)]
[DataRow(-1, 1, 0)]
[DataRow(0, 0, 0)]
public void Add_Parameterized(int a, int b, int expected)
{
Assert.AreEqual(expected, _calc.Add(a, b));
}
[DataTestMethod]
[DynamicData(nameof(GetTestData), DynamicDataSourceType.Method)]
public void Add_DynamicData(int a, int b, int expected)
{
Assert.AreEqual(expected, _calc.Add(a, b));
}
private static IEnumerable<object[]> GetTestData()
{
yield return new object[] { 1, 2, 3 };
yield return new object[] { 10, -5, 5 };
}
断言
Assert.AreEqual(expected, actual);
Assert.AreNotEqual(unexpected, actual);
Assert.IsTrue(condition);
Assert.IsFalse(condition);
Assert.IsNull(obj);
Assert.IsNotNull(obj);
Assert.IsInstanceOfType(obj, typeof(MyClass));
Assert.ThrowsException<ArgumentException>(() => Method());
CollectionAssert.Contains(list, item);
CollectionAssert.AreEquivalent(expected, actual);
StringAssert.Contains(str, "substring");
StringAssert.StartsWith(str, "prefix");
StringAssert.Matches(str, new Regex(@"\d+"));
生命周期
[AssemblyInitialize] → Once per assembly (static)
[ClassInitialize] → Once per class (static)
[TestInitialize] → Before each test
[TestMethod] → Test
[TestCleanup] → After each test
[ClassCleanup] → Once after class (static)
[AssemblyCleanup] → Once after assembly (static)
类别
[TestMethod, TestCategory("Smoke")]
public void QuickTest() { }
[TestMethod, Ignore("Bug #456")]
public void SkippedTest() { }
[TestMethod, Timeout(5000)]
public void TimedTest() { }
设置:dotnet add package MSTest.TestFramework MSTest.TestAdapter Microsoft.NET.Test.Sdk
运行:dotnet test 或 dotnet test --filter "TestCategory=Smoke"
深入模式
生产级模式请参阅 reference/playbook.md:
| 章节 | 内容 |
|---|---|
| §1 项目设置 | .csproj 依赖项、配置并行和覆盖率的 .runsettings |
| §2 测试模式 | TestMethod、DataTestMethod、DynamicData、异常测试 |
| §3 FluentAssertions | AssertionScope、异步异常、集合断言 |
| §4 类和程序集初始化 | Testcontainers、共享的高开销设置、全局配置 |
| §5 WebApplicationFactory | 使用内存数据库进行 API 集成测试 |
| §6 Bogus 测试数据 | 用于生成逼真数据的 Faker 模式 |
| §7 TestContext 和日志记录 | 诊断输出、类别、超时 |
| §8 CI/CD 集成 | 使用覆盖率报告和阈值的 GitHub Actions |
| §9 调试表 | 12 个常见问题及其原因和解决方法 |
| §10 最佳实践 | 包含 14 项内容的生产检查清单 |
评论
支持 Markdown