Junit5 动态执行测试类

Junit5 重新构造了junit5,支持在代码中执行测试类

使用LauncherDiscoveryRequest、Launcher的方法实现

public void executeByJunit5(String serviceName, String branch, String fileName) {
        //需要执行的测试类绝对路径
        String testCaseFileAbsPath = "/opt/testcases/" + fileName + ".class";
        // 被测服务当前branch dependencies lib目录
        String branchLibPath = "/opt/libs/clustera-1-2-3";
        try {
            //新建classloader
            ClassLoader loader = ClassLoaderUtil.createClassLoader("/opt/common/lib");

            Collection<File> jars = FileUtils.listFiles(new File(branchLibPath), new String[]{"jar"}, true);
            for (File f : jars) {
                if (!f.getName().contains("sources")) {
                    String dependentJar = f.getCanonicalPath();
                    ClassLoaderUtil.addPath(loader, new File(dependentJar).toURI().toURL());
                }
            }
            // 测试类加到classloader上
            ClassLoaderUtil.addPath(loader, new File("/opt/testcases/").toURI().toURL());
            // 设置当前进程的classloader为新建的loader
            Thread.currentThread().setContextClassLoader(loader);

            // 测试类
            Class testCaseClazz = loader.loadClass(fileName);

            LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
                    .request()
                    .selectors(selectClass(testCaseClazz)).build();

            Launcher launcher = LauncherFactory.create();

            // Register a listener of your choice
            SummaryGeneratingListener listener = new SummaryGeneratingListener();
            launcher.registerTestExecutionListeners(listener);

            launcher.execute(request);

            TestExecutionSummary summary = listener.getSummary();

            // 报告看情况入库还是直接展示
            loggerHelper.info(summary.toString());
        } catch (Exception e) {
            e.printStackTrace();
            loggerHelper.error(e.getStackTrace().toString());
        }
    }

对应的pom.xml

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.5.2</version>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.5.2</version>
</dependency>

使用EngineTestKit执行测试类

public void executeByJunit5(String serviceName, String branch, String fileName) {
        //需要执行的测试类绝对路径
        String testCaseFileAbsPath = "/opt/testcases/" + fileName + ".class";
        // 被测服务当前branch dependencies lib目录
        String branchLibPath = "/opt/libs/clustera-1-2-3";
        try {
            //新建classloader
            ClassLoader loader = ClassLoaderUtil.createClassLoader("/opt/common/lib");

            Collection<File> jars = FileUtils.listFiles(new File(branchLibPath), new String[]{"jar"}, true);
            for (File f : jars) {
                if (!f.getName().contains("sources")) {
                    String dependentJar = f.getCanonicalPath();
                    ClassLoaderUtil.addPath(loader, new File(dependentJar).toURI().toURL());
                }
            }
            // 测试类加到classloader上
            ClassLoaderUtil.addPath(loader, new File("/opt/testcases/").toURI().toURL());
            // 设置当前进程的classloader为新建的loader
            Thread.currentThread().setContextClassLoader(loader);

            // 测试类
            Class testCaseClazz = loader.loadClass(fileName);

            // 这种方法不会生成报告
            EngineTestKit
                    .engine("junit-jupiter")
                    .selectors(selectClass(testCaseClazz))
                    .execute();
        } catch (Exception e) {
            e.printStackTrace();
            loggerHelper.error(e.getStackTrace().toString());
        }
    }

pom.xml除了上面的依赖外,还需要增加

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-testkit</artifactId>
    <version>1.5.1</version>
</dependency>

Q&A

Q: 如果在编译测“警告: 未知的枚举常量 Status.STABLE 原因: 找不到org.apiguardian.api.API$Status的类文件”
A: claspath中缺少 apiguardian-api-1.1.0.jar

参考资料:
JUnit 5 User Guide https://goodjob.n2sm.net/cache/?docId=61b8421cac86430996e043365854d253&hq=Java
Junit5学习笔记 https://vitzhou.gitbooks.io/junit5/junit/advanced_topics.html
JUnit 5 Maven Dependency https://howtodoinjava.com/junit5/junit5-maven-dependency/
6.1.1. Discovering Tests https://junit.org/junit5/docs/current/user-guide/#launcher-api-discovery

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值