![](/img/trans.png)
[英]Maven runs JUnit tests in Eclipse but compilation fails from the command line
[英]JUnit 5 fails to find Tests from the command line
类似的 stackoverflow 帖子:“JUnit 5 ConsoleLauncher 不起作用”回答 Marc Philipp 2018 年 3 月 13 日。
我试图复制上面发布的示例。 JUnit 测试 class 为“DisplayNameDemo.java”,如下所示:
DisplayNameDemo.java
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("A special test case")
public class DisplayNameDemo {
@Test
@DisplayName("Custom test name containing spaces")
void testWithDisplayNameContainingSpaces() {
}
@Test
@DisplayName("╯°□°)╯")
void testWithDisplayNameContainingSpecialCharacters() {
}
@Test
@DisplayName("😱 ")
void testWithDisplayNameContainingEmoji() {
}
}
这是我的 JUnit 5 命令行命令:“java -jar junit-platform-console-standalone-1.6.2.jar --classpath.--select-class DisplayNameDemo --include-classname '.*'”
这在我执行时失败了,但在帖子中执行时通过了。 这是我失败结果的一部分:
~/junit5.6.2/console$ java -jar junit-platform-console-standalone-1.6.2.jar --classpath . --select-class DisplayNameDemo --include-classname '.*'
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
Usage: ConsoleLauncher [-h] [--disable-ansi-colors] [--disable-banner]
[--fail-if-no-tests] [--scan-modules] [--scan-classpath[=PATH[;|:
PATH...]]]... [--details=MODE] [--details-theme=THEME]
[--reports-dir=DIR] [-c=CLASS]... [--config=KEY=VALUE]... [-cp=PATH
我将所有类与 junit-platform-console-standalone-1.6.2.jar 文件放在同一目录中。 我还将我的类路径设置为本地目录“。” 如上图所示。
我不明白为什么这个例子失败了。 我非常感谢一些帮助——尤其是来自 Marc Philipp 或 Somuras 在 stackoverflow 上的帮助。 谢谢你的帮助 !
我猜你不是用--classpath
指向你编译的 Java 类( .class
文件所在的位置),而是指向你的原始.java
文件。
如果你使用 Maven 来构建你的项目,你编译的测试类在target/test-classes
里面。 您现在可以从项目的根目录使用独立控制台启动器,例如:
java -jar junit-platform-console-standalone-1.6.2.jar --class-path 'target/test-classes' -c your.package.YourTest
如果您不使用 Maven 并且仍希望使用带有 --class --class-path.
然后确保您的.class
文件在那里。 例如,使用javac MyJavaTest.java
来编译它们(如果没有 Maven/Gradle 有点麻烦,因为您必须将所有库都包含到编译步骤中)。
有关更多信息和实际示例,请考虑遵循本教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.