在每个'@Test'之后和JUnit中的每个'@After'之前应用'@Rule'

时间:2012-08-22 18:11:33

标签: java selenium webdriver junit4 junit-rule

我有一个测试套件,我在@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

3 个答案:

答案 0 :(得分:22)

由于规则的设置方式,您不能拥有@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

希望这有帮助!

答案 1 :(得分:4)

public class ScreenshotTestRule implements MethodRule {
    public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    statement.evaluate();

                } catch (Throwable t) {
                    captureScreenshot(frameworkMethod.getName());
                    throw t; // rethrow to allow the failure to be reported to JUnit                     
                } finally {
                    tearDown();
                }
            }

            public void tearDown() {
                //logout to the system;
            }


            public void captureScreenshot(String fileName) {
                try {
                    new File("target/surefire-reports/screenshot").mkdirs(); // Insure directory is there
                    FileOutputStream out = new FileOutputStream("target/surefire-reports/screenshot/screenshot-" + fileName + ".png");
                    out.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
                    out.close();
                } catch (Exception e) {
                    // No need to crash the tests if the screenshot fails
                }
            }
        };
    }
}

答案 2 :(得分:1)

使用ExternalResource规则怎么样?
看起来你可以为你提供足够的灵活性来满足你的需求。
如果这不是您所需要的,请查看外部资源的source code
很容易理解如何实现一个只在测试调用之后才能工作的规则。