简体   繁体   English

如何从命令行而不是 Intellij IDEA 运行 JUnit 测试?

[英]How to run JUnit tests from the command line instead of Intellij IDEA?

I have an Intellij IDEA project with Junit5 tests in the tests folder.我在测试文件夹中有一个带有 Junit5 测试的 Intellij IDEA 项目。 The tests works if I click on the play button using IDEA's GUI.如果我使用 IDEA 的 GUI 单击播放按钮,则测试有效。 However, I need to be able to use the command line to run those tests but I don't know how.但是,我需要能够使用命令行来运行这些测试,但我不知道如何。

Given the project folder how can I use the command line to run the tests in the tests folder without relying on IDEA's GUI?给定项目文件夹,我如何使用命令行在测试文件夹中运行测试而不依赖 IDEA 的 GUI?

You can use the JUnit ConsoleLauncher :您可以使用JUnit ConsoleLauncher

The ConsoleLauncher is a command-line Java application that lets you launch the JUnit Platform from the console. ConsoleLauncher 是一个命令行 Java 应用程序,可让您从控制台启动 JUnit 平台。 For example, it can be used to run JUnit Vintage and JUnit Jupiter tests and print test execution results to the console.例如,它可用于运行 JUnit Vintage 和 JUnit Jupiter 测试并将测试执行结果打印到控制台。

To run all tests from a package you can run:要从 package 运行所有测试,您可以运行:

java -jar junit-platform-console-standalone-1.5.2.jar 
      -cp 'build/classes/java/test' 
      --select-package com.mypackage

You can read the tutorial from MKyong for an introduction: JUnit 5 ConsoleLauncher examples您可以阅读 MKyong 的教程进行介绍: JUnit 5 ConsoleLauncher examples

Most build systems provide test targets.大多数构建系统都提供测试目标。 For example:例如:

  • Gradle: ./gradlew test Gradle: ./gradlew test
  • Maven: mvn test Maven:MVN mvn test

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

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