简体   繁体   English

JUnit 5 无法从命令行找到测试

[英]JUnit 5 fails to find Tests from the command line

Similar stackoverflow posting: "JUnit 5 ConsoleLauncher doesn't work" answered Marc Philipp 13 March 2018.类似的 stackoverflow 帖子:“JUnit 5 ConsoleLauncher 不起作用”回答 Marc Philipp 2018 年 3 月 13 日。

I tried to duplicate the example posted in the above posting.我试图复制上面发布的示例。 The JUnit Test class is "DisplayNameDemo.java" shown below: 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() {
      }
  }

Here is my JUnit 5 command line command: "java -jar junit-platform-console-standalone-1.6.2.jar --classpath. --select-class DisplayNameDemo --include-classname '.*'"这是我的 JUnit 5 命令行命令:“java -jar junit-platform-console-standalone-1.6.2.jar --classpath.--select-class DisplayNameDemo --include-classname '.*'”

This failed when I executed it, but passed when executed in the post.这在我执行时失败了,但在帖子中执行时通过了。 Here a partial of my failed result:这是我失败结果的一部分:

~/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

I have all classes in the same directory as the junit-platform-console-standalone-1.6.2.jar file.我将所有类与 junit-platform-console-standalone-1.6.2.jar 文件放在同一目录中。 I also have my classpath set to the local directory "."我还将我的类路径设置为本地目录“。” as shown above.如上图所示。

I cannot understand why this example fails.我不明白为什么这个例子失败了。 I would very much appreciate some help - especially from Marc Philipp or Sormuras on stackoverflow.我非常感谢一些帮助——尤其是来自 Marc Philipp 或 Somuras 在 stackoverflow 上的帮助。 Thanks for any help !谢谢你的帮助 !

I guess you don't point with --classpath to your compiled Java classes (where the .class files are), but to your raw .java files.我猜你不是用--classpath指向你编译的 Java 类( .class文件所在的位置),而是指向你的原始.java文件。

If you are using Maven to build your project, your compiled test classes are inside target/test-classes .如果你使用 Maven 来构建你的项目,你编译的测试类在target/test-classes里面。 You can now use the standalone console launcher from the root of your project like:您现在可以从项目的根目录使用独立控制台启动器,例如:

java -jar junit-platform-console-standalone-1.6.2.jar --class-path 'target/test-classes' -c your.package.YourTest

If you are not using Maven and want to still run it with the console launcher with --class-path.如果您不使用 Maven 并且仍希望使用带有 --class --class-path. then make sure your .class files are there.然后确保您的.class文件在那里。 Eg use javac MyJavaTest.java to compile them (a little bit cumbersome without Maven/Gradle as you would have to include all your libraries to the compile step).例如,使用javac MyJavaTest.java来编译它们(如果没有 Maven/Gradle 有点麻烦,因为您必须将所有库都包含到编译步骤中)。

For more information and practical examples, consider following this tutorial .有关更多信息和实际示例,请考虑遵循本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM