We want to attach screenshots to the Test Attachment in Azure pipeline. Currently, we use
- .NET 4.5.2
- Selenium.WebDriver 3.141
- Selenium.Chrome.WebDriver
- Nunit 3.12.0
- Specflow.Nunit 2.4.0
It is similar with the follwing example but we use NUnit rather than MSTest https://learn.microsoft.com/en-us/azure/devops/pipelines/test/collect-screenshots-and-video?view=azure-devops#collect-screenshots-logs-and-attachments
When run the program in VS2017, the screenshots are accessible from the test report. Also, we can see the screenshots in the azure build output.
Here is the code:
string fileName = string.Format("Screenshot_" + DateTime.Now.ToString("dd-MM-yyyy-hhmm-ss") + ".jpg");
var artifactDirectory = Directory.GetCurrentDirectory();
ITakesScreenshot takesScreenshot = _driver as ITakesScreenshot;
if (takesScreenshot != null)
{
var screenshot = takesScreenshot.GetScreenshot();
string screenshotFilePath = Path.Combine(artifactDirectory, fileName);
screenshot.SaveAsFile(screenshotFilePath, ScreenshotImageFormat.Jpeg);
TestContext.AddTestAttachment(screenshotFilePath, "screenshot");
Console.WriteLine($"Screenshot: {new Uri(screenshotFilePath)}");
}
Visual Studio Test step in Azure pipeline
After the build runs, there is no attachment
Any help would be appreciated.



Coming in a bit late on this one, but who knows, this might be helpful to someone running into similar issues.
I cannot see your entire code, but the reason this might happen could be because you're trying to save your attachment in your
OneTimeSetuporOneTimeTeardownmethods. Quoting from the documentation:...which basically means that using
TestContextmethods or properties is not allowed withinOneTimeSetuporOneTimeTeardown(even though NUnit/Visual Studio won't complain if you do so! Which adds to the confusion)So make sure you add the test's attachments from within your
TearDownor test case. AFAIK, there is no way to save attachments fromTestFixturecontext.