mstest实现类似单元测试nunit中assert.throws功能
实现 功能 类似 单元测试 assert throws
2023-06-13 09:15:16 时间
我们做单元测试NUnit中,有一个断言Assert.Throws很好用,但当我们使用MsTest时你需要这样写:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
publicvoidWriteToTextFile()
{
PDFUtility.WriteToTextFile("D:\\ACA.pdf",null);
}
现在让我们来扩展一下也实现类似成功能,增加一个类,代码如下:
///<summary>
///Usefulassertionsforactionsthatareexpectedtothrowanexception.
///</summary>
publicstaticclassExceptionAssert
{
///<summary>
///Executesanexception,expectinganexceptiontobethrown.
///LikeAssert.ThrowsinNUnit.
///</summary>
///<paramname="action">Theactiontoexecute</param>
///<returns>Theexceptionthrownbytheaction</returns>
publicstaticExceptionThrows(Actionaction)
{
returnThrows(action,null);
}
///<summary>
///Executesanexception,expectinganexceptiontobethrown.
///LikeAssert.ThrowsinNUnit.
///</summary>
///<paramname="action">Theactiontoexecute</param>
///<paramname="message">Theerrormessageiftheexpectedexceptionisnotthrown</param>
///<returns>Theexceptionthrownbytheaction</returns>
publicstaticExceptionThrows(Actionaction,stringmessage)
{
try
{
action();
}
catch(Exceptionex)
{
//Theactionmethodhasthrowntheexpectedexception.
//Returntheexception,incasetheunittestwantstoperformfurtherassertionsonit.
returnex;
}
//Ifweenduphere,theexpectedexceptionwasnotthrown.Fail!
thrownewAssertFailedException(message??"Expectedexceptionwasnotthrown.");
}
///<summary>
///Executesanexception,expectinganexceptionofaspecifictypetobethrown.
///LikeAssert.ThrowsinNUnit.
///</summary>
///<paramname="action">Theactiontoexecute</param>
///<returns>Theexceptionthrownbytheaction</returns>
publicstaticTThrows<T>(Actionaction)whereT:Exception
{
returnThrows<T>(action,null);
}
///<summary>
///Executesanexception,expectinganexceptionofaspecifictypetobethrown.
///LikeAssert.ThrowsinNUnit.
///</summary>
///<paramname="action">Theactiontoexecute</param>
///<paramname="message">Theerrormessageiftheexpectedexceptionisnotthrown</param>
///<returns>Theexceptionthrownbytheaction</returns>
publicstaticTThrows<T>(Actionaction,stringmessage)whereT:Exception
{
try
{
action();
}
catch(Exceptionex)
{
Tactual=exasT;
if(actual==null)
{
thrownewAssertFailedException(message??String.Format("Expectedexceptionoftype{0}notthrown.Actualexceptiontypewas{1}.",typeof(T),ex.GetType()));
}
//Theactionmethodhasthrowntheexpectedexceptionoftype"T".
//Returntheexception,incasetheunittestwantstoperformfurtherassertionsonit.
returnactual;
}
//Ifweenduphere,theexpectedexceptionoftype"T"wasnotthrown.Fail!
thrownewAssertFailedException(message??String.Format("Expectedexceptionoftype{0}notthrown.",typeof(T)));
}
}
好了,现在我们在MsTest中可以这样了,看下面代码:
[TestMethod]
publicvoidWriteToTextFile2()
{
//ImplementAssert.ThrowsinMSTest
ExceptionAssert.Throws<ArgumentNullException>(()=>PDFUtility.WriteToTextFile("D:\\ACA.pdf",null)
,"Outputfilepathshouldnotbenull");
}
相关文章
- EasyPlayer实现视频的局部缩放、平移功能(类似快手视频)「建议收藏」
- Redis实现关系型数据库技术研究(redis关系型数据库)
- java中由类名和方法名字符串实现其调用【反射机制】详解编程语言
- Oracle JSP实现分页功能(oraclejsp分页)
- MSSQL中的触发器实现数据库自动化功能(mssql触发器)
- 复制MySQL双主复制实现数据库高可用(mysql双主)
- p.netMSSQL数据库搭配ASP.NET实现强大的网站功能(mssqlas)
- 完美定时:Linux 实现精确控制(linux精确定时)
- 处理Java Redis实现数据过期管理(redisjava过期)
- 使用Oracle实现获取天气信息功能(oracle获取天)
- Mysql中使用分段函数实现分组统计功能(mysql分段函数)
- javascript实现划词标记划词搜索功能
- jQuery点击图片跳转上一张或下一张功能的实现代码
- 用JQuery在网页中实现分隔条功能的代码
- javascript全选与全取消功能的实现代码
- android开发基础教程—文件存储功能实现
- vb中的MD5加密在asp.net中的实现
- 用JavaScript实现类似于ListBox功能示例代码
- PHP+jquery+ajax实现即时聊天功能实例
- javascript实现滑动解锁功能
- jquery实现类似淘宝星星评分功能实例