如何从命令行运行 JUnit 测试用例
我想从命令行运行 JUnit 测试用例。 我该怎么做?
I would like to run JUnit test cases from the command line.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想从命令行运行 JUnit 测试用例。 我该怎么做?
I would like to run JUnit test cases from the command line.
How can I do this?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
对于 JUnit 5.x,它是:
在 https://stackoverflow.com/a/52373592 查找简短摘要/1431016 以及 https: //junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher
对于JUnit 4.X,这确实是:
但是如果您使用 >JUnit 3.X 请注意类名称不同:
您可能需要将更多 JAR 或目录与类文件一起添加到类路径中,并用分号 (Windows) 或冒号 (UNIX/Linux) 分隔它们。这取决于您的环境。
编辑:我添加了当前目录作为示例。取决于您的环境以及构建应用程序的方式(可以是 bin/ 或 build/ 甚至 my_application.jar 等)。注意 Java 6+ 确实支持类路径中的 glob,您可以执行以下操作:
编写测试! :-)
For JUnit 5.x it's:
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:
But if you are using JUnit 3.X note the class name is different:
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:
Write tests! :-)
Maven 方式
如果您使用 Maven,您可以运行以下命令来运行所有测试用例:
或者您可以运行如下所示的特定测试
如果您想查看堆栈跟踪(如果有)控制台而不是 target\surefire-reports 文件夹中的报告文件,请将用户属性 Surefire.useFile 设置为 false。例如:
Gradle 方式
如果您使用 Gradle,您可以运行以下命令来运行所有测试用例:
或者您可以运行如下所示的特定测试
如果您想了解更多信息,您可以考虑选项例如--stacktrace、或--info、或--debug。
例如,当您使用 info 日志记录级别 --info 运行 Gradle 时,它将向您显示每个测试运行时的结果。如果有任何异常,它会显示堆栈跟踪,指出问题所在。
如果您想查看总体测试结果,可以在浏览器中打开报告,例如(在 Ubuntu 中使用 Google Chrome 打开):
Ant way
一旦设置了 Ant 构建文件构建.xml 中,您可以从命令行运行 JUnit 测试用例,如下所示:
您可以按照下面的链接阅读有关如何在 Ant 构建文件中配置 JUnit 测试的更多信息:
https://ant.apache.org/manual/Tasks/junit.html
普通方式
如果您不使用 Maven、Gradle 或 Ant,您可以按照以下方式操作:
首先,您需要编译测试用例。例如(在 Linux 中):
然后运行您的测试用例。例如:
Maven way
If you use Maven, you can run the following command to run all your test cases:
Or you can run a particular test as below
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:
Gradle way
If you use Gradle, you can run the following command to run all your test cases:
Or you can run a particular test as below
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.
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):
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:
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):
Then run your test cases. For example:
@lzap 给出的答案是一个很好的解决方案。不过,我想补充一点,你应该添加 .到类路径,这样你当前的目录就不会被遗漏,导致你自己的类被遗漏。我在某些平台上也遇到过这种情况。因此 JUnit 4.x 的更新版本是:
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:
确保 JUnit.jar 位于您的类路径中,然后从控制台调用命令行运行程序
参考:junit FAQ
Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console
Reference: junit FAQ
对于 JUnit 4.12,以下内容对我不起作用:
显然,从 JUnit 4.11 开始您还应该在类路径中包含 hamcrest-core.jar :
With JUnit 4.12 the following didn't work for me:
Apparently, from JUnit 4.11 onwards you should also include
hamcrest-core.jar
in your classpath:在 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.
您可以使用下一个命令运行属于模块“my-module”的测试类 CustomTest 中的所有测试方法:
或者使用下一个命令仅运行测试类 CustomTest 中的 1 个测试方法 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:
Or run only 1 test-method myMethod from test-class CustomTest using next command:
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
实际上,您还可以使 Junit 测试成为可运行的 Jar 并将可运行的 jar 称为
java-jar
Actually you can also make the Junit test a runnable Jar and call the runnable jar as
java -jar
就我个人而言,我会使用 Maven Surefire JUnit 运行程序来做到这一点。
Personally I would use the Maven surefire JUnit runner to do that.
我有同样的要求,使用命令行运行我在 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”文件夹中。使用以下命令。
• 然后,编译JUnit 测试文件并将它们放入“target”文件夹中。
• 最后,运行JUnit 测试用例。
谢谢。
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.
• Then, compile the JUnit Test Files and put them in the “target” folder.
• Finally, run the JUnit Test Cases.
Thank you.
或者,您可以在 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.