如何从命令行运行Java的JUnit测试 [英] How to run JUnit tests for Java from the command line

查看:903
本文介绍了如何从命令行运行Java的JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何从命令行运行Junit测试用例?

Possible Duplicate:
How to run Junit testcases from command line?

我以前使用maven运行过JUnit测试.现在,我将所有源代码打包到一个JAR文件中,并希望使用java命令运行它.我怎样才能做到这一点?请注意,我的代码中没有主类.

I've run my JUnit tests using maven before. Now I'm packaging all my source code into a JAR file, and want to run it using a java command. How can I do that? Note that there is no main class in my code.

推荐答案

您需要确保classpath包含

You need to make sure the classpath contains

  1. 您的JAR
  2. JUnit JAR

您可以通过对java命令使用-cp标志来设置类路径.然后,您可以使用 junit.textui.TestRunner 来运行测试.

You can set the class path by using the -cp flag to the java command. Then you can use junit.textui.TestRunner to run the tests.

如果您使用的是Linux(请注意,将:用作jar之间的路径分隔符)

If you're using Linux (note the use of : as the path separator between jars)

java -cp /path/to/my.jar:/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests

如果您使用的是Windows(请注意,将;用作jar之间的路径分隔符)

If you're using Windows (note the use of ; as the path separator between jars)

java -cp /path/to/my.jar;/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests

这篇关于如何从命令行运行Java的JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆