我想在C#硒中截取失敗的測試用例的截圖。如果Assert.AreEqual失敗,如何截圖?如何把條件與Assert.AreEqual?
但我不知道如何使用條件與Assert.AreEqual
。
我試過使用if(Assert.Equals == false)
也,但那是行不通的。
任何人都可以幫忙嗎?
我想在C#硒中截取失敗的測試用例的截圖。如果Assert.AreEqual失敗,如何截圖?如何把條件與Assert.AreEqual?
但我不知道如何使用條件與Assert.AreEqual
。
我試過使用if(Assert.Equals == false)
也,但那是行不通的。
任何人都可以幫忙嗎?
它有助於查看更多的實際測試,但由於Assert.Equals
沒有返回值,因此可以將其分解爲多個步驟。
舉例來說,如果你有這樣的:
Assert.AreEqual(value1, value2);
然後,你可以用這個代替它:其實
var areValuesEqual = (value1 == value2);
Assert.IsTrue(areValuesEqual);
if (!areValuesEqual)
{
// rest of testing logic
}
嗯, 「Asset.AreEqual」 接受三個參數 1.預期的結果in bool 2. bool的原始結果 3.錯誤信息
如果預期和原始不匹配我t會拋出一個錯誤,對於截圖你需要使用try-catch,我很久以前就用過了。
公共無效AreEqual(布爾預計,布爾結果,字符串評論= 「」,字符串pictureName = 「」){ 嘗試 { Assert.AreEqual(預期,因此,評論);
}
catch
{
/// will capture a screenshot of errors
if (string.IsNullOrEmpty(pictureName) && !string.IsNullOrEmpty(comment))
{
int length = comment.Replace(" ", string.Empty).Length;
if (length > 30)
length = 30;
pictureName = comment.Replace(" ", string.Empty).Substring(0, length);
}
pictureName = (pictureName == "" ? Guid.NewGuid().ToString() : pictureName);
GetScreenShot(pictureName);
// Getscreenshot function capture image for me u need to put your code here(before throw)
throw;
}
}