繁体   English   中英

VS 2010,编码的UI测试:重新运行失败的测试用例

[英]VS 2010, Coded UI Tests: Re-run failed test cases

我正在使用VS2010 Premium,编码的UI测试。

你知道如何在运行后重新执行失败的测试用例吗? 如果在重新执行后传递了测试,那么它应该在结果报告中传递。

不是那么优化的方式,但是如果抛出异常,您可以将所有代码放入try/catch块并重新运行测试:

[CodedUITest]
public class CodedUITest
{
    private static int _maxTestRuns = 5;

    [TestCleanup]
    public void Cleanup()
    {
        //If the test has reached the max number of executions then it is failed.
        if (_maxTestRuns == 0)
            Assert.Fail("Test executed {0} times and it was failed", _maxTestRuns);
    }

    [TestMethod]
    public void CodedUITestMethod()
    {
        try
        {
            this.UIMap.RecordedMethod1();
        }

        catch (Exception exception)
        {
            //Call Cleanup, rerun the test and report the error.
            if (_maxTestRuns > 0)
            {
                _maxTestRuns--;
                TestContext.WriteLine(exception.Message);
                TestContext.WriteLine("Running Again...");
                this.Cleaup();
                this.CodedUITestMethod();
            }
        }
    }
}

您还可以概括Schaliasos提出的方法,我们可以创建一个这样的基类:

[CodedUITest]
public class _TestBase
{
    private static int _maxTestRuns;
    public Exception _currentException;

    public _TestBase()
    {
    }

    public void TryThreeTimes(Action testActions)
    {
        _maxTestRuns = 3;
        _currentException = null;
        while (_maxTestRuns > 0)
        {
            try
            {
                testActions();
            }
            catch (Exception exception)
            {
                _maxTestRuns--;
                _currentException = exception;
            }
            if (_currentException == null)
                break; // Test passed, stop retrying
        }
        if (_maxTestRuns == 0)  // If test failed three times, bubble up the exception.
        {
            throw _currentException;
        }          
    }

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext context
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }
    private TestContext testContextInstance;
}

然后当我们编写测试时,我们可以继承上面的类并执行此操作:

[CodedUITest]
public class NewTestClass : _TestBase
{
    [TestMethod]
    public void MyTestMethod1()
    {
        TryThreeTimes(new Action(() =>
            // Assertions and Records come here
            Assert.IsTrue(false);
         }));
    }

    [TestMethod]
    public void MyTestMethod2()
    {
        TryThreeTimes(new Action(() =>
            // Assertions and Records come here.
            Assert.IsTrue(true);
         }));
    }
}   

我一直在考虑如何使这更简单,任何建议将不胜感激。 如果您有许多想要经常运行的测试,这至少可以节省相当多的代码,也许有些人可以推广函数TryThreeTimes,因此其中一个参数将是重新运行的数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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