JUnit 5 ConsoleLauncher示例

本文详细介绍了如何使用JUnit 5的ConsoleLauncher从命令行运行测试,包括下载JAR文件,不同平台的使用方法,以及如何通过各种参数运行特定的测试。提供了Windows环境下命令行的使用示例,并给出了源代码的获取方式。
摘要由CSDN通过智能技术生成

本文向您展示了如何使用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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值