4

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.

Community
  • 1
  • 1
undefined
  • 33,537
  • 22
  • 129
  • 198
  • Not with NUnit, no. What are you using for the UI Automation? Selenium? Selenium does have support for screenshots if you've gone that route. – Bryan Ray Nov 03 '15 at 03:39
  • I am using selenium, and i know how to take the screenshots, what im trying to do is to associate those back to the testresults – undefined Nov 04 '15 at 03:56

2 Answers2

5

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");
0

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.

Community
  • 1
  • 1
rogersillito
  • 869
  • 1
  • 10
  • 27
  • Hey this doesn't really relate to my question. I know how to take screenshots. What i cant do is embed them in the test results. – undefined Apr 23 '16 at 04:31
  • Ok, apologies for the misunderstanding. If you are trying to actually view the screenshots within the NUnit application itself following a test run, I'm not able to offer a solution. The solution I linked to could however be used to generate log output (potentially in html form) which associates the screenshot file with each test result. This is what I do for use on a build server, and it would work running locally too - you could even script your build tool to open this for you after a test run. – rogersillito Apr 23 '16 at 17:57