主页 > 知识库 > mstest实现类似单元测试nunit中assert.throws功能

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

热门标签:400电话办理信任翰诺科技 怎样给陕西地图标注颜色 ai电销机器人对贷款有帮助吗 云狐人工智能电话机器人 广州销售外呼系统定制 宿迁智能外呼系统排名 电销机器人 数据 福州人工智能电销机器人加盟 地图标注多少钱一张

我们做单元测试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 ThrowsT>(Action action) where T : Exception
{
return ThrowsT>(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 ThrowsT>(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.ThrowsArgumentNullException>(()=> PDFUtility.WriteToTextFile("D:\\ACA.pdf", null)
 ,"Output file path should not be null");
 }
 

标签:焦作 大兴安岭 绵阳 宜春 延安 新疆 黄南 曲靖

巨人网络通讯声明:本文标题《mstest实现类似单元测试nunit中assert.throws功能》,本文关键词  mstest,实现,类似,单元,测试,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《mstest实现类似单元测试nunit中assert.throws功能》相关的同类信息!
  • 本页收集关于mstest实现类似单元测试nunit中assert.throws功能的相关信息资讯供网民参考!
  • 推荐文章