Knowledge Base Article

Using sendmail for test failure alerts

I use TestExecute on a VM frequently for long running test projects.  This frees up my workstation for other tasks.  However I have to wait for it to complete to see my results or set the logging so the logs are periodically saved and then copy the logs to my local PC for import with TestComplete to see a partial log and get an idea of how things are going.  In most cases I will wait for TestExecute to complete the test run and then I will analyze the results, submit my reports and begin to correct any test code failures.  I may have dozens of tests to go through at the end of a run.

 

I coded a few functions into the event handlers to use sendmail to email me when a test fails so I can follow along with TestExecute in real time.  This way I can see when things are going horribly wrong and stop the run or analyze test failures as they occur to speed up my turnaround time in test reporting.  Below is an example of what I came up with.  

 

You should consult your I.T. department about the use of port 25 on an internal mail server. 

This how the SendMail function works. 

It sends mail through your.mailserver.com via port 25 to a valid recipient address. 

 

SendMail:

https://support.smartbear.com/testcomplete/docs/scripting/sending-email-from-scripts.html#SendMail 

 

aqTestCase.CurrentTestCase.Name will return the name of the running test if run from the execution plan.  This is how TestExecute works so that is perfect.  If this is run from TestComplete you will need to run your tests from the Execution Plan get this information.  Really though if you are only running tests one at a time you don't need this information.  I have wrapped the 'Event_Email' function in an if block tied to a project variable (Project.Variables.bEvent_Email) so it can be 'switched on' prior to a  long run where you want notifications.  I also added a project variable to allow setting a recipient address (Project.Variables.strEmail_Rcpt).

 

aqTestCase.CurrentTestCase:

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqtestcase/currenttestcase.html

https://support.smartbear.com/testcomplete/docs/reference/program-objects/testcase/name.html 

 

You could also hard code a function call in a test script if you always want an email notification for a particular test(s).  You may need to make some modifications here but the SendMail function in TestComplete has been really useful for me.  I hope you find this information useful as well.

 

Example:

function EventControl_OnLogError(Sender, LogParams)
{
    Email_Event("Test Case: ");
}

function Email_Event (str_Message = "No Test Message Supplied. ")
{
  if (Project.Variables.bEvent_Email)
  {
    SendEmail(  anyone@anyplace.bla
                Project.Variables.strEmail_Rcpt,
                
                /*Mail Server*/ 
                "mail.server.com",
                /*Senders Name*/ 
                "TestComplete",
                /*Senders Address*/ 
                "Testing@test.com",
                /*Subject*/ 
                "AppName, Test Case: " + aqTestCase.CurrentTestCase.Name,
                /*Body*/ 
                str_Message + aqTestCase.CurrentTestCase.Name + "' failed " +
                " Running on: " + Sys.HostName);
  }     
}

function SendEmail(str_RcptAddr, str_Server, str_SenderName, str_SenderAddr, str_Subj, str_Body, str_Files = null)
{
  var b_Result = false;
  if (str_Files == null)
  {  
    if (SendMail(str_RcptAddr, str_Server, str_SenderName, str_SenderAddr, str_Subj, str_Body))
    {
        Log["Message"]("Mail was sent");
        var b_Result = true;
    }
    else
    {
        Log["Warning"]("Mail was not sent");
        var b_Result = false;
    }
  }
  else
  {
    if (SendMail(str_RcptAddr, str_Server, str_SenderName, str_SenderAddr, str_Subj, str_Body, str_Files))
    {
        Log["Message"]("Mail was sent");
        var b_Result = true;
    }
      else
    {
        Log["Warning"]("Mail was not sent");
        var b_Result = false;
    }
  }
} 

 

Updated 3 years ago
Version 1.0
No CommentsBe the first to comment