Java – unconditionally execute tasks in ant?

I'm trying to define the task of sending (using echo) messages when the target completes execution, whether the target is successful or not Specifically, the target executes a task to run some unit tests. I want to send a message indicating where the results can be used:

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

Unfortunately, if the test fails, the task fails and execution aborts Therefore, the message is output only when the test passes - contrary to what I want I know I can put the task before the task, but it makes it easier for users to miss this message What am I trying to do?

Update: facts have proved me stupid There is a haltonfailure = "true" task in my < TestNG >, which explains the behavior I see The problem now is that setting this to false will lead to the success of the entire ant build. Even if the test fails, this is not what I want The following answers using this task may seem to be what I want

Solution

The solution to your problem is to use the failureproperty with the haltonfailure property of the TestNG 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, elsewhere, when you want the build to fail, add ant code as follows:

<target name="doSomethingIfTestsWereSuccessful" unless="tests.Failed">
   ...
</target>

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

You can then call dosomethingeiftestsfailed. Where you want your ant build to fail

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>