Java Junit 5(Jupiter)测试状态
大家好,我正在使用Junit 5 Jupiter,我想知道每次测试后它是成功还是失败,并根据每个测试结果使用@AfterEach方法,比如如果成功,我将做一些事情,但如果失败,我将做其他事情 你能帮帮我吗。。 似乎@rule对JUnit4有效,而不是对Jupiter有效Java Junit 5(Jupiter)测试状态,java,selenium,junit,junit5,Java,Selenium,Junit,Junit5,大家好,我正在使用Junit 5 Jupiter,我想知道每次测试后它是成功还是失败,并根据每个测试结果使用@AfterEach方法,比如如果成功,我将做一些事情,但如果失败,我将做其他事情 你能帮帮我吗。。 似乎@rule对JUnit4有效,而不是对Jupiter有效 package Example; import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
package Example;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SSJunit5 {
static WebDriver driver;
@BeforeAll
public static void setUpBeforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\david\\Desktop\\Documents\\Chrome Driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@AfterAll
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@BeforeEach
public void setUp() throws Exception {
}
@AfterEach
public void tearDown() throws Exception {
}
@Rule
public TestRule watcher = new TestWatcher() {
@Override
protected void succeeded(Description description) {
System.out.println("Pass!");
}
@Override
protected void failed(Throwable e, Description description) {
ScreenShots.ScreenShot(driver, "SSafterSendKeys");
System.out.println("Fail!");
}
};
@Test
public void test() {
driver.get("https://www.google.com");
//ScreenShots.ScreenShot(driver, "webPageON");
driver.findElement(By.id("lsta-ib")).sendKeys("hello");
String expected = "asd";
String actual = "qwert";
assertEquals(expected, actual);
}
}
目前还没有替代TestWatcher的产品(目前正在开发中)。不过,您可以编写自己的扩展并检查
ExtensionContext.getExecutionException()
。在JUnitExtensions
库中还有一个JUnit5扩展,相当于JUnit4TestWatcher
。几天前,JUnit5.4.0引入了See和.TestWatcher。看看这个:(“JUnit5中的规则注释”: