Salesforce Cloud deployment – Running Specified Tests during deployment

There are multiple ways to deploy metadata in Salesforce cloud. Below are the multiple ways to perform Salesforce cloud deployments.

1. Change sets

2. ANT Migration tool

3. Force.com migration tool

In each Salesforce cloud deployment, all test class will execute. It is very simple,If you Salesforce org contains few Apex test classes. But it is really very difficult & time consuming if your Salesforce org contains lot of Apex test classes. This is really time consuming process because we have  to run all test classes to deploy deploy single component. Salesforce has provided simple process to make Salesforce cloud deployment very simple with Summer 15 release. With this option we can specified test classes during deployment. This option is available only for force.com migration tool, not for change sets.

Use testLevel attribute in your build.xml target.

Use testLevel=”RunSpecifiedTests” to run Specified Tests & Use testLevel=”RunLocalTests” to run all your local test classes.

To run specified tests during Salesforce cloud deployments use below format:

<targetname="deployCode">
   <sf:deployusername="${sf.username}"password="${sf.password}" 
          sessionId="${sf.sessionId}"serverurl="${sf.serverurl}"
          deployroot="codepkg" testLevel="RunSpecifiedTests">
        <runTest>TestClass1</runTest>
        <runTest>TestClass2</runTest>
        <runTest>TestClass3</runTest>
   </sf:deploy>
</target>

To run all your local test classes use below format. This only for Sanbox as by default local test will run if you are not specified all test classes like above.

<targetname="deployCode">
   <sf:deployusername="${sf.username}"password="${sf.password}" 
          sessionId="${sf.sessionId}"serverurl="${sf.serverurl}"
          deployroot="codepkg" testLevel="RunLocalTests">
   </sf:deploy>
</target>