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

时间:2021-03-21 01:20:42

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

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

11 个解决方案

#1


220  

For JUnit 4.X it's really:

JUnit 4。X真是:

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:

但是如果你使用的是JUnit 3。注意,类名不同:

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.

您可能需要在类路径中添加更多的jar或目录,并将其与分号(Windows)或colons (UNIX/Linux)分开。这取决于你的环境。

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:

编辑:我添加了当前目录作为示例。取决于您的环境和如何构建应用程序(可以是bin/或构建/甚至my_application)。jar等等)。注意,Java 6+确实支持类路径中的globs,您可以这样做:

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

Hope it helps. Write tests! :-)

希望它可以帮助。编写测试!:-)

#2


50  

Maven way

Maven的方式

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

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

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:

如果您希望在控制台中看到堆栈跟踪(如果有的话),而不是在目标\surefire reports文件夹中报告文件,则设置用户属性surefire。useFile为假。例如:

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

Gradle way

Gradle方式

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

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

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.

如果您希望获得更多信息,可以考虑一些选项,比如stacktrace,或者——info,或者——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.

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

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):

如果您希望看到整个测试结果,您可以在浏览器中打开报告,例如(在Ubuntu中使用谷歌Chrome):

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构建文件之后。xml,可以从命令行运行JUnit测试用例,如下所示:

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

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

Normal way

正常方式

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

如果您不使用Maven,或Gradle或Ant,您可以遵循以下方式:

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

首先,您需要编译您的测试用例。例如(在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

#3


48  

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:

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

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

#4


21  

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

确保JUnit。jar位于类路径中,然后从控制台调用命令行运行器。

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

junitcore[测试类名称]

Reference: junit FAQ

参考:junit常见问题解答

#5


18  

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

对于JUnit 4.12,下面的方法对我不起作用:

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:

显然,从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]

#6


12  

In windows it is

在窗户

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

java - cp。;/道路/ junit。jar文件。junitcore TestClass[测试类名没有。类扩展名]

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.

例如:c:\>java -cp .;f:/libraries/junit-4.8.2 org. junitcore TestSample1 TestSample2…等等,如果一个有多个测试类。

-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.

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

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

上面的命令行语句帮助您从命令提示符(i-e MSDos)执行junit(版本4+)测试。

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

注意:JUnitCore是一个执行junit测试的facade,这个facade包含在junit的4+版本中。

#7


5  

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

实际上,您还可以让Junit测试一个可运行的Jar,并将runnable Jar称为java -jar。

#8


4  

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:

如果您的项目是基于maven的,那么您可以使用下一个命令来运行属于模块“my-module”的测试类定制测试的所有测试方法:

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

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

或者使用下一个命令从test-class CustomTest中运行1个测试方法myMethod:

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

对于这种能力,您需要Maven Surefire Plugin .2.7.3+和Junit 4。更多细节在这里:http://maven.apache.org/surefire/maven-surefireplugin/examples/test.html。

#9


4  

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

就我个人而言,我将使用Maven surefire JUnit runner来做到这一点。

#10


0  

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

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

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

从java文件运行(请求、类类和运行器)或运行类。

#11


-2  

If you project is ant based then you should be able to do something like this from the console:

如果你的项目是基于ant的那么你应该能够从控制台做这样的事情:

ant test

If this doesn't work, but still your project is ant based, you can run ant -p to list the main targets of the project.

如果这个方法不起作用,但是您的项目仍然是基于ant的,那么您可以运行ant -p来列出项目的主要目标。

#1


220  

For JUnit 4.X it's really:

JUnit 4。X真是:

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:

但是如果你使用的是JUnit 3。注意,类名不同:

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.

您可能需要在类路径中添加更多的jar或目录,并将其与分号(Windows)或colons (UNIX/Linux)分开。这取决于你的环境。

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:

编辑:我添加了当前目录作为示例。取决于您的环境和如何构建应用程序(可以是bin/或构建/甚至my_application)。jar等等)。注意,Java 6+确实支持类路径中的globs,您可以这样做:

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

Hope it helps. Write tests! :-)

希望它可以帮助。编写测试!:-)

#2


50  

Maven way

Maven的方式

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

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

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:

如果您希望在控制台中看到堆栈跟踪(如果有的话),而不是在目标\surefire reports文件夹中报告文件,则设置用户属性surefire。useFile为假。例如:

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

Gradle way

Gradle方式

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

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

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.

如果您希望获得更多信息,可以考虑一些选项,比如stacktrace,或者——info,或者——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.

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

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):

如果您希望看到整个测试结果,您可以在浏览器中打开报告,例如(在Ubuntu中使用谷歌Chrome):

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构建文件之后。xml,可以从命令行运行JUnit测试用例,如下所示:

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

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

Normal way

正常方式

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

如果您不使用Maven,或Gradle或Ant,您可以遵循以下方式:

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

首先,您需要编译您的测试用例。例如(在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

#3


48  

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:

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

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

#4


21  

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

确保JUnit。jar位于类路径中,然后从控制台调用命令行运行器。

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

junitcore[测试类名称]

Reference: junit FAQ

参考:junit常见问题解答

#5


18  

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

对于JUnit 4.12,下面的方法对我不起作用:

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:

显然,从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]

#6


12  

In windows it is

在窗户

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

java - cp。;/道路/ junit。jar文件。junitcore TestClass[测试类名没有。类扩展名]

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.

例如:c:\>java -cp .;f:/libraries/junit-4.8.2 org. junitcore TestSample1 TestSample2…等等,如果一个有多个测试类。

-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.

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

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

上面的命令行语句帮助您从命令提示符(i-e MSDos)执行junit(版本4+)测试。

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

注意:JUnitCore是一个执行junit测试的facade,这个facade包含在junit的4+版本中。

#7


5  

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

实际上,您还可以让Junit测试一个可运行的Jar,并将runnable Jar称为java -jar。

#8


4  

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:

如果您的项目是基于maven的,那么您可以使用下一个命令来运行属于模块“my-module”的测试类定制测试的所有测试方法:

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

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

或者使用下一个命令从test-class CustomTest中运行1个测试方法myMethod:

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

对于这种能力,您需要Maven Surefire Plugin .2.7.3+和Junit 4。更多细节在这里:http://maven.apache.org/surefire/maven-surefireplugin/examples/test.html。

#9


4  

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

就我个人而言,我将使用Maven surefire JUnit runner来做到这一点。

#10


0  

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

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

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

从java文件运行(请求、类类和运行器)或运行类。

#11


-2  

If you project is ant based then you should be able to do something like this from the console:

如果你的项目是基于ant的那么你应该能够从控制台做这样的事情:

ant test

If this doesn't work, but still your project is ant based, you can run ant -p to list the main targets of the project.

如果这个方法不起作用,但是您的项目仍然是基于ant的,那么您可以运行ant -p来列出项目的主要目标。