4.3. 控制台启动器
它ConsoleLauncher是一个命令行 Java 应用程序,可让您从控制台启动 JUnit 平台。例如,它可用于运行 JUnit Vintage 和 JUnit Jupiter 测试并将测试执行结果打印到控制台。
包含所有依赖项的可执行文件junit-platform-console-standalone-1.10.1.jar发布在Maven 中央存储库的 junit-platform-console-standalone目录下
。它包括以下依赖项:
junit:junit:4.13.2
org.apiguardian:apiguardian-api:1.1.2
org.hamcrest:hamcrest-core:1.3
org.junit.jupiter:junit-jupiter-api:5.10.1
org.junit.jupiter:junit-jupiter-engine:5.10.1
org.junit.jupiter:junit-jupiter-params:5.10.1
org.junit.platform:junit-platform-commons:1.10.1
org.junit.platform:junit-platform-console:1.10.1
org.junit.platform:junit-platform-engine:1.10.1
org.junit.platform:junit-platform-launcher:1.10.1
org.junit.platform:junit-platform-reporting:1.10.1
org.junit.platform:junit-platform-suite-api:1.10.1
org.junit.platform:junit-platform-suite-commons:1.10.1
org.junit.platform:junit-platform-suite-engine:1.10.1
org.junit.platform:junit-platform-suite:1.10.1
org.junit.vintage:junit-vintage-engine:5.10.1
org.opentest4j:opentest4j:1.3.0
您可以运行独立程序ConsoleLauncher,如下所示。
$ java -jar junit-platform-console-standalone-1.10.1.jar execute
├─ JUnit Vintage
│ └─ example.JUnit4Tests
│ └─ standardJUnit4Test ✔
└─ JUnit Jupiter
├─ StandardTests
│ ├─ succeedingTest() ✔
│ └─ skippedTest() ↷ for demonstration purposes
└─ A special test case
├─ Custom test name containing spaces ✔
├─ ╯°□°)╯ ✔
└─ ???? ✔
Test run finished after 64 ms
[ 5 containers found ]
[ 0 containers skipped ]
[ 5 containers started ]
[ 0 containers aborted ]
[ 5 containers successful ]
[ 0 containers failed ]
[ 6 tests found ]
[ 1 tests skipped ]
[ 5 tests started ]
[ 0 tests aborted ]
[ 5 tests successful ]
[ 0 tests failed ]
您还可以运行独立程序ConsoleLauncher,如下所示(例如,包含目录中的所有 jar):
$ java -cp classes:testlib/* org.junit.platform.console.ConsoleLauncher
退出代码如果任何容器或测试失败,则
退出ConsoleLauncher并带有状态代码。1如果未发现任何测试并且--fail-if-no-tests提供了命令行选项,则ConsoleLauncher退出并显示状态代码2。否则,退出代码为0。
4.3.1. 子命令和选项
提供ConsoleLauncher以下子命令:
Usage: junit [OPTIONS] [COMMAND]
Launches the JUnit Platform for test discovery and execution.
[@
Commands:
discover Discover tests
execute Execute tests
engines List available test engines
For more information, please refer to the JUnit User Guide at
https://junit.org/junit5/docs/current/user-guide/
发现测试
Usage: junit discover [OPTIONS]
Discover tests
[@
--disable-banner Disable print out of the welcome message.
--disable-ansi-colors Disable ANSI colors in output (not supported by all terminals).
-h, --help Display help information.
SELECTORS
--scan-classpath, --scan-class-path[=PATH]
Scan all directories on the classpath or explicit classpath
roots. Without arguments, only directories on the system
classpath as well as additional classpath entries supplied via
-cp (directories and JAR files) are scanned. Explicit classpath
roots that are not on the classpath will be silently ignored.
This option can be repeated.
--scan-modules Scan all resolved modules for test discovery.
-u, --select-uri=URI Select a URI for test discovery. This option can be repeated.
-f, --select-file=FILE Select a file for test discovery. This option can be repeated.
-d, --select-directory=DIR Select a directory for test discovery. This option can be
repeated.
-o, --select-module=NAME Select single module for test discovery. This option can be
repeated.
-p, --select-package=PKG Select a package for test discovery. This option can be repeated.
-c, --select-class=CLASS Select a class for test discovery. This option can be repeated.
-m, --select-method=NAME Select a method for test discovery. This option can be repeated.
-r, --select-resource=RESOURCE
Select a classpath resource for test discovery. This option can
be repeated.
-i, --select-iteration=TYPE:VALUE[INDEX(..INDEX)?(,INDEX(..INDEX)?)*]
Select iterations for test discovery (e.g. method:com.acme.Foo#m()
[1..2]). This option can be repeated.
FILTERS
-n, --include-classname=PATTERN
Provide a regular expression to include only classes whose fully
qualified names match. To avoid loading classes unnecessarily,
the default pattern only includes class names that begin with
"Test" or end with "Test" or "Tests". When this option is
repeated, all patterns will be combined using OR semantics.
Default: ^(Test.*|.+[.$]Test.*|.*Tests?)$
-N, --exclude-classname=PATTERN
Provide a regular expression to exclude those classes whose fully
qualified names match. When this option is repeated, all
patterns will be combined using OR semantics.
--include-package=PKG Provide a package to be included in the test run. This option can
be repeated.
--exclude-package=PKG Provide a package to be excluded from the test run. This option
can be repeated.
-t, --include-tag=TAG Provide a tag or tag expression to include only tests whose tags
match. When this option is repeated, all patterns will be
combined using OR semantics.
-T, --exclude-tag=TAG Provide a tag or tag expression to exclude those tests whose tags
match. When this option is repeated, all patterns will be
combined using OR semantics.
-e, --include-engine=ID Provide the ID of an engine to be included in the test run. This
option can be repeated.
-E, --exclude-engine=ID Provide the ID of an engine to be excluded from the test run.
This option can be repeated.
RUNTIME CONFIGURATION
-cp, --classpath, --class-path=PATH
Provide additional classpath entries -- for example, for adding
engines and their dependencies. This option can be repeated.
--config=KEY=VALUE Set a configuration parameter for test discovery and execution.
This option can be repeated.
CONSOLE OUTPUT
--color-palette=FILE Specify a path to a properties file to customize ANSI style of
output (not supported by all terminals).
--single-color Style test output using only text attributes, no color (not
supported by all terminals).
--details=MODE Select an output details mode for when tests are executed. Use
one of: none, summary, flat, tree, verbose, testfeed. If 'none'
is selected, then only the summary and test failures are shown.
Default: tree.
--details-theme=THEME Select an output details tree theme for when tests are executed.
Use one of: ascii, unicode. Default is detected based on
default character encoding.
For more information, please refer to the JUnit User Guide at
https://junit.org/junit5/docs/current/user-guide/
执行测试
Usage: junit execute [OPTIONS]
Execute tests
[@
--disable-banner Disable print out of the welcome message.
--disable-ansi-colors Disable ANSI colors in output (not supported by all terminals).
-h, --help Display help information.
SELECTORS
--scan-classpath, --scan-class-path[=PATH]
Scan all directories on the classpath or explicit classpath
roots. Without arguments, only directories on the system
classpath as well as additional classpath entries supplied via
-cp (directories and JAR files) are scanned. Explicit classpath
roots that are not on the classpath will be silently ignored.
This option can be repeated.
--scan-modules Scan all resolved modules for test discovery.
-u, --select-uri=URI Select a URI for test discovery. This option can be repeated.
-f, --select-file=FILE Select a file for test discovery. This option can be repeated.
-d, --select-directory=DIR Select a directory for test discovery. This option can be
repeated.
-o, --select-module=NAME Select single module for test discovery. This option can be
repeated.
-p, --select-package=PKG Select a package for test discovery. This option can be repeated.
-c, --select-class=CLASS Select a class for test discovery. This option can be repeated.
-m, --select-method=NAME Select a method for test discovery. This option can be repeated.
-r, --select-resource=RESOURCE
Select a classpath resource for test discovery. This option can
be repeated.
-i, --select-iteration=TYPE:VALUE[INDEX(..INDEX)?(,INDEX(..INDEX)?)*]
Select iterations for test discovery (e.g. method:com.acme.Foo#m()
[1..2]). This option can be repeated.
FILTERS
-n, --include-classname=PATTERN
Provide a regular expression to include only classes whose fully
qualified names match. To avoid loading classes unnecessarily,
the default pattern only includes class names that begin with
"Test" or end with "Test" or "Tests". When this option is
repeated, all patterns will be combined using OR semantics.
Default: ^(Test.*|.+[.$]Test.*|.*Tests?)$
-N, --exclude-classname=PATTERN
Provide a regular expression to exclude those classes whose fully
qualified names match. When this option is repeated, all
patterns will be combined using OR semantics.
--include-package=PKG Provide a package to be included in the test run. This option can
be repeated.
--exclude-package=PKG Provide a package to be excluded from the test run. This option
can be repeated.
-t, --include-tag=TAG Provide a tag or tag expression to include only tests whose tags
match. When this option is repeated, all patterns will be
combined using OR semantics.
-T, --exclude-tag=TAG Provide a tag or tag expression to exclude those tests whose tags
match. When this option is repeated, all patterns will be
combined using OR semantics.
-e, --include-engine=ID Provide the ID of an engine to be included in the test run. This
option can be repeated.
-E, --exclude-engine=ID Provide the ID of an engine to be excluded from the test run.
This option can be repeated.
RUNTIME CONFIGURATION
-cp, --classpath, --class-path=PATH
Provide additional classpath entries -- for example, for adding
engines and their dependencies. This option can be repeated.
--config=KEY=VALUE Set a configuration parameter for test discovery and execution.
This option can be repeated.
CONSOLE OUTPUT
--color-palette=FILE Specify a path to a properties file to customize ANSI style of
output (not supported by all terminals).
--single-color Style test output using only text attributes, no color (not
supported by all terminals).
--details=MODE Select an output details mode for when tests are executed. Use
one of: none, summary, flat, tree, verbose, testfeed. If 'none'
is selected, then only the summary and test failures are shown.
Default: tree.
--details-theme=THEME Select an output details tree theme for when tests are executed.
Use one of: ascii, unicode. Default is detected based on
default character encoding.
REPORTING
--fail-if-no-tests Fail and return exit status code 2 if no tests are found.
--reports-dir=DIR Enable report output into a specified local directory (will be
created if it does not exist).
For more information, please refer to the JUnit User Guide at
https://junit.org/junit5/docs/current/user-guide/
列出测试引擎
Usage: junit engines [OPTIONS]
List available test engines
[@
--disable-banner Disable print out of the welcome message.
--disable-ansi-colors
Disable ANSI colors in output (not supported by all terminals).
-h, --help Display help information.
For more information, please refer to the JUnit User Guide at
https://junit.org/junit5/docs/current/user-guide/
4.3.2. 参数文件(@-文件)
在某些平台上,当创建具有大量选项或长参数的命令行时,您可能会遇到命令行长度的系统限制。
从 1.3 版本开始,ConsoleLauncher支持参数 files,也称为
@-files。参数文件是本身包含要传递给命令的参数的文件。当底层picocli命令行解析器遇到以字符 开头的参数时@,它会将该文件的内容扩展到参数列表中。
文件中的参数可以用空格或换行符分隔。如果参数包含嵌入的空格,则整个参数应该用双引号或单引号引起来 — 例如,"-f=My Files/Stuff.java"。
如果参数文件不存在或无法读取,则参数将按字面意思处理,不会被删除。这可能会导致“参数不匹配”错误消息。picocli.trace您可以通过在系统属性设置为 的情况下执行命令来解决此类错误
DEBUG。
可以在命令行上指定多个@-文件。指定的路径可以是相对于当前目录的路径,也可以是绝对路径。
@您可以通过使用附加符号对其进行转义来传递带有初始字符的实际参数@。例如,@@somearg将成为@somearg且不会受到扩张。
4.3.3. 颜色定制
输出中使用的颜色ConsoleLauncher可以自定义。该选项--single-color将应用内置的单色样式,同时
--color-palette接受属性文件来覆盖
ANSI SGR颜色样式。下面的属性文件演示了默认样式:
SUCCESSFUL = 32
ABORTED = 33
FAILED = 31
SKIPPED = 35
CONTAINER = 35
TEST = 34
DYNAMIC = 35
REPORTED = 37