undefined
undefined

Reputation: 34289

Add a failure screenshot to an Nunit test

I have an Nunit test which is doing some automated UI testing.

I know with MSTest you can add a screenshot into the results (see Attach an image to a test report in MSTest)

Is there something similar in nunit? Can I add a picture into the test results somehow? I've done a bit of looking but couldn't find anything similar.

Upvotes: 5

Views: 7206

Answers (2)

Christopher Wood
Christopher Wood

Reputation: 141

I think you are looking for NUnit's TestContext.AddTestAttachment(file)

So you could do something like this in Selenium and 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");

Upvotes: 6

rogersillito
rogersillito

Reputation: 941

If you're prepared to alter the way you write your tests, a workable solution is to create an execution wrapper for your test logic. This wrapper handles exceptions by taking a screenshot before control flow returns to the NUnit runner.

See this solution for more details and code samples.

Upvotes: 0

Related Questions