在MSTest中如何检查上次测试是否通过(在TestCleanup中)

Mar*_*ann 10 selenium mstest webdriver

我正在使用MSTest在Selenium中创建Web测试,并希望每次测试失败时都会截取屏幕截图,但我不希望每次测试都通过.

我想要做的是在[TestCleanup]方法中放置一个截图函数,如果测试失败则运行它,但如果测试通过则不运行.但是如何判断最后一次测试是否通过?

目前我正在做bool = false[TestInitialize]bool = true如果测试贯穿.

但我不认为这是一个非常好的解决方案.

所以基本上我正在寻找一种方法来检测上次测试是否为true/false [TestCleanup].

Mar*_*ann 11

if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
    // some code
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*ers 8

@MartinMussmann的答案是正确的,但不完整.要访问"TestContext"对象,您需要确保将其声明为TestClass中的属性:

[TestClass]
public class BaseTest
{
    public TestContext TestContext { get; set; }

    [TestCleanup]
    public void TestCleanup()
    {
        if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
        {
            // some code
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这也在以下文章中提到.