Sub topic 5
NoTestRun: skipping tests
This chapter assumes you can read basic Apex and the @IsTest annotation. It also assumes you know the difference between a sandbox and production, and that you have run sf project deploy start before. The chapter covers the lowest of the four test levels, NoTestRun.
What NoTestRun does
NoTestRun deploys your metadata and runs no Apex tests. The deployment still validates the metadata itself. The compiler checks your Apex classes and triggers, and missing dependencies still fail the deploy. Only the test execution step is skipped.
This makes it the fastest level. In a large org, a full test run can take many minutes or hours. Skipping it turns a deploy into a matter of seconds.
Running it from the CLI
Pass the level with the --test-level flag:
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunYou do not need --tests. That flag belongs to RunSpecifiedTests, covered in a later chapter.
Where it is allowed
NoTestRun is meant for development environments such as sandboxes, scratch orgs, Developer Edition orgs and trial orgs. In these orgs it is also the default when you set no level, so the command above behaves the same without the flag.
Production is different. A production deployment that contains Apex classes or triggers does not accept NoTestRun, and the deployment is rejected. Production deployments that contain Apex default to RunLocalTests, which later chapters cover. A production deployment with no Apex components, for example only page layouts or custom fields, runs no tests.
Coverage and NoTestRun
The 75% code coverage rule is checked against test results from the deployment. With NoTestRun, no tests run, so there are no results and the deployment does not enforce the 75% rule. Your code can have no test at all and still deploy to a sandbox.
This does not mean coverage no longer matters. The rule is applied later, when the same code is promoted to production. If a class has no test, you find out at the production deployment, which is the worst time to find out.
When to use it
Use NoTestRun when you want fast feedback and the tests add nothing to the question you are asking:
- Pushing changes to your own sandbox while you iterate on a feature.
- Deploying metadata that has no logic, such as labels or layouts, to a development org.
- Refreshing a scratch org from source before you start work.
Avoid it for any stage that decides whether code is ready for production. A continuous integration (CI) job that exists to catch broken code should run tests, using one of the other levels.
Common mistakes
- Using
NoTestRunin a script that targets production. If the package contains Apex, the deployment fails, so a script that worked against a sandbox breaks at release time. - Assuming that a sandbox deploy with
NoTestRunproves the tests pass. No test ran, so nothing was proven. - Adding
--tests AccountServiceTestalongsideNoTestRunand expecting that class to run. Named tests belong toRunSpecifiedTests.
Exercise
A teammate deploys a package that contains an Apex class and its test class to production with --test-level NoTestRun. What happens, and which level would you use instead?
Show answer
The deployment is rejected, because production does not accept NoTestRun when the deployment contains Apex. Use RunLocalTests, which is the default for such deployments, or RunSpecifiedTests with the relevant test classes.