华域联盟 .Net mstest实现类似单元测试nunit中assert.throws功能

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

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

 }

 

本文由 华域联盟 原创撰写:华域联盟 » mstest实现类似单元测试nunit中assert.throws功能

转载请保留出处和原文链接:https://www.cnhackhy.com/53426.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部