我正在使用VS2010 Premium,编码的UI测试。
您知道如何在运行后重新执行失败的测试用例吗? 如果在重新执行后传递了测试,那么它应该在结果报告中传递。
答案 0 :(得分:5)
不是那么优化的方法,但你可以将所有代码放到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();
}
}
}
}
答案 1 :(得分:0)
你也可以概括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,因此其中一个参数就是重新运行的数量。