如何在Salesforce Release Pipeline中配置Test Class执行及跳过指定部署测试
Got it, let's walk through exactly what you need to do here—both skipping tests for your UAT deployment and enabling them later when you're ready to fix the test class.
一、部署时跳过Test Class执行
Since your dev team wants to skip the test class for this UAT deployment, here are the common ways to do it depending on your deployment tool:
1. 使用Salesforce CLI
If you're deploying via the CLI, use the NoTestRun flag to skip all tests entirely. Run this command (adjust the path to match your class files):
sfdx force:source:deploy -p ./force-app/main/default/classes -l NoTestRun
Just a heads-up: This is only allowed in sandbox environments (like UAT/QA). Production deployments will never let you skip tests, so make sure you plan to fix that test class before any production push.
2. 使用Salesforce Setup界面(变更集或直接部署)
If you're using the standard Setup UI (e.g., deploying a change set or uploading files directly):
- When you reach the "Test Options" step during deployment, select Run no tests from the dropdown menu.
- Note: Some sandboxes might have restrictions, but UAT environments typically allow this option for temporary deployments.
二、启用Test Class执行的配置方法
When you're ready to run tests (either to fix the test class or for future deployments), here's how to enable test execution:
1. CLI部署时启用测试
You have a few options here depending on what you need:
- Run all local tests (tests in the target org that cover your deployed code):
sfdx force:source:deploy -p ./force-app/main/default/classes -l RunLocalTests - Run a specific test class (great for testing just the fixed test):
sfdx force:source:deploy -p ./force-app/main/default/classes -l RunSpecifiedTests -r YourBrokenTestClassName - Run all tests in the org (overkill for most deployments, but useful for full validation):
sfdx force:source:deploy -p ./force-app/main/default/classes -l RunAllTestsInOrg
2. Setup界面启用测试
In the Setup deployment flow:
- At the "Test Options" step, choose either Run all tests (to validate all Apex in the org) or Run specified tests (then select the test class you want to execute).
- For organization-wide settings that enforce test runs, you can check this in Setup > Deployment Settings—look for the option "Require Apex tests to be run when deploying to production" (this is enabled by default for production orgs, but you can adjust sandbox settings here if needed).
Important Note
Skipping tests should always be a temporary workaround. Make sure to prioritize fixing that test class soon—Apex tests are critical for maintaining code quality, ensuring your classes work as expected, and meeting Salesforce's code coverage requirements for production deployments.
内容的提问来源于stack exchange,提问作者user7345254

