简体   繁体   中英

Take screenshot in Nunit Multiple Asserts

I want to take screenshot if a assert condition fails which is inside Assert.Multiple.

Eg

[Test]
public void TestScreenshot()
{
    Assert.Multiple(() =>
    {
        Assert.IsTrue(false);
        Assert.Istrue(true);
    });
}

In the above test,let say the first assertion is failure.So i want to take screenshot after the first assertion. But Assert.Multiple will return the assertions once all the steps executed within the multiple condition.

So, Is there anyway to take screenshot after a condition passes/fails which is added in the Multiple condition.

Unfortunately, what you want to do is not supported. However, in the case of failure, the following may work.

Assert.IsTrue(false, () => TakeScreenShotAndReturnMessage("Your error message"));
...
private string TakeScreenShotAndReturnMessage(string msg)
{
    // Code here to take the screen shot

    TestContext.AddTestAttachment(pathToScreenShot);

    return msg;
}

A few warnings:

  1. Hacks don't get any hackier than this. The code abuses a feature that is intended for generating error messages dynamically.

  2. It's pure "forum code" and will probably require changing. If you do, feel free to edit this if you're able. Depending on your version of NUnit you may need to use a delegate rather than a lambda.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM