Newer Version Available
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, set the RunSpecifiedTests test level on the DeployOptions object. Next, specify each test class to run in DeployOptions. Finally, pass DeployOptions as an argument to the deploy() call. The following example performs those steps to run only the specified test classes.
1// Create the DeployOptions object.
2DeployOptions deployOptions = new DeployOptions();
3
4// Set the appropriate test level.
5deployOptions.setTestLevel(TestLevel.RunSpecifiedTests);
6
7// Specify the test classes to run.
8// String array contains test class names.
9String[] tests = {"TestClass1", "TestClass2", "TestClass3"};
10// Add the test class names array to the deployment options.
11deployOptions.setRunTests(tests);
12
13// Call deploy() by passing the deployment options object as an argument.
14AsyncResult asyncResult = metadatabinding.deploy(zipBytes,deployOptions);Notes About Running Specific Tests
- You can specify only test classes. You can’t specify individual test methods.
- We recommend that you refactor test classes to include the minimum number of tests that meet code coverage requirements. Refactoring your test classes can contribute to shorter test execution times, and as a result, shorter deployment times.
- You can deactivate a trigger in the target organization by deploying it with an inactive state. However, the trigger must have been previously deployed with an active state.