将失败屏幕截图添加到Nunit测试中

本文关键字:Nunit 测试 添加 失败 屏幕截图 | 更新日期: 2023-09-27 18:20:14

我有一个Nunit测试,它正在进行一些自动化的UI测试。

我知道使用MSTest,你可以在结果中添加屏幕截图(请参阅MSTest中的测试报告附加图像)

努尼特有类似的东西吗?我可以在测试结果中添加图片吗?我找了一些,但找不到类似的东西。

将失败屏幕截图添加到Nunit测试中

我想您正在寻找NUnit的TestContext.AddTestAttachment(文件)

所以你可以在Selenium和NUnit:中做这样的事情

// Get the screenshot from Selenium WebDriver and save it to a file
Screenshot screenshot = driver.GetScreenshot();
string screenshotFile = Path.Combine(TestContext.CurrentContext.WorkDirectory,
    "my_screenshot.png");
screenshot.SaveAsFile(screenshotFile, ScreenshotImageFormat.Png);
// Add that file to NUnit results
TestContext.AddTestAttachment(screenshotFile, "My Screenshot");

如果您准备改变编写测试的方式,一个可行的解决方案是为测试逻辑创建一个执行包装器。这个包装器通过在控制流返回NUnit运行程序之前进行屏幕截图来处理异常。

有关更多详细信息和代码示例,请参阅此解决方案。