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

2019-05-14 14:58发布

I'm using VS2010 Premium, Coded UI Tests.

Do you know how to re-execute failed test cases after run? If test was passed after re-execution then it should be passed in result report.

2条回答
甜甜的少女心
2楼-- · 2019-05-14 15:38

Not so optimal way but you could put all your code to try/catch block and rerun the test if an exception is thrown:

[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();
            }
        }
    }
}
查看更多
欢心
3楼-- · 2019-05-14 15:39

You could also generalize the method Schaliasos proposes, we could create a base class like this:

[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;
}

And then when we write tests we could inherit the class above and do this:

[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);
         }));
    }
}   

I have been thinking about how this could be made even simpler, any suggestions would be appreciated. This at least saves quite a bit of code if you have many tests that you would like to run often, maybe it would make sense for some to generalize the function TryThreeTimes so one of the arguments would be the number of reruns.

查看更多
登录 后发表回答