我正在使用MSTest Assert.ThrowsException
,但有时我想在抛出的异常对象上添加测试条件,例如检查消息是否提到特定短语,标识符,数据值或其他内容。
我发现的最佳解决方案是不使用Assert.ThrowsException
的结构,但是使用try...catch
:
bool success = false;
try
{
// Code to be tested goes here...
success = true;
}
catch (Exception e)
{
Assert.IsInstanceOfType(e, typeof(DesiredExceptionClass));
// Tests on e go here...
}
if (success)
Assert.Fail($"Should have thrown {nameof(DesiredExceptionClass)}.");
有没有更好的办法?
“更好”是指更具可读性,更紧凑的代码,更少的“样板”。
我正在使用这种方式:
Exception exception = null;
try
{
// Code to be tested goes here...
}
catch (Exception e)
{
exception = e;
}
Assert.IsInstanceOfType(exception, typeof(DesiredExceptionClass));
当应该提出异常时,有一个pattern I have post in the past来验证额外的期望。
您似乎正在尝试减少“Boilerplate代码”,因此我创建了以下帮助程序类:
public static class ExceptionAssert
{
/// <summary>
/// Use this method if your UT is using <see cref="ExpectedExceptionAttribute"/> and you want to verify additional expectations
/// </summary>
/// <typeparam name="T">The expected exception type base</typeparam>
/// <param name="action">Execute the unit to test</param>
/// <param name="verifier">Verify the additional expectations</param>
public static void AssertException<T>(Action action, Action<T> verifier) where T: Exception
{
try
{
action();
}
catch(T e)
{
verifier(e);
throw;
}
}
/// <summary>
/// Use this method if your UT is not using <see cref="ExpectedExceptionAttribute"/> and you want to verify additional expectations
/// </summary>
/// <typeparam name="T">The expected exception type base</typeparam>
/// <param name="action">Execute the unit to test</param>
/// <param name="verifier">Verify the additional expectations</param>
/// <param name="allowDriven">Indicates if the raised exception can be an instance of driven class</param>
public static void AssertExceptionWithoutExcepctedExceptionAttribute<T>(Action action, Action<T> verifier, bool allowDriven = true) where T : Exception
{
try
{
action();
Assert.Fail("No Exception raised");
}
catch (T e)
{
if (!allowDriven && e.GetType() != typeof(T))
{
Assert.Fail($"The raised exception :: {e.GetType()} is a driven instance of :: {typeof(T)}");
}
verifier(e);
}
}
}
现在您可以执行以下某个UT:
[TestMethod]
[ExpectedException(typeof(Exception), AllowDerivedTypes = true)] // change the attribute settings
public void Foo()
{
// do arrange:
ExceptionAssert.AssertException<Exception>(() => // change "Exception" to the required exception type or left it as any exception
{
// do the act:
}, exception =>
{
// do you asserts statements:
});
}
[TestMethod]
public void FooBar()
{
// do arrange:
ExceptionAssert.AssertExceptionWithoutExcepctedExceptionAttribute<Exception>(() => // change "Exception" to the required exception type or left it as any exception
{
// do the act:
}, exception =>
{
// do you asserts statements:
}, false);
}
BTW,IMO除了他的类型/驱动之外你永远不应该验证异常本身(或者如果它是带有附加信息的自定义异常),因为你的代码永远不会依赖于他的MSG,调用堆栈等......我认为这是MS之所以没有增加 ExpectedExceptionAttribute验证MSG的能力。