如何停止数据驱动的编码 UI 测试

本文关键字:UI 测试 编码 何停止 数据驱动 | 更新日期: 2023-09-27 18:31:44

谁能告诉我如何在数据驱动的编码 UI 测试的第一次失败时停止迭代? 我有数百次迭代,我想知道在完成之前失败了。 这意味着,我不想等待数小时才能知道第一次迭代失败了。 这可能吗?

谢谢

如何停止数据驱动的编码 UI 测试

我遇到了类似的问题,经过一些研究,我发现有两种可能的解决方案:1. 复杂的一个 - 通过实现 ITestMethodInvoker 接口,使用您自己的自定义数据适配器创建自己的测试调度程序。在那里,您可以按照自己希望的方式运行测试迭代。您还需要重写TestContex类,以便使用数据适配器中的数据行向测试方法提供数据行。2.最简单的一个 - 这实际上不会阻止Visual Studio遍历数据源中的所有DataRows,但不是执行实际的测试方法,Visual Studio会将其标记为跳过并继续。下面是示例代码:

[CodedUITest]
public class MyTestClass
{
    private TestContext testContext;
    private static int countFailedIterations = 0;
    private static string currentMethod = "";
    public MyTestClass()
    {
        if (!currentMethod.Equals(testContext.FullyQualifiedTestClassName))
        {
            // Reset Iteration Count
            countFailedIterations = 0;
            currentMethod = testContext.FullyQualifiedTestClassName;
        }
    }
    [TestInitialize]
    public void MyTestInit()
    {
        if (countFailedIterations > 0)
            Assert.Inconclusive("Stop it, please");
    }
    [TestCleanup]
    public void MytestCleanup()
    {
        if (testContext.CurrentTestOutcome != UnitTestOutcome.Passed)
            countFailedIterations++;
    }
    [TestMethod]
    [DataSource("MyDataSource")]
    public void MyTestMethod()
    {
        //Blah Blah Blah
    }
    public TestContext TestContext
    {
        get { return testContext; }
        set { testContext = value; }
    }
}

仅当您有多个数据驱动的测试方法(因此想要重置失败的迭代计数器)时,才需要构造函数中的代码。但你是对的 - 当构造函数运行时,TestContext 仍然没有初始化。您实际上可以将代码从构造函数移动到 MyTestInit() 方法 - 它应该可以工作。