Jsystem test case 的rerun实现

本文介绍了如何在测试框架Jsystem中实现Rerun机制,针对不稳定用例,当测试失败时,可以重复运行指定次数,超过次数则视为最终结果。通过自定义Fixture和RunListener,实现对用例失败后的重试逻辑。

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





### 关于 Rerun 教程或指南 Rerun 是一种常见的开发工具功能,通常用于重新执行某些操作、脚本或者构建过程。以下是有关如何查找和理解 rerun 功能的相关信息。 #### 什么是 Rerun? 在软件开发环境中,“rerun”指的是重复运行某个特定的任务或命令。这可能涉及自动化构建系统中的任务重试机制,或者是调试过程中反复测试某段代码的行为。例如,在 BitBake 构建系统中,`DEBUG: LOAD ~/bbTutorial/meta-tutorial/classes/base.bbclass` 表明加载了一个类文件并准备进行处理[^1]。如果在此过程中发生错误,则可以使用 `rerun` 来尝试再次执行该任务。 #### 如何配置多个路径供 Rerun 使用? 当涉及到多位置资源管理时,可以通过冒号分隔不同目录来指定这些位置,无论操作系统是什么都可以适用这种方式;也可以多次应用此选项实现相同效果。另外需要注意的是,所提供的路径还可以是一个匹配若干条目的通配符模式(glob pattern),不过在这种情况下往往需要对其进行转义以免被误解析[^2]。 对于具体到某一框架下的 rerun 实现方式,请参考对应官方文档获取最权威指导说明: - 如果您正在研究像Bitbake这样的嵌入式Linux构建环境,那么应该查阅其手册了解关于task execution retry policies相关内容。 下面给出一段简单的伪代码表示基本逻辑结构: ```python def rerun_task(task_name, max_attempts=3): attempt_count = 0 while True: try: execute_task(task_name) break except TaskFailure as e: attempt_count += 1 if attempt_count >= max_attempts: raise MaximumRetriesExceeded(f"Task {task_name} failed after {max_attempts} attempts.") print("All tasks completed successfully!") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值