以Command Line运行 Junit

我在写 JUnit相关的总结的时候,遇到一个问题。假如我想 说明我的测试结果的话,就必须从idea里面截图,再上传到 图床,再在文件中导入图片链接。就比较麻烦。

那有没有办法运行完测试以后,就有一堆文字来让我直接复制,显示出来测试结果呢? 通过查找,我们发现 JUnit的 ConsoleLaunch提供了这样的方法。

ConsoleLaunch 可以让你运行通过命令行来运行 test文件,并把结果输出到控制台上,这是一个单独的 Jar包。

最终效果如图:

本文地址:https://www.cnblogs.com/hchengmx/p/16795253.html
代码Git地址:https://gitee.com/hchengmx/junit5-samples

1. 下载

下载地址:Central Repository: org/junit/platform/junit-platform-console-standalone

下载对应版本中的 jar 文件即可。

P.S. 此文中应用的是 1.9.1

https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.9.1/

2. 使用方法

该文章使用的代码文件目录结构如下:

C:.
├─main
│  └─java
│      └─com
│          └─hchengmx
│              ├─annotation
│              │      TestCaseName.java
│              │
│              └─misc
│                      CustomDisplayNameGenerator.java
│                      CustomerDisabledCondition.java
│
└─test
    └─java
        └─com
            └─hchengmx
                ├─depends
                │      DependsTest.java
                │
                ├─display
                │      DisplayNameCustomTest.java
                │      DisplayNameGeneratorTest.java
                │      DisplayNameParamTest.java
                │      DisplayNameTest.java
                │
                ├─executioncondition
                │      CustomerDisabledConditionTest.java
                │
                ├─misc
                │      JavaFakerTest.java
                │      LeetcodeTest.java
                │
                └─nested
                        NestedSampleTest.java
$ java -jar junit-platform-console-standalone-1.9.1.jar <OPTIONS>

整个 Console Launcher的 Option 可以分为以下几个部分

2.1 SELECTORS

这个部分就是要选择要运行哪些Test Case。

要是选择 --scan-classpath / --scan-class-path,代表要运行该包下面的所有test文件。

其他的,就是可以加一些特定条件,只运行什么Class / 只运行什么包下的 test。

例子1:运行所有的Test Case

java -jar junit-platform-console-standalone-1.9.1.jar  --classpath target/classes --classpath target/test-classes  --scan-classpath
java -jar junit-platform-console-standalone-1.9.1.jar  -cp target/classes -cp target/test-classes  --scan-classpath

例子2:运行指定Class文件中的Test

java -jar junit-platform-console-standalone-1.9.1.jar  --classpath target/classes --classpath target/test-classes --select-class com.hchengmx.display.TestConsole

例子3:运行指定目录下的Test

java -jar junit-platform-console-standalone-1.9.1.jar  --classpath target/classes --classpath target/test-classes --select-package com.hchengmx.display

2.2 FILTER

例子1:运行tag为happypass的test case

$ java -jar junit-platform-console-standalone-1.9.1.jar  --classpath target/classes --classpath target/test-classes --include-tag happypass --scan-classpath

Example 2:运行在package com.hchengmx.display 下的test case

java -jar junit-platform-console-standalone-1.9.1.jar  --classpath target/classes --classpath target/test-classes --include-package com.hchengmx.display --scan-classpath

2.3 RUNTIME CONFIGURATION

2.3.1 classpath

以maven为例:

java -jar junit-platform-console-standalone-1.9.1.jar  --classpath target/classes --classpath target/test-classes --classpath C:\Users\hchen\.m2\repository 

注意:这里的 target/classes、target/test-classes 为编译后的目录文件,不同的语言/编译器路径略有不同,也可能没有。

2.3.2 config

并行运行所有的Test文件

java -jar junit-platform-console-standalone-1.9.1.jar --classpath target/classes --classpath target/test-classes --config=junit.jupiter.execution.parallel.enabled=true --config=junit.jupiter.execution.parallel.mode.default=concurrent --scan-classpath

2.4 REPORTING=

结果输出到什么目录,windows下注意用双斜线 \

$ java -jar junit-platform-console-standalone-1.9.1.jar  -cp target/classes -cp target/test-classes  --scan-classpath --reports-dir=C:\\Users\\hchen\\Downloads\\

java -cp lib/*:. org.junit.platform.console.ConsoleLauncher -p mx

2.5 CONSOLE OUTPUT

这里就是对shell窗口的output样式的参数。包括颜色、是不是隐藏欢迎语、详情的展示、

--disable-ansi-colors
--color-palette=FILE
--single-color
--details-theme=THEME
这几个是关于颜色 & 主题。

--details=MODE 展示的样式(区别就是展示的详细程度,summary, flat, tree, verbose 默认为 tree)

--disable-banner: 添加以后在console中就不显示这句话了 Thanks for using JUnit! Support its development at https://junit.org/sponsoring

3. 常见问题

1. 出现乱码

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

.
+-- JUnit Jupiter [OK]
| +-- BeforeTest [X] org/slf4j/Logger
| +-- DisplayNameGeneratorTest [OK]

这是因为是由 cmd 运行脚本文件的,需要采用 Git Bash或者其他软件来运行jar文件。

2. Caused by: java.lang.ClassNotFoundException: com.hchengmx.misc.CustomDisplayNameGenerator

表明ClassNotFoundException的包是本项目的包,这可能是因为 classpath的参数里面,没有包括该类所在的目录,通常来说,src

3. Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils

表明第三方的包找不到,则需要通过classpath也引入第三方的包

$ java -jar junit-platform-console-standalone-1.9.1.jar --classpath target/classes --classpath target/test-classes --classpath testlib/javafaker-1.0.2.jar --classpath testlib/snakeyaml-1.23-android.jar --classpath testlib/commons-lang3-3.5.jar --scan-classpath

ps: 笔者才疏学浅,这一步要是引用了多个jar,只找到资料需要一个一个引用进去,个人觉得比较麻烦。
是否可以把所有的jar放在一个目录下,只需要配置一个目录就好了。
官网也提供了类似的方法,JUnit 5 User Guide - Console Launcher,但是笔者没有尝试成功,要是有朋友找到方法还烦请评论区留言或者私信,不胜感激。

  1. 找不到test文件 0 tests found

运行没报错,但是输出表示 0 tests found,这是由于 classpath参数不正确,该classpath目录下没有test文件

$ java -jar junit-platform-console-standalone-1.9.1.jar --classpath target/classes --scan-classpath

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

.
+-- JUnit Jupiter [OK]
+-- JUnit Vintage [OK]
'-- JUnit Platform Suite [OK]

Test run finished after 57 ms
[         3 containers found      ]
[         0 containers skipped    ]
[         3 containers started    ]
[         0 containers aborted    ]
[         3 containers successful ]
[         0 containers failed     ]
[         0 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]

参考资料

  1. JUnit 5 ConsoleLauncher examples - Mkyong.com
  2. JUnit 5 User Guide - running-tests-console-launcher-options
  3. JUnit 5 Release Notes
  4. Junit 5 - ConsoleLauncher - Java Tutorials
  5. Command Line jUnit. Soo, you already watch all of Uncle Bob… | by David Raygoza Gómez | El Acordeon del Programador | Medium
posted on 2022-10-15 22:56  hchengmx  阅读(310)  评论(0编辑  收藏  举报