junit 和 ant 问题。无法开始测试
当我从 ant 运行 junit 测试时,我总是得到:
D:\metrike>ant test
Buildfile: build.xml
init:
compile:
test:
[junit] Running jmt.test.TestCodeBase
[junit] Testsuite: jmt.test.TestCodeBase
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0,046 sec
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0,046 sec
[junit]
[junit] Testcase: warning(junit.framework.TestSuite$1): FAILED
[junit] No tests found in jmt.test.TestCodeBase
[junit] junit.framework.AssertionFailedError: No tests found in jmt.test.TestCodeBase
[junit]
[junit]
[junit] Test jmt.test.TestCodeBase FAILED
这是 ant 文件:
<target name="test" depends="compile">
<mkdir dir="target/test-results"/>
<junit haltonfailure="no" printsummary="on">
<classpath >
<pathelement location="target/classes"/>
<pathelement location="Libraries/junit3.8.1/junit.jar"/>
</classpath>
<formatter type="brief" usefile="false"/>
<formatter type="xml" />
<batchtest todir="target/test-results" >
<fileset dir="target/classes" includes="**/TestCodeBase.class"/>
</batchtest>
</junit>
</target>
但是当我手动运行测试时,junit 测试有效:
D:\metrike>cd target
D:\metrike\target>cd classes
D:\metrike\target\classes>java jmt.test.TestCodeBase
fatsource.jar eclapsed : 2297 ms
over all : 2297 ms
contains 3073 classes and 3700 referred classes, 35968 referred methods, 22351 referred fields
Memory usage: 21326 KB
Post gc-memory usage: 19506 KB
contains 3073 classes and 3700 referred classes, 35968 referred methods, 22351 referred fields
有人可以告诉我我做错了什么吗?我一整天都在尝试解决这个问题,但找不到解决方案。
When I am running a junit test from ant I always get:
D:\metrike>ant test
Buildfile: build.xml
init:
compile:
test:
[junit] Running jmt.test.TestCodeBase
[junit] Testsuite: jmt.test.TestCodeBase
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0,046 sec
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0,046 sec
[junit]
[junit] Testcase: warning(junit.framework.TestSuite$1): FAILED
[junit] No tests found in jmt.test.TestCodeBase
[junit] junit.framework.AssertionFailedError: No tests found in jmt.test.TestCodeBase
[junit]
[junit]
[junit] Test jmt.test.TestCodeBase FAILED
This is the ant file:
<target name="test" depends="compile">
<mkdir dir="target/test-results"/>
<junit haltonfailure="no" printsummary="on">
<classpath >
<pathelement location="target/classes"/>
<pathelement location="Libraries/junit3.8.1/junit.jar"/>
</classpath>
<formatter type="brief" usefile="false"/>
<formatter type="xml" />
<batchtest todir="target/test-results" >
<fileset dir="target/classes" includes="**/TestCodeBase.class"/>
</batchtest>
</junit>
</target>
But when I manually run the test, junit test works:
D:\metrike>cd target
D:\metrike\target>cd classes
D:\metrike\target\classes>java jmt.test.TestCodeBase
fatsource.jar eclapsed : 2297 ms
over all : 2297 ms
contains 3073 classes and 3700 referred classes, 35968 referred methods, 22351 referred fields
Memory usage: 21326 KB
Post gc-memory usage: 19506 KB
contains 3073 classes and 3700 referred classes, 35968 referred methods, 22351 referred fields
Can someone please tell me what I am doing wrong? I have been trying to fix this for a whole day but I cannot find the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)jmt.test.TestCodeBase是否扩展TestCase(junit.framework.TestCase)?
如果没有,则需要由 junit ant 任务拾取。
2)该类是作为junit TestCase编写的,还是只是从main方法中调用的?有关以 Junit3 风格编写简单测试的示例,请参阅此链接。对于 Junit4,只需在方法上方添加 @Test 即可。
3)测试方法是Junit3风格(每个方法都以test开头)还是Junit4风格(每个方法上面都有一个@Test)?
如果是 Junit3,你应该可以开始了。如果是 Junit4,您需要在 ant 类路径中包含 Junit4 测试库,而不是使用 junit3.8.1。
1) Does jmt.test.TestCodeBase extend TestCase (junit.framework.TestCase)?
If not, it will need to to be picked up by the junit ant task.
2) Is the class written as a junit TestCase, or is it just called from the main method? See this link for an example of writing simple tests in Junit3 style. For Junit4, just add @Test above the methods.
3) Are the test methods in Junit3 style (every method starts with test) or Junit4 style (every method has a @Test above it)?
If Junit3, you should be good to go. If Junit4, you need to include the Junit4 test library in your ant classpath, rather than using junit3.8.1.
看来您的测试类实际上并不是 JUnit 测试类。当您手动运行它时,您不是将其作为测试运行,而是作为常规 Java 应用程序运行。该类有一个 main 方法,对吗?要作为 JUnit 3(您似乎正在使用)测试运行,该类需要扩展 TestCase 并具有一个或多个名称以“test”开头的公共 void 方法。为了进行测试,我会尝试在 IDE 中将类作为 JUnit 测试运行。
It seems your test class is not actually a JUnit test class. When you run it manually, you are not running it as a test, but as a regular Java application. The class has a main method, right? To run as a JUnit 3 (which you seem to be using) test, the class needs to extend TestCase and have one or more public void methods whose names start with 'test'. For testing, I would try running the class as a JUnit test in the IDE.
请发布 jmt.test.TestCodeBase 的示例代码,特别是。类定义和测试方法之一。
我看起来像您使用
public static int main()
而不是 JUnit 约定public void testFoo()
方法。如果您使用 JUnit4,您的测试方法应该具有@Test
注释。JUnit 测试通常不能仅使用
java
运行Please post sample code of jmt.test.TestCodeBase, esp. class definition and one of the test methods.
I looks like you use
public static int main()
instead of JUnit conventionpublic void testFoo()
methods. If you're using JUnit4, your test methods should have the@Test
annotation.JUnit tests usually cannot be run just with
java <Testclass>