mstest实现类似单元测试nunit中assert.throws功能
更新时间:2014年01月22日 14:10:24 作者:
我们做单元测试NUnit中,有一个断言Assert.Throws很好用,现在我们来扩展一下也实现类似成功能,大家参考使用吧
我们做单元测试NUnit中,有一个断言Assert.Throws很好用,但当我们使用MsTest时你需要这样写:
复制代码 代码如下:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void WriteToTextFile()
{
PDFUtility.WriteToTextFile("D:\\ACA.pdf", null);
}
现在让我们来扩展一下也实现类似成功能,增加一个类,代码如下:
复制代码 代码如下:
/// <summary>
/// Useful assertions for actions that are expected to throw an exception.
/// </summary>
public static class ExceptionAssert
{
/// <summary>
/// Executes an exception, expecting an exception to be thrown.
/// Like Assert.Throws in NUnit.
/// </summary>
/// <param name="action">The action to execute</param>
/// <returns>The exception thrown by the action</returns>
public static Exception Throws(Action action)
{
return Throws(action, null);
}
/// <summary>
/// Executes an exception, expecting an exception to be thrown.
/// Like Assert.Throws in NUnit.
/// </summary>
/// <param name="action">The action to execute</param>
/// <param name="message">The error message if the expected exception is not thrown</param>
/// <returns>The exception thrown by the action</returns>
public static Exception Throws(Action action, string message)
{
try
{
action();
}
catch (Exception ex)
{
// The action method has thrown the expected exception.
// Return the exception, in case the unit test wants to perform further assertions on it.
return ex;
}
// If we end up here, the expected exception was not thrown. Fail!
throw new AssertFailedException(message ?? "Expected exception was not thrown.");
}
/// <summary>
/// Executes an exception, expecting an exception of a specific type to be thrown.
/// Like Assert.Throws in NUnit.
/// </summary>
/// <param name="action">The action to execute</param>
/// <returns>The exception thrown by the action</returns>
public static T Throws<T>(Action action) where T : Exception
{
return Throws<T>(action, null);
}
/// <summary>
/// Executes an exception, expecting an exception of a specific type to be thrown.
/// Like Assert.Throws in NUnit.
/// </summary>
/// <param name="action">The action to execute</param>
/// <param name="message">The error message if the expected exception is not thrown</param>
/// <returns>The exception thrown by the action</returns>
public static T Throws<T>(Action action, string message) where T : Exception
{
try
{
action();
}
catch (Exception ex)
{
T actual = ex as T;
if (actual == null)
{
throw new AssertFailedException(message ?? String.Format("Expected exception of type {0} not thrown. Actual exception type was {1}.", typeof(T), ex.GetType()));
}
// The action method has thrown the expected exception of type 'T'.
// Return the exception, in case the unit test wants to perform further assertions on it.
return actual;
}
// If we end up here, the expected exception of type 'T' was not thrown. Fail!
throw new AssertFailedException(message ?? String.Format("Expected exception of type {0} not thrown.", typeof(T)));
}
}
好了,现在我们在MsTest中可以这样了,看下面代码:
复制代码 代码如下:
[TestMethod]
public void WriteToTextFile2()
{
//Implement Assert.Throws in MSTest
ExceptionAssert.Throws<ArgumentNullException>(()=> PDFUtility.WriteToTextFile("D:\\ACA.pdf", null)
,"Output file path should not be null");
}
相关文章
asp.net 程序性能优化的七个方面 (c#(或vb.net)程序改进)
在我们开发asp.net过程中,需要注意的一些细节,以达到我们优化程序执行效率。2009-03-03ASP.NET Core应用错误处理之ExceptionHandlerMiddleware中间件呈现“定制化错误页面”
这篇文章主要给大家介绍了关于ASP.NET Core应用错误处理之ExceptionHandlerMiddleware中间件呈现“定制化错误页面”的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧2019-01-01比较简单的将数据信息导入wrod文档方案(C# for word)
史上最简单将数据信息导入wrod文档方案(C# for word)2010-01-01Entity Framework Core延迟加载(懒加载)用法
这篇文章介绍了Entity Framework Core延迟加载(懒加载)的使用方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02asp.net继承IHttpHandler接口实现给网站图片添加水印功能实例
这篇文章主要介绍了asp.net继承IHttpHandler接口实现给网站图片添加水印功能,实例分析了asp.net基于IHttpHandler接口实现网站图片水印功能的具体步骤与相关技巧,需要的朋友可以参考下2016-07-07
最新评论