How to send email on test failure

1,597 views
Skip to first unread message

Unni

unread,
Mar 20, 2012, 8:54:58 AM3/20/12
to testng...@googlegroups.com

I want to send an email on Test Failure. While searching i got the below link which explain the task.

http://openwritings.net/public/ant/send-email-if-tests-failed-using-ant


But that is not working for me. Can anyone else help me out by checking this link.

My query is what is 'test.failed' and how it gets value from testng as said in the article. Is this the default value we need to provide or Do we need to give any other value? Also what is failonerror property means??

My execution does not provide me any info and not send a mail though i have a failure in the TestNG report.

Tomek Kaczanowski

unread,
Mar 20, 2012, 8:57:51 AM3/20/12
to testng...@googlegroups.com
1) Isn't it rather a job for CI server to send notifications?
2) If you really want TestNG to do the job, you can write your own
listener (see documentation)

--
Regards / Pozdrawiam
Tomek Kaczanowski
http://kaczanowscy.pl/tomek

2012/3/20 Unni <junis...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "testng-users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/testng-users/-/XImLBHPwEpYJ.
> To post to this group, send email to testng...@googlegroups.com.
> To unsubscribe from this group, send email to
> testng-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/testng-users?hl=en.

JohnBurgess

unread,
Mar 20, 2012, 12:11:11 PM3/20/12
to testng-users
Hi

test.failed is a property that will get set by testng if any of the
tests failed. The bit that sets it is

<!-- Your TestNG target -->
<target name="run_testng" >
<testng failureProperty="test.failed">
<!-- ... your TestNG instructions here... -->
</testng>
</target>

The failureProperty attribute of the testng task tells it what
property to set if a test fails. There's also a skippedProperty
element to let you know if any tests are skipped. See the
documentation on the ant task.

After the run_testng target you'd then want the email target to run IF
and ONLY IF there were failures. As the example showed, but with a
dependency that they'd forgotten:

<target name="sendMail" if="test.failed" depends="run_testng">
<mail mailhost="your.mail.smtp.com" mailport="25"
failonerror="false"
subject="Your subject here...">

<from address="fr...@company.com"/>

<to address="t...@company.com"/>
<to address="t...@company.com"/>
<to address="t...@company.com"/>
<to address="t...@company.com"/>

<message>Your message here....</message>

<attachments>
<fileset dir=".">
<include name="filename.txt"/>
</fileset>
</attachments>

</mail>
</target>

Unni

unread,
Mar 21, 2012, 8:38:24 AM3/21/12
to testng...@googlegroups.com
Dear John,

I have put the dependency too , yet its not working.. below is the part of my build file. I have added a target called "sendMail" and a 'failureproperty" attribute in testng tag of target 'run'. The target run was already available in my project. Also as you can see i am using reportNG... is it because of this i am not able to send email?

<target name="run" depends="compile">
    <!--
        testng classpath has been provided reference of jar files and compiled classes
    This will generate report NG report.
    -->
    <testng classpath="${test.classpath}:${classes.dir}" failureproperty="test.failed">
        <xmlfileset dir="${basedir}/config/ny" includes="testng.xml" />
    </testng>
</target>
   
      <!--
        The target sendMail will be executed only if test.failed is true.
          test.failed value come from <testng>, see run_testng target above.
          Therefore, email will be sent only if the test failed.
      -->

    <target name="sendMail" if="test.failed" depends="run">
        <mail mailhost="smtp.xxx.com" mailport="25" failonerror="false" subject="Your subject here...">
     
          <from address="x...@mm.com"/>
     
          <to address="x...@mm.com"/>

     
          <message>Your message here....</message>
          </mail>
       </target>

Thanks
Unni

unni

unread,
Apr 5, 2012, 6:42:15 AM4/5/12
to testng...@googlegroups.com
any finding??

jeevan

unread,
Apr 5, 2012, 8:12:44 AM4/5/12
to testng...@googlegroups.com

You need to use ITestListener class and  ontestfailure call mail API..
Thanks,
Jeevan
any finding??
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/umtnjUwOOuQJ.

swethanaik

unread,
Apr 24, 2012, 7:32:59 AM4/24/12
to testng...@googlegroups.com

I try to run scripts using testng, I have 4 packages. When i start the test
execution, it starts with first module and before it finishes, it starts
with second module test cases. Now again returns to first module test cases
and searches for components, cant find and it finallly fails. Is there any
way to keep the test execution happen in order specified.
--
View this message in context: http://old.nabble.com/How-to-send-email-on-test-failure-tp33538572p33739011.html
Sent from the testng-users mailing list archive at Nabble.com.

swethanaik

unread,
Apr 24, 2012, 7:33:02 AM4/24/12
to testng...@googlegroups.com

I try to run scripts using testng, I have 4 packages. When i start the test
execution, it starts with first module and before it finishes, it starts
with second module test cases. Now again returns to first module test cases
and searches for components, cant find and it finallly fails. Is there any
way to keep the test execution happen in order specified.
--
View this message in context: http://old.nabble.com/How-to-send-email-on-test-failure-tp33538572p33739012.html

swethanaik

unread,
Apr 24, 2012, 7:33:05 AM4/24/12
to testng...@googlegroups.com

I try to run scripts using testng, I have 4 packages. When i start the test
execution, it starts with first module and before it finishes, it starts
with second module test cases. Now again returns to first module test cases
and searches for components, cant find and it finallly fails. Is there any
way to keep the test execution happen in order specified.
--
View this message in context: http://old.nabble.com/How-to-send-email-on-test-failure-tp33538572p33739014.html

swethanaik

unread,
Apr 24, 2012, 7:33:07 AM4/24/12
to testng...@googlegroups.com

I try to run scripts using testng, I have 4 packages. When i start the test
execution, it starts with first module and before it finishes, it starts
with second module test cases. Now again returns to first module test cases
and searches for components, cant find and it finallly fails. Is there any
way to keep the test execution happen in order specified.
--
View this message in context: http://old.nabble.com/How-to-send-email-on-test-failure-tp33538572p33739015.html

Krishnan Mahadevan

unread,
Apr 24, 2012, 8:17:17 AM4/24/12
to testng...@googlegroups.com
Use preserve-order=true ?

Can you show us your testng suite file so that someone can provide a much more apt answer ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


--
You received this message because you are subscribed to the Google Groups "testng-users" group.

Cédric Beust ♔

unread,
Apr 24, 2012, 1:46:15 PM4/24/12
to testng...@googlegroups.com
On Tue, Apr 24, 2012 at 5:17 AM, Krishnan Mahadevan <krishnan.ma...@gmail.com> wrote:
Use preserve-order=true ?

Note that this is the default, unless he's using a pretty old version of TestNG.

-- 
Cédric

swethanaik

unread,
May 2, 2012, 7:09:53 AM5/2/12
to testng...@googlegroups.com

Hi,

I have tried using preserve order but it doesn't work. I also tried using
methods in classes.

<test name="Pos" >
<classes>
<class name="com.abc.xyz.Pos">
<methods>
<include name="Pos001" />
<include name="Pos002" />
<include name="Pos003" />
<include name="Pos004_Pos031" />
<include name="Pos006" />
<include name="Pos007" />
<include name="Pos008" />
<include name="Pos009" />
</methods>
</class>

I uninstalled latest 6.4 testng and installed 6.1 but it didnt help. I try
to upgrade to later version but it shows error message. Any idea on how to
solve this issue?


Regs,
Swetha
http://old.nabble.com/file/p33763369/testng.txt testng.txt
--
View this message in context: http://old.nabble.com/How-to-send-email-on-test-failure-tp33538572p33763369.html

Krishnan Mahadevan

unread,
May 2, 2012, 8:18:52 AM5/2/12
to testng...@googlegroups.com
Swetha,
I think preserve-order="true" is applicable only for <classes></classes>


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


Cédric Beust ♔

unread,
May 2, 2012, 1:45:24 PM5/2/12
to testng...@googlegroups.com
preserve-order should work for methods as well.

Swetha: can you create a small self-contained project that I can build to reproduce the issue?

Thanks.

-- 
Cédric

Reply all
Reply to author
Forward
0 new messages