当前位置: 移动技术网 > IT编程>开发语言>.net > mstest实现类似单元测试nunit中assert.throws功能

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

春秋q传私服,advise的用法,儿女传奇之猎野人

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网