没有合适的资源?快使用搜索试试~ 我知道了~
mstest实现类似单元测试nunit中assert.throws功能
0 下载量 11 浏览量
2021-01-02
07:18:20
上传
评论
收藏 26KB PDF 举报
温馨提示
我们做单元测试NUnit中,有一个断言Assert.Throws很好用,但当我们使用MsTest时你需要这样写: 代码如下:[TestMethod][ExpectedException(typeof(ArgumentNullException))]public void WriteToTextFile(){PDFUtility.WriteToTextFile(“D:\\ACA.pdf”, null);} 现在让我们来扩展一下也实现类似成功能,增加一个类,代码如下: 代码如下:/// <summary>/// Useful assertions for actions that are expe
资源推荐
资源详情
资源评论
mstest实现类似单元测试实现类似单元测试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>
资源评论
weixin_38625416
- 粉丝: 5
- 资源: 920
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功