My *_* Me 2 attributes unit-testing exception
是否有一种简单的(属性驱动的)方法可以对异常消息进行以下测试失败.
[TestMethod()]
[ExpectedException(typeof(ArgumentException))]
public void ExceptionTestTest()
{
throw new ArgumentException("BBB");
}
Run Code Online (Sandbox Code Playgroud)
如果异常中的消息是BBB,我希望测试通过,但如果是其他任何内容则失败.我查看了ExpectedException属性的第二个参数,但如果Exception类型不同,那么这只是在测试报告中显示的消息.
我知道我可以尝试{}捕获{}异常然后断言消息IsEqual到消息,但这感觉很笨拙.
PS.我正在使用Visual Studio 2008的内置单元测试(专业版)
Visual Studio中的内置东西很差.看看NUnit.它的加载更加复杂,具有语法帮助程序,使特定Asserts更容易,可以作为调试或独立运行的一部分运行,具有控制台运行程序以及UI等.我已经使用它多年 - 这是业务.Exception消息选项提供了它的复杂程度:
public enum MessageMatch
{
/// Expect an exact match
Exact,
/// Expect a message containing the parameter string
Contains,
/// Match the regular expression provided as a parameter
Regex,
/// Expect a message starting with the parameter string
StartsWith
}
Run Code Online (Sandbox Code Playgroud)
不要被推迟:它很复杂,但并不复杂 - 它很容易上班.
| 归档时间: |
|
| 查看次数: |
247 次 |
| 最近记录: |