如何从命令行运行 JUnit 测试用例 [英] How to run JUnit test cases from the command line
问题描述
我想从命令行运行 JUnit 测试用例.我该怎么做?
I would like to run JUnit test cases from the command line. How can I do this?
推荐答案
对于 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
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
对于 JUnit 4.X,它确实是:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
但是如果您使用的是 JUnit 3.X 请注意类名是不同的:
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]
您可能需要将更多的 JAR 或目录与您的类文件添加到类路径中,并用分号 (Windows) 或冒号 (UNIX/Linux) 分隔它们.这取决于您的环境.
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.
我添加了当前目录作为示例.取决于您的环境以及您构建应用程序的方式(可以是 bin/或 build/甚至 my_application.jar 等).注意 Java 6+ 确实支持类路径中的 globs,你可以这样做:
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 ...
希望有帮助.编写测试! :-)
这篇关于如何从命令行运行 JUnit 测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!