测试类中不需要定义main函数,不能直接用 命令( java [classname] )来运行,以下摘录了JUnit4.8.2 cookbook 中的介绍:
How do you run your tests and collect their results?
Once you have tests, you'll want to run them. JUnit provides tools to define the suite to be run and to display its results. To run tests and see the results on the console, run this from a Java program:
org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);
or this from the command line, with both your test class and junit on the classpath:
java org.junit.runner.JUnitCore TestClass1.class [...other test classes...]
You make your JUnit 4 test classes accessible to a TestRunner designed to work with earlier versions of JUnit, declare a static method suite that returns a test.
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(Example.class);
}
本文介绍了如何使用JUnit进行单元测试的运行及结果收集。通过Java程序或命令行方式,可以运行测试类并查看输出结果。此外,为了兼容旧版本的JUnit测试运行器,需要在JUnit4测试类中定义静态suite方法。
841

被折叠的 条评论
为什么被折叠?



