我正在尝试改进现有的自动 Selenium 测试系统。 我的目标是重复由于连接问题而失败的测试。 我已经找到并尝试关注此线程 How to Re-run failed JUnit tests immediately?这表明它非常有用。
在我的例子中,套件由类组成,所以我尝试用@ClassRule 替换@Rule,以便在每次尝试时重复@Before 和@After 部分。 很抱歉我的无知,但我应该把这条规则放在哪里呢?在我的套件类?或者在代表测试的类中?
最佳答案
我是How to Re-run failed JUnit tests immediately?的原回答者
如果我理解正确,您遇到的问题是由于 @Before
引起的在 RetryRule
中的代码之前执行, 和 @After
之后被执行。
所以你当前的行为是这样的:
@Before
@Retry
test code
@Retry
@After
但是你可以实现你的 @Before
和 @After
通常-有一条规则ExternalResource正是这样做的。你会实现 @Before
和 @After
通常:
@Rule public ExternalResource beforeAfter = new ExternalResource() {
public void before() {
// code that was in @Before
}
public void after() {
// code that was in @After
}
}
那么你不需要@Before
和 @After
.然后您可以使用 RuleChain 链接这些规则.这会强制执行您的规则:
@Rule public RuleChain chain= RuleChain
.outerRule(new LoggingRule("outer rule")
.around(new LoggingRule("middle rule")
.around(new LoggingRule("inner rule");
所以你的最终解决方案应该是这样的:
private ExternalResource beforeAfter = ...
private RetryRule retry = ...
@Rule public RuleChain chain = RuleChain
.outerRule(retry)
.around(beforeAfter);
请注意,如果您使用的是 RuleChain
,您不再需要 @Rule
ExternalResource
上的注释和 RetryRule
, 但你在 RuleChain
上做了.
关于java - 使用 Junit 重新运行失败的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16738997/