本文介绍了测试失败时未执行JUnit规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的测试文件:
package test;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
public class TestWatcherTest
{
@Rule
public TestWatcher testWatcher = new TestWatcher()
{
protected void failed(Throwable e, Description description)
{
System.out.println("in failed()");
}
};
@Test
public void shouldFail()
{
Assert.assertTrue("Test failed", false);
}
}
输出为:
<failure message="Test failed" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Test failed at test.TestWatcherTest.shouldFail(TestWatcherTest.java:24)
</failure>
似乎不执行failed().我读了很多帖子,但似乎对我没有任何帮助.我在做什么错了?
It seems failed() is not being executed. I read many posts but nothing seems to work for me. What am I doing wrong?
推荐答案
问题出在蚂蚁配置上.来自 http://testautomationusingjunit.blogspot.com/2013/08/applying-rules-to-junit-tests-running.html ,需要对目标进行编辑以在类路径中包含JUnitCore.
The issue was with ant configuration. From http://testautomationusingjunit.blogspot.com/2013/08/applying-rules-to-junit-tests-running.html, the target needs to be edited to contain JUnitCore in the classpath.
<target name="junit" depends="build">
<java classname="org.junit.runner.JUnitCore">
<classpath>
<path location="selenium_server/selenium-server-standalone-xxx.xx.jar"/>
</classpath>
</java>
<junit fork="no" haltonfailure="no" printsummary="true" failureProperty="test.failed" dir=".">
<test name="src.SampleTest" todir="./report" />
</junit>
</target>
这篇关于测试失败时未执行JUnit规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!