mstest实现类似单元测试nunit中assert.throws功能
更新时间:2021年9月22日 10:12 点击:1448
我们做单元测试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");
}
相关文章
- 这篇文章主要介绍了解决@SpringBootTest 单元测试遇到的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-14
- 这篇文章主要介绍了JavaWeb实战之编写单元测试类测试数据库操作,文中有非常详细的代码示例,对正在学习javaweb的小伙伴们有很大的帮助,需要的朋友可以参考下...2021-04-22
- 这篇文章主要介绍了Springboot 使用具体化类和配置来缩短单元测试时间,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-11-05
- 这篇文章主要介绍了使用@SpringBootTest注解进行单元测试,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
聊聊springboot2.2.3升级到2.4.0单元测试的区别
这篇文章主要介绍了springboot 2.2.3 升级到 2.4.0单元测试的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-14- 这篇文章主要介绍了Springboot集成JUnit5优雅进行单元测试的示例,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下...2020-10-15
- 这篇文章主要介绍了vue 单元测试的相关资料,帮助大家更好的理解和学习使用vue框架,感兴趣的朋友可以了解下...2021-04-16
- 本篇文章给大家详细讲述了.NET单元测试的详细方法和步骤,有需要的朋友参考学习下。...2021-09-22
使用springboot单元测试对weblistener的加载测试
这篇文章主要介绍了使用springboot单元测试对weblistener的加载测试,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-14ASP.NET Core对Controller进行单元测试的完整步骤
这篇文章主要给大家介绍了关于ASP.NET Core对Controller进行单元测试的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2021-09-22- NUnit是.Net平台的测试框架,广泛用于.Net平台的单元测试和回归测试中,下面我们用示例详细学习一下他的使用方法...2020-06-25
Java单元测试Powermockito和Mockito使用总结
公司单元测试框架选用了Junit 4.12,Mock框架选用了Mockito和PowerMock,本文主要介绍了Java单元测试Powermockito和Mockito使用总结,感兴趣的可以了解一下...2021-09-08- 这篇文章主要介绍了浅谈.Net Core后端单元测试的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09
详解在SpringBoot中使用MongoDb做单元测试的代码
这篇文章主要介绍了详解在SpringBoot中使用MongoDb做单元测试的代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-06- 这篇文章主要介绍了SpringBootTest单元测试报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-18
- 这篇文章主要介绍了xUnit 编写 ASP.NET Core 单元测试的方法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2021-09-22
- 在本篇文章里小编给大家整理了一篇关于python unittest单元测试的步骤,对此有兴趣的朋友们可以跟着学习下。...2021-08-02
Spring使用RestTemplate和Junit单元测试的注意事项
这篇文章主要介绍了Spring使用RestTemplate和Junit单元测试的注意事项,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-28SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案
这篇文章主要介绍了SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-04- Pytest是Python的一种单元测试框架,与 Python 自带的 Unittest 测试框架类似,但是比 Unittest 框架使用起来更简洁,效率更高,今天给大家详细介绍一下pytest的使用,需要的朋友可以参考下...2021-06-07