OneTimeTearDown 无法使用 selenium WebDriver

OneTimeTearDown is not working using selenium WebDriver

这段代码应该在测试失败时截图:

 [TestClass]
public class UnitTest1
{
    [OneTimeTearDown]
    public void TestFail()
    {
        IWebDriver driver = new ChromeDriver();
        if (NUnit.Framework.TestContext.CurrentContext.Result.Outcome != ResultState.Success)
        {
            string screensLocation = @"D:\";
            string testName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
            var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
            screenshot.SaveAsFile(screensLocation + testName + ".png");
        }
    }
    [TestMethod]
    public void TestMethod1()
    {
        // my code, here test is failed
    }
}

但它不起作用。我在 D:\ 位置没有任何屏幕 否则有没有办法在 OneTimeTearDown 属性下调试代码?因为当测试失败时,调试结束,我不知道方法 TestFail() 中发生了什么。 感谢您的帮助。

OneTimeTearDownAttribute 是 NUnit 的一个特性。

虽然您的代码显示 "nunit",但您的代码实际上并未使用它。 TestClassAttributeTestMethodAttribute 是 MS 测试的功能。如果您尝试 运行 使用 NUnit 进行此测试,它根本无法识别这些测试。

显然,您的测试程序集确实引用了 NUnit 框架,因为它不会以其他方式编译。

所以...归根结底,您的测试代码引用了两个不同的框架,因此 运行 任何一个 运行 都无法成功!!!您必须选择要使用的两者中的哪一个,删除另一个引用并为您选择保留的框架使用 运行ner。