[SalesForce] How to deploy with Ant and run tests but ignore code coverage

My team sometimes has a release process that requires deploying multiple packages in stages. We want to make sure that the system remains stable at the end of each of these deployments by running a few sanity tests, but we don't care about the code coverage until we deploy the last package.

Is there a way to run an <sf:deploy> target that runs a few test classes (and fails if those tests fail), but does not fail if code coverage is not reached for the deployed classes?

Best Answer

By default, no tests are run in a deployment to a non-production organization, such as a sandbox or a Developer Edition organization.

To specify tests to run in your development environment, set a testLevel deployment option. For example, to run local tests in a deployment and to exclude managed package tests, add the testLevel="RunLocalTests" parameter to the deploy target.

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

Running a Subset of Tests in a Deployment

If the code coverage of an Apex component in the deployment is less than 75%, the deployment fails. If one of the specified tests fails, the deployment also fails. We recommend that you test your deployment in sandbox first to ensure that the specified tests cover each component sufficiently. Even if your organization’s overall code coverage is 75% or more, the individual coverage of the Apex components being deployed can be less. If the code coverage requirement isn’t met, write more tests and include them in the deployment. To run a subset of tests, add the testLevel="RunSpecifiedTests" parameter to the deploy target. Specify each test class to run for a deploy target in a child element within the sf:deploy element. Add the test class name within the tags. Add as many runTest tags as you need, one for each test class.

<target name="deployCode">
<sf:deploy username="${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>

Refer: Force.com Migration Tool Guide

Related Topic