How to execute ant using java and capture output?

I have an ant build file that contains a JUnit test suite that I would like to execute. Currently, I just right-click and run the build file from Eclipse.

I want to write java code that can automatically execute the ant build file. So I just run the code and ant will execute.

Secondly, I want to record the test result. The result is currently based on a JUnit HTML report. I want to make my own simple test report. I read that there is a JUnitResultFormatter, but I cannot find step by step how to use it. Can someone point me a link?

+5
source share
1 answer

The easiest way to do this is to use a class JunitCorefrom java. It is not recommended to directly contact mainfrom ant, see "Junit Faq and http://www.answerspice.com/c119/1497833/how-do-i-run-junit-tests-from-inside-my-java-application .

It often happens that for each test case you can define the same one so that you can run tests separately from the command line. I usually also modify the logging options in these methods to get more information when I run one test manually than from ant.


To create a custom report, you will need to execute , which will create your report and register it, as described in javadoc: RunListener

public void main(String... args) {
  JUnitCore core= new JUnitCore();
  core.addListener(new RingingListener());
  core.run(MyTestClass.class);
}

, , , .

+3

All Articles