本文向您展示了如何使用JUnit 5 ConsoleLauncher
从命令行运行测试。
经过测试
- JUnit 5.5.2
- junit-platform-console-standalone 1.5.2
1.下载JAR
要从命令行运行测试,我们可以从Maven中央存储库手动下载junit-platform-console-standalone.jar 。
PS此示例使用1.5.2
版。
https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.5.2/
2.用法
通常,测试类位于以下类路径中:
-
build/classes/java/test
-
target/test-classes
2.1从该类路径build/classes/java/test
运行所有build/classes/java/test
Terminal
$ java -jar junit-platform-console-standalone-1.5.2.jar --classpath build/classes/java/test --scan-classpath
$ java -jar junit-platform-console-standalone-1.5.2.jar -cp build/classes/java/test --scan-classpath
2.2使用类名运行指定的测试。
Terminal
$ java -jar junit-platform-console-standalone-1.5.2.jar
-cp 'build/classes/java/test'
-c com.mkyong.order.MethodOrderTest
$ java -jar junit-platform-console-standalone-1.5.2.jar
-cp 'build/classes/java/test'
--select-class com.mkyong.order.MethodOrderTest
2.3从软件包中运行所有测试。
Terminal
$ java -jar junit-platform-console-standalone-1.5.2.jar
-cp 'build/classes/java/test'
--select-package com.mkyong.order
2.4从包中运行所有测试,通过正则表达式模式过滤测试的类名。
Terminal
$ java -jar junit-platform-console-standalone-1.5.2.jar
-cp 'build/classes/java/test'
--select-package com.mkyong.order
--include-classname='.*Count.*'
2.5输出样本。
Terminal
$ java -jar junit-platform-console-standalone-1.5.2.jar
-cp 'build/classes/java/test'
--select-package com.mkyong.order
--include-classname='.*'
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
+-- JUnit Jupiter [OK]
| +-- MethodRandomTest [OK]
| | +-- testA() [OK]
| | +-- testZ() [OK]
| | +-- testY() [OK]
| | +-- testE() [OK]
| | '-- testB() [OK]
| +-- MethodParameterCountTest [OK]
| | +-- Parameter Count : 3 [OK]
| | | +-- 1 ==> fruit='apple', qty=1, price=1.99 [OK]
| | | '-- 2 ==> fruit='banana', qty=2, price=2.99 [OK]
| | +-- Parameter Count : 2 [OK]
| | | +-- 1 ==> fruit='apple', qty=1 [OK]
| | | '-- 2 ==> fruit='banana', qty=2 [OK]
| | '-- Parameter Count : 1 [OK]
| | +-- 1 ==> ints=1 [OK]
| | +-- 2 ==> ints=2 [OK]
| | '-- 3 ==> ints=3 [OK]
| +-- MethodAlphanumericTest [OK]
| | +-- testA() [OK]
| | +-- testB() [OK]
| | +-- testE() [OK]
| | +-- testY() [OK]
| | '-- testZ() [OK]
| '-- MethodOrderTest [OK]
| +-- test2() [OK]
| +-- test3() [OK]
| +-- test1() [OK]
| +-- test0() [OK]
| '-- test4() [OK]
'-- JUnit Vintage [OK]
Test run finished after 109 ms
[ 9 containers found ]
[ 0 containers skipped ]
[ 9 containers started ]
[ 0 containers aborted ]
[ 9 containers successful ]
[ 0 containers failed ]
[ 22 tests found ]
[ 0 tests skipped ]
[ 22 tests started ]
[ 0 tests aborted ]
[ 22 tests successful ]
[ 0 tests failed ]
PS有关所有ConsoleLauncher选项,请参阅正式的JUnit 5指南。
3. Windows
3.1如果要在经典命令提示符下运行以上命令,请用双引号替换单引号,例如:
Command Prompt – Windows
$ java -jar junit-platform-console-standalone-1.5.2.jar
-cp "build/classes/java/test"
-c com.mkyong.order.MethodOrderTest
3.2传递--disable-ansi-colors
选项以禁用输出中的ANSI颜色。 命令提示符不支持此功能。
Command Prompt – Windows
$ java -jar junit-platform-console-standalone-1.5.2.jar
-cp "build/classes/java/test"
-c com.mkyong.order.MethodOrderTest
--disable-ansi-colors
下载源代码
$ git clone https://github.com/mkyong/junit-examples
参考文献
翻译自: https://mkyong.com/junit5/junit-5-consolelauncher-examples/