Junit 5(Jupiter)测试状态

时间:2018-09-16 11:11:45

标签: java selenium junit junit5

您好,我正在使用Junit 5 Jupiter,我想在每次测试后知道它是否成功,并通过每个测试结果使用@AfterEach方法,例如是否成功,但是我会做一些事情,但是如果失败,我会做其他的东西 你能帮我吗.. 似乎@rule适用于Junit 4,不适用于Jupiter

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);
    }

}

0 个答案:

没有答案