简体   繁体   English

从命令行进行JUnit测试

[英]JUnit test from commandline

I am trying to run a JUnit 4 test from commandline. 我正在尝试从命令行运行JUnit 4测试。 This is my current command: 这是我当前的命令:

java -cp C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner C:\Some\Path\target\test-classes\com\wicket\range\ui\MyTest.class

This gives a class not found error. 这给出了一个类未找到错误。

I have also tried the following: 我也尝试了以下方法:

C:\Some\Path\target\test-classes>java -cp C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner com.wicket.range.MyTest.class

This also gives a class not found error; 这也给出了一个类未找到的错误。 what could be the issue here? 这可能是什么问题?

I assume your test class is under C:\\Some\\Path\\target\\test-classes (in appropriate subdirectory). 我假设您的测试类在C:\\ Some \\ Path \\ target \\ test-classes下(在适当的子目录中)。 Your command has only junit in it. 您的命令中只有junit。 It also need class path to the test and other dependencies. 它还需要测试的类路径和其他依赖项。

Try 尝试

java -cp C:\Some\Path\target\test-classes;C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner com.wicket.range.MyTest.class

Looks like you are using Maven (saw the ".m2" in your classpath). 看起来您正在使用Maven(在类路径中看到了“ .m2”)。 How about this.. 这个怎么样..

cd <location of pom.xml>
mvn -Dtest=MyTest test

Granted it may be a while until before it runs your test and it's not going to use JUnit's text ui runner, but it should run your test without much fuss about ClassNotFoundException. 在运行测试之前可能要花一点时间,并且它不会使用JUnit的文本ui运行器,但是它应该运行您的测试而不必大惊小怪。 Examine files in target\\surefire-reports for test results afterwards. 之后检查target \\ surefire-reports中的文件以获取测试结果。 Guess it depends on exactly what your goal is. 猜猜这完全取决于您的目标是什么。

Otherwise, Jayan's answer looks about right to me. 否则,Jayan的答案对我来说是正确的。 For his to work, I think you want to first 为了他的工作,我想你要先

cd C:\Some\Path\target\test-classes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM