向 Nunit 测试添加失败屏幕截图

Not*_*ple 5 c# nunit

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

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

nunit中有类似的东西吗?我可以以某种方式将图片添加到测试结果中吗?我做了一些查找,但找不到类似的东西。

小智 6

我认为您正在寻找 NUnit 的TestContext.AddTestAttachment(file)

所以你可以在 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");
Run Code Online (Sandbox Code Playgroud)


rog*_*ito 0

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

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