我正在接管C#中的遺留代碼,並且單元此代碼的測試不起作用。應該在沒有TestMethod屬性的方法中使用Assert嗎?
讓我來解釋下面的代碼。
[TestMethod]
public void SampleTest() {
...
// Parallel call of this method via Threading
TestUtil(args);
...
}
private void TestUtil(...) {
...
Assert.IsTrue(Condition1);
Assert.IsTrue(Condition2);
}
當我在Visual Studio中運行SampleTest,而不是標誌着測試爲失敗(因爲一些條件失敗),VS拋出QTAgent.exe異常。
你知道爲什麼QTAgent.exe在這種情況下崩潰,而不是失敗的測試?
如果你調用的函數是單元測試的一部分,那麼你不應該使用'TestMethod'屬性。那些通常被測試自動化觸發來運行所有的單元測試,你不希望TestUtil單獨運行,所以不要,不要使用'[TestMethod]'屬性。在調用私有函數的Test方法範圍內繼續使用Assert是很好的。 –