Jsystem test case 的rerun实现

Jsystem是基于Junit的测试框架,优点在于基于GUI界面实现对test suite的编辑管理。

Jsystem默认没有提供Rerun机制,但是对于一些不稳定的case,我们期望当case fail时,可以Rerun几遍,如果过了就算过了,如果超过几次还是fail那么才算真的fail。

利用JunitCore Fixture 和 RunListener 可以实现该功能,当然目前该方法并不完美,存在嵌套case的情况,不过已经足够满足我们的要求。

请看下面的时序图

关键步骤描述

1. 设置Fixture 和tearDownFixture

    自定义class 继承自Fixture

    e.g.

        public class MyFixture extends Fixture {

            public static String testCaseName;

            private static int rerurnCount = 0;

            private static boolean rerun = true;

            pivate final int MAX_RERUN_COUNT = 3;

            public void failTearDown() throws Exception{

                if(rerunCount < MAX_RERUN_COUNT){

                    rerunCount++;

                    org.junt.runner.JUnitCore core = new org.juit.runner.JUnitCore();

                    core.addListener(new MyTestRunListener());

                    core.run(Class.forName(testCaseName);

                } else {

                    resetRerunCount();

                }

            }

           

             pubilc static void resetRerunCount() {

                 rerunCount = 0;

             }

        }


    Jsystem的case都是SystemTestCase4的子类,使用setFixture 和setTearDownFixture 设置Fixture,目的是当case fail时可以进入自定义的Fixture,以便rerun case

    e.g

        MyTestCase.setFixture(MyFixture.class);

        MyTestCase.setTearDownFixture(RootFixture.class); //为什么不能是MyFixture,我没搞清楚


2.自定义RunListener,实现对Rerun次数的控制

   每次rerun重置rerun标志为false,若rurun的case pass了,不会进入RunListener的 testFailure方法,直接进入testFinished,那么要重置Fixture的reRunCount,

否则会造成,接下的case若再fail再不会触发rerun.

    e.g.

        public class MyTestRunListener extends RunListener {

            public void testFailure(Failure failure){
                MyTestFixture.rerun = true;
            }

            public void testFinished(Description description){
                //If rerun passed it would not execute MyTestFixture failTearDown
                //It would execute next test case directly.
                if(!MyTestFixture.rerun){
                    MyTestFixture.resetRerunCount();
                }

             }

            public void testRunStarted(Description description){
                MyTestFixture.rerun = false;
            }
        }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值