当测试失败没有被执行的JUnit规则(JUnit Rule not being executed w

2019-10-18 12:37发布

以下是我的测试文件:

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>

它似乎失败()没有被执行。 我看了很多帖子,但似乎没有为我工作。 我究竟做错了什么?

Answer 1:

问题是用蚂蚁的配置。 从http://testautomationusingjunit.blogspot.com/2013/08/applying-rules-to-junit-tests-running.html ,目标需要被编辑包含JUnitCore在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 Rule not being executed when test fails
标签: junit-rule