xUnit/MSTest 设置与断言
学习测试结构(Arrange-Act-Assert),了解断言如何使测试失败,并将这些理念对应到 MSTest/xUnit 特性。
xUnit/MSTest 设置与断言 是 CoddyKit 上的免费 C# Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。
什么是单元测试
目标:编写小型、可重复执行的单元测试。
- 独立的测试项目使用 MSTest 或 xUnit
- 每个测试检查一种行为
- 使用清晰的名称和断言
AAA 模式
测试遵循 Arrange → Act → Assert。这里使用简单检查来模拟断言。
using System;
public static class MathUtil
{
// Method under test
public static int Add(int a, int b)
{
return a + b;
}
}
public class Program
{
public static void Main(string[] args)
{
// Arrange
int a = 2;
int b = 3;
int expected = 5;
// Act
int actual = MathUtil.Add(a, b);
// Assert (manual check for this console demo)
if (actual == expected)
Console.WriteLine("PASS: Add returned " + actual);
else
Console.WriteLine("FAIL: expected " + expected + " but got " + actual);
}
}
断言行为
断言不匹配时会抛出异常。真实的框架工作方式类似(接口更丰富,报告更完善)。
using System;
public static class Assert
{
public static void AreEqual(int expected, int actual)
{
if (expected != actual)
throw new Exception("AreEqual failed: expected " + expected + ", actual " + actual);
}
public static void IsTrue(bool condition, string message)
{
if (!condition) throw new Exception("IsTrue failed: " + message);
}
}
public static class MathUtil
{
public static int Mul(int a, int b) { return a * b; }
}
public class Program
{
public static void Main(string[] args)
{
try
{
int r = MathUtil.Mul(2, 4);
Assert.AreEqual(8, r); // pass
Assert.IsTrue(r % 2 == 0, "even"); // pass
Console.WriteLine("All assertions passed.");
}
catch (Exception ex)
{
Console.WriteLine("TEST FAILED: " + ex.Message);
}
}
}
测试通过示例
每个测试检查一种行为。在真实的 MSTest/xUnit 中,失败的 Assert 会将测试标记为失败。
using System;
public static class StringUtil
{
public static string Repeat(string s, int n)
{
if (n < 0) throw new ArgumentOutOfRangeException("n");
string r = "";
for (int i = 0; i < n; i++) r += s;
return r;
}
}
public static class Assert
{
public static void AreEqual(string expected, string actual)
{
if (expected != actual)
throw new Exception("AreEqual failed: expected [" + expected + "], actual [" + actual + "]");
}
}
public class Program
{
static void TestRepeat_Happy()
{
string got = StringUtil.Repeat("ab", 3);
Assert.AreEqual("ababab", got); // pass
}
static void TestRepeat_EmptyTimesZero()
{
string got = StringUtil.Repeat("x", 0);
Assert.AreEqual("", got); // pass
}
public static void Main(string[] args)
{
try { TestRepeat_Happy(); Console.WriteLine("PASS: Happy"); } catch (Exception ex) { Console.WriteLine("FAIL: Happy -> " + ex.Message); }
try { TestRepeat_EmptyTimesZero(); Console.WriteLine("PASS: Zero"); } catch (Exception ex) { Console.WriteLine("FAIL: Zero -> " + ex.Message); }
}
}
MSTest 与 xUnit 的对应关系
MSTest 基础:
- 在类上使用 [TestClass]
- 在每个测试上使用 [TestMethod]
- Assert.AreEqual/IsTrue/AreNotEqual…
xUnit 基础:
- 对测试方法使用 [Fact]
- Assert.Equal/True/False…
- 不需要测试类特性
命名与作用域提示
提示:
- 使用 Method_Scenario_ExpectedResult 这样的方式命名测试
- 每个测试使用一个 assert,或只检查一种行为
- 让测试保持小巧且相互独立
断言会导致失败
回顾
回顾:使用 AAA 模式,通过断言比较预期值与实际值,并映射到 MSTest([TestMethod])或 xUnit([Fact])。
常见问题解答
「xUnit/MSTest 设置与断言」课时是免费的吗?
是的 — 「xUnit/MSTest 设置与断言」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。
「xUnit/MSTest 设置与断言」这节课中我会学到什么?
学习测试结构(Arrange-Act-Assert),了解断言如何使测试失败,并将这些理念对应到 MSTest/xUnit 特性。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。
「xUnit/MSTest 设置与断言」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- xUnit/MSTest 设置与断言
- 通过接口创建伪对象与测试数据构建器
- 调试失败的测试