一尘不染

在JUnit中,在每个“ @Test”之后和每个“ @After”之前应用“ @Rule”

selenium

我有一个测试套件,可以在其中退出系统@After并在中关闭浏览器@AfterClass。我尝试使用@Rule每种测试方法都使用Selenium拍摄失败的测试屏幕截图。我手动检查了@Rule它只在每个之前运行,@Before但我想在它@Test之前和之后进行设置@After。我找不到简单的解决方案。任何帮助将不胜感激。

public class MorgatgeCalculatorTest  {

@Before
public void before(){
    System.out.println("I am before");
}
@BeforeClass
public static void beforeclass(){
    System.out.println("I am beforeclass");
}
@Test
    public void test(){
        System.out.println("I am Test");
    }
@Test
public void test2(){
    System.out.println("I am Test2");
}
@After
    public void after(){
        System.out.println("I am after");
    }
@AfterClass
        public static  void afterclass(){
            System.out.println("I am afterclass");

}
@Rule
ExpensiveExternalResource ExpensiveExternalResource = new ExpensiveExternalResource();

static class ExpensiveExternalResource implements MethodRule  {
    public ExpensiveExternalResource(){
        System.out.println("I am rule");
    }

    @Override
    public Statement apply(Statement arg0, FrameworkMethod arg1, Object arg2) {
        // TODO Auto-generated method stub
        return null;
    }    
}

我得到的输出是

I am beforeclass
I am rule
I am before
I am Test
I am after
I am rule
I am before
I am Test2
I am after
I am afterclass

阅读 272

收藏
2020-06-26

共1个答案

一尘不染

由于规则的设置方式,因此不能有@before之后或@after之后的规则。您可以想到放在测试方法上的诸如shell之类的规则。要进行的第一个shell是@
before / @ after。此后,将应用@rules。

一种快速执行所需操作的方法是完全避免使用@After。可以创建一个规则,以便在方法失败时截取屏幕截图,然后在代码执行后执行截图。它不像@After一样漂亮,但是可以工作。(我也实现了TestRule,因为MethodRule已被贬值)。

public class MortgageCalculatorTest  {
    @Before
    public void before(){
        System.out.println("I am before");
    }

    @BeforeClass
    public static void beforeclass(){
        System.out.println("I am beforeclass");
    }

    @Test
    public void test(){
        System.out.println("I am a Test");
    }

    @Test
    public void test2(){
        System.out.println("I am a Failed Test");
        fail();
    }

    @AfterClass
            public static  void afterclass(){
                System.out.println("I am afterclass");

    }

    @Rule
    public ExpensiveExternalResource ExpensiveExternalResource = new ExpensiveExternalResource();

    public static class ExpensiveExternalResource implements TestRule  {


      //  public ExpensiveExternalResource(){}


        public class ExpansiveExternalResourceStatement extends Statement{

            private Statement baseStatement;

            public ExpansiveExternalResourceStatement(Statement b){
                baseStatement = b;
            }

            @Override
            public void evaluate() throws Throwable {
                try{
                    baseStatement.evaluate();
                }catch(Error e){
                    System.out.println("I take a Screenshot");
                    throw e;   
                }finally{
                    after();
                }
            }

            //Put your after code in this method!
            public void after(){
                System.out.println("I am after");
            }
        }

        public Statement apply(Statement base, Description description) {
            return new ExpansiveExternalResourceStatement(base);

        }


    }
}

该规则的所有工作都在一个语句中完成。org.junit.runners.model.Statement是一个代表代码束的类。因此,这里 apply
方法将接收您要放置shell的代码束。Apply返回您的语句,该语句执行您提供的代码束,并用try / catch语句将其包围以捕获方法失败。

该方法的输出为:

I am beforeclass
I am before
I am a Test
I am after
I am before
I am a Failed Test
I take a Screenshot
I am after
I am afterclass

希望这可以帮助!

2020-06-26