【发布时间】:2019-10-07 04:43:33
【问题描述】:
使用 NUnit ir MeTest Assert 方法时如何捕获 Failed Assert 异常消息或状态。
当我的断言失败时,我试图捕获“AssertionException”,我如何使用 NUnit 或 MsTest 捕获它。因为 Assert 方法不返回任何类型。我的要求是即使断言未能完成剩余的断言,测试也应该继续,应该捕获错误并且应该使这个断言失败。我正在使用下面的代码语句。当我使用 Nunit 框架时,它的失败和持续到下一个断言即使我使用 try..catch 块也无法捕获,而在 MsTest 中它失败,使用 try..catch 块捕获并且不继续下一个断言。 非常感谢您的帮助!
public static void ResponseValueAssert(dynamic actualValue, dynamic expectedValue, string nameOfAssert)
{
//var ex = Assert.Throws<AssertionException>(() =>
//Assert.AreEqual(expectedValue, actualValue, "Actual value doesn't match with Expected value {0}", nameOfAssert));
if (ResponseValueAssertImplicit(actualValue, expectedValue, nameOfAssert))
{
Console.WriteLine("\r\nResponse Assert:- {0}: <PASS>", nameOfAssert);
}
else
{
Console.WriteLine("\r\nResponse Assert:- {0}: <<FAIL>>", nameOfAssert);
Console.Error.WriteLine("\r\nResponse Assert:- {0}: <<FAIL>>", nameOfAssert);
// Assert.Fail();
}
Console.WriteLine("Expected Value: {0}.\r\nActual Value: {1}.", actualValue, expectedValue);
}
public static bool ResponseValueAssertImplicit(dynamic actualValue, dynamic expectedValue, string nameOfAssert)
{
try
{
Assert.AreEqual(expectedValue, actualValue, "Actual value doesn't match with Expected value {0}", nameOfAssert);
return true;
}
catch (AssertionException ex)
{
return false;
}
}
【问题讨论】: