如何从命令行运行 JUnit 测试用例

发布于 2024-08-20 21:11:23 字数 34 浏览 6 评论 0原文

我想从命令行运行 JUnit 测试用例。 我该怎么做?

I would like to run JUnit test cases from the command line.
How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

只想待在家 2024-08-27 21:11:23

对于 JUnit 5.x,它是:

java -jar junit-platform-console-standalone-<version>.jar <Options>

https://stackoverflow.com/a/52373592 查找简短摘要/1431016 以及 https: //junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher

对于JUnit 4.X,这确实是:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

但是如果您使用 >JUnit 3.X 请注意类名称不同:

java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]

您可能需要将更多 JAR 或目录与类文件一起添加到类路径中,并用分号 (Windows) 或冒号 (UNIX/Linux) 分隔它们。这取决于您的环境。

编辑:我添加了当前目录作为示例。取决于您的环境以及构建应用程序的方式(可以是 bin/ 或 build/ 甚至 my_application.jar 等)。注意 Java 6+ 确实支持类路径中的 glob,您可以执行以下操作:

java -cp lib/*.jar:/usr/share/java/junit.jar ...

编写测试! :-)

For JUnit 5.x it's:

java -jar junit-platform-console-standalone-<version>.jar <Options>

Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher

For JUnit 4.X it's really:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

But if you are using JUnit 3.X note the class name is different:

java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]

You might need to add more JARs or directories with your class files to the classpath and separate that with semicolons (Windows) or colons (UNIX/Linux). It depends on your environment.

Edit: I've added current directory as an example. Depends on your environment and how you build your application (can be bin/ or build/ or even my_application.jar etc). Note Java 6+ does support globs in classpath, you can do:

java -cp lib/*.jar:/usr/share/java/junit.jar ...

Write tests! :-)

帅气称霸 2024-08-27 21:11:23

Maven 方式

如果您使用 Maven,您可以运行以下命令来运行所有测试用例:

mvn clean test

或者您可以运行如下所示的特定测试

mvn clean test -Dtest=your.package.TestClassName
mvn clean test -Dtest=your.package.TestClassName#particularMethod

如果您想查看堆栈跟踪(如果有)控制台而不是 target\surefire-reports 文件夹中的报告文件,请将用户属性 Surefire.useFile 设置为 false。例如:

mvn clean test -Dtest=your.package.TestClassName -Dsurefire.useFile=false

Gradle 方式

如果您使用 Gradle,您可以运行以下命令来运行所有测试用例:

gradle test

或者您可以运行如下所示的特定测试

gradle test --tests your.package.TestClassName
gradle test --tests your.package.TestClassName.particularMethod

如果您想了解更多信息,您可以考虑选项例如--stacktrace、或--info、或--debug。

例如,当您使用 info 日志记录级别 --info 运行 Gradle 时,它​​将向您显示每个测试运行时的结果。如果有任何异常,它会显示堆栈跟踪,指出问题所在。

gradle test --info

如果您想查看总体测试结果,可以在浏览器中打开报告,例如(在 Ubuntu 中使用 Google Chrome 打开):

google-chrome build/reports/tests/index.html

Ant way

一旦设置了 Ant 构建文件构建.xml 中,您可以从命令行运行 JUnit 测试用例,如下所示:

ant -f build.xml <Your JUnit test target name>

您可以按照下面的链接阅读有关如何在 Ant 构建文件中配置 JUnit 测试的更多信息:
https://ant.apache.org/manual/Tasks/junit.html

普通方式

如果您不使用 Maven、Gradle 或 Ant,您可以按照以下方式操作:

首先,您需要编译测试用例。例如(在 Linux 中):

javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/TestClassName.java

然后运行您的测试用例。例如:

java -cp /absolute/path/for/compiled/classes:/absolute/path/to/junit-4.12.jar:/absolute/path/to/hamcrest-core-1.3.jar org.junit.runner.JUnitCore your.package.TestClassName

Maven way

If you use Maven, you can run the following command to run all your test cases:

mvn clean test

Or you can run a particular test as below

mvn clean test -Dtest=your.package.TestClassName
mvn clean test -Dtest=your.package.TestClassName#particularMethod

If you would like to see the stack trace (if any) in the console instead of report files in the target\surefire-reports folder, set the user property surefire.useFile to false. For example:

mvn clean test -Dtest=your.package.TestClassName -Dsurefire.useFile=false

Gradle way

If you use Gradle, you can run the following command to run all your test cases:

gradle test

Or you can run a particular test as below

gradle test --tests your.package.TestClassName
gradle test --tests your.package.TestClassName.particularMethod

If you would like more information, you can consider options such as --stacktrace, or --info, or --debug.

For example, when you run Gradle with the info logging level --info, it will show you the result of each test while they are running. If there is any exception, it will show you the stack trace, pointing out what the problem is.

gradle test --info

If you would like to see the overall test results, you can open the report in the browser, for example (Open it using Google Chrome in Ubuntu):

google-chrome build/reports/tests/index.html

Ant way

Once you set up your Ant build file build.xml, you can run your JUnit test cases from the command line as below:

ant -f build.xml <Your JUnit test target name>

You can follow the link below to read more about how to configure JUnit tests in the Ant build file:
https://ant.apache.org/manual/Tasks/junit.html

Normal way

If you do not use Maven, or Gradle or Ant, you can follow the following way:

First of all, you need to compile your test cases. For example (in Linux):

javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/TestClassName.java

Then run your test cases. For example:

java -cp /absolute/path/for/compiled/classes:/absolute/path/to/junit-4.12.jar:/absolute/path/to/hamcrest-core-1.3.jar org.junit.runner.JUnitCore your.package.TestClassName
断念 2024-08-27 21:11:23

@lzap 给出的答案是一个很好的解决方案。不过,我想补充一点,你应该添加 .到类路径,这样你当前的目录就不会被遗漏,导致你自己的类被遗漏。我在某些平台上也遇到过这种情况。因此 JUnit 4.x 的更新版本是:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

The answer that @lzap gave is a good solution. However, I would like to add that you should add . to the class path, so that your current directory is not left out, resulting in your own classes to be left out. This has happened to me on some platforms. So an updated version for JUnit 4.x would be:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
黎歌 2024-08-27 21:11:23

确保 JUnit.jar 位于您的类路径中,然后从控制台调用命令行运行程序

java org.junit.runner.JUnitCore [测试类名称]

参考:junit FAQ

Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console

java org.junit.runner.JUnitCore [test class name]

Reference: junit FAQ

時窥 2024-08-27 21:11:23

对于 JUnit 4.12,以下内容对我不起作用:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

显然,从 JUnit 4.11 开始您还应该在类路径中包含 hamcrest-core.jar :

java -cp .:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore [test class name]

With JUnit 4.12 the following didn't work for me:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

Apparently, from JUnit 4.11 onwards you should also include hamcrest-core.jar in your classpath:

java -cp .:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore [test class name]
可爱咩 2024-08-27 21:11:23

在 Windows 中,它是

java -cp .;/path/junit.jar org.junit.runner.JUnitCore TestClass [不带 .class 扩展名的测试类名]

例如:
c:\>java -cp .;f:/libraries/junit-4.8.2 org.junit.runner.JUnitCore TestSample1 TestSample2 ... 依此类推,如果有多个测试类。

-cp 代表类路径,点 (.) 代表现有的类路径,而分号 (;) 将附加给定的 jar 附加到类路径,如上面的示例 junit-4.8.2 现已可用在类路径中执行 JUnitCore 类,这里我们用它来执行我们的测试类。

上述命令行语句可帮助您从命令提示符(即 MSDos)执行 junit(版本 4+)测试。

注意:JUnitCore 是执行 junit 测试的门面,该门面包含在 4+ 版本的 junit 中。

In windows it is

java -cp .;/path/junit.jar org.junit.runner.JUnitCore TestClass [test class name without .class extension]

for example:
c:\>java -cp .;f:/libraries/junit-4.8.2 org.junit.runner.JUnitCore TestSample1 TestSample2 ... and so on, if one has more than one test classes.

-cp stands for class path and the dot (.) represents the existing classpath while semi colon (;) appends the additional given jar to the classpath , as in above example junit-4.8.2 is now available in classpath to execute JUnitCore class that here we have used to execute our test classes.

Above command line statement helps you to execute junit (version 4+) tests from command prompt(i-e MSDos).

Note: JUnitCore is a facade to execute junit tests, this facade is included in 4+ versions of junit.

女中豪杰 2024-08-27 21:11:23

您可以使用下一个命令运行属于模块“my-module”的测试类 CustomTest 中的所有测试方法:

mvn clean test -pl :my-module -Dtest=CustomTest

或者使用下一个命令仅运行测试类 CustomTest 中的 1 个测试方法 myMethod:

mvn clean test -pl :my-module -Dtest=CustomTest#myMethod

如果您的项目是基于 Maven 的, 这种能力你需要 Maven Surefire Plugin v.2.7.3+ 和 Junit 4。
更多详细信息在这里:http://maven.apache。 org/surefire/maven-surefire-plugin/examples/single-test.html

If your project is Maven-based you can run all test-methods from test-class CustomTest which belongs to module 'my-module' using next command:

mvn clean test -pl :my-module -Dtest=CustomTest

Or run only 1 test-method myMethod from test-class CustomTest using next command:

mvn clean test -pl :my-module -Dtest=CustomTest#myMethod

For this ability you need Maven Surefire Plugin v.2.7.3+ and Junit 4.
More details is here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

乖乖兔^ω^ 2024-08-27 21:11:23

实际上,您还可以使 Junit 测试成为可运行的 Jar 并将可运行的 jar 称为
java-jar

Actually you can also make the Junit test a runnable Jar and call the runnable jar as
java -jar

夜深人未静 2024-08-27 21:11:23

就我个人而言,我会使用 Maven Surefire JUnit 运行程序来做到这一点。

Personally I would use the Maven surefire JUnit runner to do that.

峩卟喜欢 2024-08-27 21:11:23

我有同样的要求,使用命令行运行我在 IntelliJ 中创建的测试类(测试用例)。

以下步骤对我有用。

• 从以下链接下载“JUnit Standalone Launcher”JAR 文件。

https://junit .org/junit5/docs/5.0.0-M5/user-guide/#running-tests-console-launcher-options

• 更改为IntelliJ 项目的根目录。

cd

• 将下载的JAR 文件放入IntelliJ 项目的根目录中。

• 在根目录中创建一个名为“target”的新目录。

mkdir target

• 首先,编译Java 源文件并将它们放入“target”文件夹中。使用以下命令。

javac -d target src/main/java/org/example/SourceFile.java

• 然后,编译JUnit 测试文件并将它们放入“target”文件夹中。

javac -d target -cp target:junit-platform-console-standalone-1.9.3.jar src/test/java/org/example/TestSourceFile.java

• 最后,运行JUnit 测试用例。

java -jar junit-platform-console-standalone-1.9.3.jar --class-path target --select-class org.example.TestSourceFile

谢谢。

I had the same requirement to run a Test Class (test cases) I created in IntelliJ, using command line.

The following steps worked for me.

• Download “JUnit Standalone Launcher” JAR file from the below link.

https://junit.org/junit5/docs/5.0.0-M5/user-guide/#running-tests-console-launcher-options

• Change into the IntelliJ project's root directory.

cd <root_directory>

• Put the downloaded JAR file in the IntelliJ Project’s root directory.

• Create a new directory named “target” in the root directory.

mkdir target

• First, compile the Java Source Files and put them in the “target” folder. Use the below command.

javac -d target src/main/java/org/example/SourceFile.java

• Then, compile the JUnit Test Files and put them in the “target” folder.

javac -d target -cp target:junit-platform-console-standalone-1.9.3.jar src/test/java/org/example/TestSourceFile.java

• Finally, run the JUnit Test Cases.

java -jar junit-platform-console-standalone-1.9.3.jar --class-path target --select-class org.example.TestSourceFile

Thank you.

ぽ尐不点ル 2024-08-27 21:11:23

或者,您可以在 JunitCore 类中使用以下方法 http://junit.sourceforge .net/javadoc/org/junit/runner/JUnitCore.html

从 java 文件运行(使用 Request 、 Class 类和 Runner )或 runClasses 。

Alternatively you can use the following methods in JunitCore class http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html

run (with Request , Class classes and Runner) or runClasses from your java file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文