我正在使用 VS2010 Premium,编码的 UI 测试。
你知道如何在运行后重新执行失败的测试用例吗?如果重新执行后测试通过,则应在结果报告中通过。
我正在使用 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 是有意义的,因此其中一个参数是重新运行的次数。