1 回答
TA贡献1875条经验 获得超3个赞
您需要做的是创建一个处理测试结果的规则。该规则将被创建为 TestWatcher,然后您将覆盖其失败和成功的方法。例子:
@Rule
public TestWatcher watchman = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
// Handle logging of failing tests
System.out.println("Fail!");
}
@Override
protected void succeeded(Description description) {
// Handle logging of succeeding tests
System.out.println("Success!");
}
};
描述参数包含有关失败类的信息http://junit.sourceforge.net/javadoc/org/junit/runner/Description.html
至于方法注释,以下注释是 TestNG - JUnit 4 的等效项:
@BeforeTest - @BeforeClass
@AfterTest - @AfterClass
@BeforeMethod - @Before
@AfterMethod - @After
以下代码演示了如何构建测试类:
public class MyTestClass {
@Rule
public TestWatcher watchman = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
// Handle logging of failing tests
System.out.println("Fail!");
}
@Override
protected void succeeded(Description description) {
// Handle logging of succeeding tests
System.out.println("Success!");
}
};
@BeforeClass
public static void setup() {
//Some setup before all test methods
}
@AfterClass
public static void cleanup() {
// Some cleanup after all test methods
}
@Before
public void register() {
// Some setup before each test method
}
@Test
public void succeedingTest() {
Assert.assertTrue(true);
}
@Test
public void failingTest() {
Assert.assertTrue(false);
}
}
添加回答
举报