欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-27 20:49:21
我们做单元测试nunit中,有一个断言assert.throws很好用,但当我们使用mstest时你需要这样写:复制代码 代码如下:[testmethod][expecte...

我们做单元测试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");
 }