佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫(kù) > mstest實(shí)現(xiàn)類似單元測(cè)試nunit中assert.throws功能

mstest實(shí)現(xiàn)類似單元測(cè)試nunit中assert.throws功能

熱門標(biāo)簽:國(guó)美全國(guó)運(yùn)營(yíng)中心 電銷業(yè)務(wù) 客戶服務(wù) 電商新玩法 科大訊飛語音識(shí)別系統(tǒng) 網(wǎng)站排名優(yōu)化 百度AI接口 人工智能

我們做單元測(cè)試NUnit中,有一個(gè)斷言Assert.Throws很好用,但當(dāng)我們使用MsTest時(shí)你需要這樣寫:

復(fù)制代碼 代碼如下:

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

現(xiàn)在讓我們來擴(kuò)展一下也實(shí)現(xiàn)類似成功能,增加一個(gè)類,代碼如下:

復(fù)制代碼 代碼如下:

/// 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)));
}
}

好了,現(xiàn)在我們?cè)贛sTest中可以這樣了,看下面代碼:
復(fù)制代碼 代碼如下:

[TestMethod]
 public void WriteToTextFile2()
{
//Implement Assert.Throws in MSTest
ExceptionAssert.ThrowsArgumentNullException>(()=> PDFUtility.WriteToTextFile("D:\\ACA.pdf", null)
 ,"Output file path should not be null");
 }
 

標(biāo)簽:南平 廈門 攀枝花 拉薩 棗莊 咸寧 益陽 POS機(jī)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mstest實(shí)現(xiàn)類似單元測(cè)試nunit中assert.throws功能》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    万州区| 凉山| 湘潭县| 无极县| 金秀| 忻城县| 讷河市| 惠东县| 内丘县| 建水县| 昌邑市| 怀安县| 万安县| 梁河县| 桓仁| 镇安县| 镇原县| 绥中县| 双辽市| 宁阳县| 法库县| 兴化市| 桃园市| 阿克苏市| 千阳县| 镇远县| 蛟河市| 临安市| 汉沽区| 礼泉县| 龙门县| 新河县| 肥城市| 青铜峡市| 鹰潭市| 舟曲县| 邵武市| 清远市| 赤水市| 大渡口区| 阿鲁科尔沁旗|