将故障屏幕截图添加到Nunit测试

时间:2015-11-03 02:21:51

标签: c# nunit

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

我知道使用MSTest,您可以在结果中添加屏幕截图(请参阅Attach an image to a test report in MSTest

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

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找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");

答案 1 :(得分:0)

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

See this solution for more details and code samples