从Java运行JUnit测试

时间:2013-11-16 11:05:12

标签: java junit

我想从Java运行我的JUnit测试。

我用:

JUnitCore runner = new JUnitCore();
runner.addListener(new TextListener(System.out));
runner.run(AdditionTest.class);

但我想要测试的名称,结果(truefalse),失败

我是怎么做到的?

1 个答案:

答案 0 :(得分:2)

阅读documentation!当您调用run(...)时,JUnitCore会返回Result个对象。结果对象包含getFailures()getRunTime()以及wasSuccessful()等方法。

JUnitCore runner = new JUnitCore();
runner.addListener(new TextListener(System.out));
Result result = runner.run(AdditionTest.class);
boolean wasSuccessful = result.wasSuccessful();
System.out.println("tests were successful: " + wasSuccessful);