TFS 2010: Send Email after Test Run

Lab Management in TFS 2010 provides the ability to build, deploy and test in an automated fashion. However, the most important thing which is lacking is the ability to send the test results in an email. You can configure an alert for the build definition which uses the LabDefaultTemplate but users will have to click on a link to view the build logs. Moreover, the build logs seem to be missing when you configure an alert for a build using LabDefaultTemplate, refer to https://connect.microsoft.com/VisualStudio/feedback/details/621142/blank-summary-is-displayed-on-the-link-received-in-the-email-notification-for-builds-using-labdefaulttemplate

The best way to send email after the test run is to customize the LabDefaultTemplate.

  1. Open LabDefaultTemplate.xml in Visual Studio
  2. Go to Arguments and add the following arguments:
    1. SendEmail: Boolean
    2. EmailAddress: String
    3. EmailScriptPath: String
  3. Go to Toolbox, “Primitives” section and add the “Delay” component with value as New TimeSpan(0, 1, 0)
  4. Go to Toolbox, "Control Flow" and add the If Component with condition as SendEmail
  5. Go to Toolbox, “Team Foundation Build Activities” section and add the ���InvokeProcess” component inside the If Then Block with the following values:
    1. FileName: EmailScriptPath
    2. Arguments: BuildDetail.BuildNumber + " " + EmailAddress

Here is how the template looks:

While creating the build definition using this template, you will have to specify the following:
Send Email (True/False), EmailAddress, EmailScriptPath

Your Email Script can be a PowerShell script like this:

Param(
      $BuildNumber=$(throw "BuildNumber required"),
      $EmailTo=$(throw "EmailTo required")
      )
     
    $smtpServer=$("smtphost.redmond.corp.microsoft.com")
    $url="/ReportServer?%2fTfsReports%2fDefaultCollection%2f%2fTests%2fTestResults&rs:Command=Render&rs:Format=mhtml&rc:Parameters=false&Build=$BuildNumber">http://<;server>/ReportServer?%2fTfsReports%2fDefaultCollection%2f<teamproject>%2fTests%2fTestResults&rs:Command=Render&rs:Format=mhtml&rc:Parameters=false&Build=$BuildNumber"
    echo $url
    $destination="results-" + $BuildNumber + ".mhtml"
    $clnt = new-object System.Net.WebClient
    $clnt.UseDefaultCredentials = $true
    $clnt.DownloadFile($url,$destination)
    $subject="Test results for "+ "$BuildNumber"
    echo $EmailTo
    .\smartmail.exe smtp auth:sspi server:$smtpServer from:#mail to:$EmailTo content-type:text/mhtml raw:$destination subject:$subject

This script is using a tool called smartmail.exe to send an email. It downloads a report and sends it as email body. The report can be a default TFS report or a custom report. To generate a custom report, refer to http://social.technet.microsoft.com/wiki/contents/articles/creating-reports-for-tfs-2010-test-results.aspx

The important things to consider:

  1. The email is sent from the account under which the build agent is running. Hence this account should have a mailbox.
  2. The PowerShell script runs from the build agent, hence you will have to run the following command to enable running scripts: Set-ExecutionPolcy RemoteSigned

Here is the template for sending email after test run: http://cid-6e7347d6ac73603f.office.live.com/self.aspx/.Public/TFS2010/LabDefaultTemplateWithEmail.xaml 

Above PowerShell script is not working at my end. Can you please give us a working script?

Going ahead using a Lab process setting. Ther are four options:

  1. Environment--Here is the env which is created from Lab center.
  2. Build--Which build here do we need to specify? It's a dev build on which we normally test or else it is the build which we have created for coded UI to deploy the solution.
  3. Deploy--Can you provide us what is deployment script? Or more details of it?
  4. Test--Specified --Plan and suite on which test to be run.

Let us know if we are missing something. 


See Also