当前位置:  编程技术>.net/c#/asp.net

mstest实现类似单元测试nunit中assert.throws功能

    来源: 互联网  发布时间:2014-08-25

    本文导语:  我们做单元测试NUnit中,有一个断言Assert.Throws很好用,但当我们使用MsTest时你需要这样写: 代码如下:[TestMethod][ExpectedException(typeof(ArgumentNullException))]public void WriteToTextFile(){PDFUtility.WriteToTextFile("D:\ACA.pdf", null);} 现在让我们来扩展一...

我们做单元测试NUnit中,有一个断言Assert.Throws很好用,但当我们使用MsTest时你需要这样写:

代码如下:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void WriteToTextFile()
{
PDFUtility.WriteToTextFile("D:\ACA.pdf", null);
}

现在让我们来扩展一下也实现类似成功能,增加一个类,代码如下:

代码如下:

///
/// Useful assertions for actions that are expected to throw an exception.
///
public static class ExceptionAssert
{
///
/// Executes an exception, expecting an exception to be thrown.
/// Like Assert.Throws in NUnit.
///
/// The action to execute
/// The exception thrown by the action
public static Exception Throws(Action action)
{
return Throws(action, null);
}

///
/// Executes an exception, expecting an exception to be thrown.
/// Like Assert.Throws in NUnit.
///
/// The action to execute
/// The error message if the expected exception is not thrown
/// The exception thrown by the action
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.");
}

///
/// Executes an exception, expecting an exception of a specific type to be thrown.
/// Like Assert.Throws in NUnit.
///
/// The action to execute
/// The exception thrown by the action
public static T Throws(Action action) where T : Exception
{
return Throws(action, null);
}

///
/// Executes an exception, expecting an exception of a specific type to be thrown.
/// Like Assert.Throws in NUnit.
///
/// The action to execute
/// The error message if the expected exception is not thrown
/// The exception thrown by the action
public static T Throws(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(()=> PDFUtility.WriteToTextFile("D:\ACA.pdf", null)
 ,"Output file path should not be null");
 }
 

    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3