使用 Assert 命名空间的 Microsoft.VisualStudio.TestTools.UnitTesting 类来验证特定功能。 测试方法运行应用程序中的代码,但只有在包含 Assert 语句时才报告正确性。
概述
MSTest 提供三个断言类:
| Class | 目的 |
|---|---|
Assert |
通用的值、类型和异常断言。 |
StringAssert |
针对字符串的模式、子字符串和比较的断言。 |
CollectionAssert |
用于比较和验证集合的集合断言。 |
小窍门
当Assert和StringAssert/CollectionAssert都存在功能时,首选Assert类。 该 Assert 类提供更好的可发现性,是编写新代码的推荐选择。
StringAssert 和 CollectionAssert 被保持以实现向后兼容性。
所有断言方法都接受一个可选消息参数,该参数在断言失败时显示,有助于确定原因:
Assert.AreEqual(expected, actual, "Values should match after processing");
Assert 类
使用 Assert 类验证所测试的代码是否按预期方式运行。
常见断言方法
[TestMethod]
public async Task AssertExamples()
{
// Equality
Assert.AreEqual(5, calculator.Add(2, 3));
Assert.AreNotEqual(0, result);
// Reference equality
Assert.AreSame(expected, actual);
Assert.AreNotSame(obj1, obj2);
// Boolean conditions
Assert.IsTrue(result > 0);
Assert.IsFalse(string.IsNullOrEmpty(name));
// Null checks
Assert.IsNull(optionalValue);
Assert.IsNotNull(requiredValue);
// Type checks
Assert.IsInstanceOfType<IDisposable>(obj);
Assert.IsNotInstanceOfType<string>(obj);
// Exception testing (MSTest v3.8+)
Assert.ThrowsExactly<ArgumentNullException>(() => service.Process(null!));
await Assert.ThrowsExactlyAsync<InvalidOperationException>(
async () => await service.ProcessAsync());
}
可用 API
- Assert.AreEqual
- Assert.AreNotEqual
- Assert.AreNotSame
- Assert.AreSame
- Assert.Contains
- Assert.ContainsSingle
- Assert.DoesNotContain
- Assert.DoesNotEndWith
- Assert.DoesNotMatchRegex
- Assert.DoesNotStartWith
- Assert.Fail
- Assert.HasCount
- Assert.Inconclusive
- Assert.IsEmpty
- Assert.IsFalse
- Assert.IsGreaterThan
- Assert.IsGreaterThanOrEqualTo
- Assert.IsInRange
- Assert.IsInstanceOfType
- Assert.IsLessThan
- Assert.IsLessThanOrEqualTo
- Assert.IsNegative
- Assert.IsNotEmpty
- Assert.IsNotInstanceOfType
- Assert.IsNotNull
- Assert.IsNull
- Assert.IsPositive
- Assert.IsTrue
- Assert.MatchesRegex
- Assert.StartsWith
- Assert.Throws
- Assert.ThrowsAsync
- Assert.ThrowsExactly
- Assert.ThrowsExactlyAsync
StringAssert 类
使用 StringAssert 类来比较和检查字符串。
注释
所有 StringAssert 方法在类中 Assert 都具有等效项。 首选Assert方法以实现更好的发现性。 为了向后兼容,保留了该 StringAssert 类。
可用的 API 包括:
- StringAssert.Contains
- StringAssert.DoesNotMatch
- StringAssert.EndsWith
- StringAssert.Matches
- StringAssert.StartsWith
CollectionAssert 类
使用 CollectionAssert 类比较对象的集合,并验证集合的状态。
注释
当 Assert 类中存在等效的方法(例如 Assert.Contains,Assert.DoesNotContain)时,建议优先使用 Assert 以提高可发现性。 该 CollectionAssert 类主要用于向后兼容性。
可用的 API 包括:
- CollectionAssert.AllItemsAreInstancesOfType
- CollectionAssert.AllItemsAreNotNull
- CollectionAssert.AllItemsAreUnique
- CollectionAssert.AreEqual
- CollectionAssert.AreEquivalent
- CollectionAssert.AreNotEqual
- CollectionAssert.AreNotEquivalent
- CollectionAssert.Contains
- CollectionAssert.DoesNotContain
- CollectionAssert.IsNotSubsetOf
- CollectionAssert.IsSubsetOf
最佳做法
使用特定断言:优先使用
AreEqual而不是IsTrue(a == b),以获得更好的故障消息。包括描述性消息:帮助使用明确的断言消息快速识别故障。
一次测试一件事:每个测试方法都应验证单个行为。
使用
Throws/ThrowsExactly来处理异常:在 MSTest v3.8+ 中,优先使用Assert.ThrowsAssert.ThrowsExactly及其异步对应的ThrowsAsyncThrowsExactlyAsync,而不是ExpectedException属性。首选
Assert:StringAssert/CollectionAssert当这两个类中都存在功能时,请使用Assert类,以提高可发现性和一致性。
相关分析器
以下分析器有助于确保正确使用断言:
-
MSTEST0006 - 避免
ExpectedException属性,改用Assert.Throws方法。 - MSTEST0017 - 断言参数应按正确的顺序传递。
- MSTEST0023 - 不要否定布尔断言。
-
MSTEST0025 - 优先使用
Assert.Fail而不是总是为假条件。 - MSTEST0026 - 断言参数应避免条件访问。
- MSTEST0032 - 查看总为真的断言条件。
- MSTEST0037 - 使用正确的断言方法。
-
MSTEST0038 - 避免
Assert.AreSame与值类型一起使用。 -
MSTEST0039 - 使用较
Assert.Throws新的方法。 - MSTEST0040 - 避免在异步 void 上下文中使用断言。
-
MSTEST0046 - 使用
Assert而不是StringAssert。 -
-
Assert.ThrowsMSTEST0051应包含单个语句。 -
MSTEST0053 - 避免
Assert格式参数。 - MSTEST0058 - 避免在 catch 块中断言。