Unconditionally complete a task in ant?

I am trying to identify a task that emits (using an echo) a message when a target completes, regardless of whether the target was successful or not. In particular, the goal performs the task of running some unit tests, and I want to convey a message about where the results are available:

<target name="mytarget"> <testng outputDir="${results}" ...> ... </testng> <echo>Tests complete. Results available in ${results}</echo> </target> 

Unfortunately, if the tests fail, the task fails and execution fails. Thus, the message is displayed only if the tests pass - opposite to what I want. I know that I can pose a task to a task, but it will make it easier for users to skip this message. Am I trying to do this?

Update: It turns out I'm stupid. I had haltOnFailure = "true" in my <testng> task, which explains the behavior that I saw. Now the problem is that setting this value to false leads to the successful completion of the ant build, even if the tests fail, which I don't want. The answer below, using the task, looks like I want ...

+6
java unit-testing ant
source share
5 answers

The solution to your problem is to use failureProperty in combination with the haltOnFailure property of the haltOnFailure task as follows:

 <target name="mytarget"> <testng outputDir="${results}" failureProperty="tests.failed" haltOnFailure="false" ...> ... </testng> <echo>Tests complete. Results available in ${results}</echo> </target> 

Then, in another place, when you want the build to fail, you add the following ant code:

 <target name="doSomethingIfTestsWereSuccessful" unless="tests.failed"> ... </target> <target name="doSomethingIfTestsFailed" if="tests.failed"> ... <fail message="Tests Failed" /> </target> 

Then you can call doSomethingIfTestsFailed, where you want your ant build to fail.

+4
source share

According to Ant docs, there are two properties that control whether the build process is stopped or not if the testng task does not work:

haltonfailure - stop the build process if a failure occurs during the test run. The default is false.

haltonskipped - stop the build process if there is at least a missed test. The default is false.

I cannot say from the fragment if you set this property or not. It might be worth trying to explicitly set haltonfailure to false if it is currently set to true.

In addition, if you use the <exec> functionality in Ant, there are similar properties to control what happens if the executed command is executed:

failonerror . Stop the build process if the command completes with a return code. alarm failure. The default is false.

failifexecutionfails . Stop the build if we cannot start the program. The default value is true.

It is not possible to specify based on a partial code snippet in your message, but I assume that the most likely culprit is failonerror or haltonfailure set to true.

+5
source share

You can use try-catch as follows:

 <target name="myTarget"> <trycatch property="foo" reference="bar"> <try> <testing outputdir="${results}" ...> ... </testing> </try> <catch> <echo>Test failed</echo> </catch> <finally> <echo>Tests complete. Results available in ${results}</echo> </finally> </trycatch> </target> 
+5
source share

Although you are showing a fake task called "testng" in your example, I assume that you are using the junit target.

In this case, it is strange that you see these results, because the default junit target does NOT cancel execution when the test fails.

There is a way to tell ant to stop the build with a junit error or an error using halt attributes, for example. haltonfailure:

 <target name="junit" depends="junitcompile"> <junit printsummary="withOutAndErr" fork="yes" haltonfailure="yes"> 

However, both haltonfailure and haltonerror are disabled by default. I suppose you can check your build file to see if any of these flags are set. They can even be installed globally, so one thing you could try is to explicitly set the "no" in your task to make sure that it is overridden if it is installed in the global scope.

http://ant.apache.org/manual/Tasks/junit.html

0
source share

Can you develop the testng task? If so, then you can use this function to have the testng task run on another JVM.

0
source share

All Articles