Choose a deploy test level
Security & deployment · Beginner · 10 min · Runs in your Salesforce Developer org · Apex Path
Return the Metadata API test level for a change. Apex uses specified tests, profile and layout use local tests, and anything else, including null, uses every test in the org.
Lesson
Why this matters
A production deploy without tests can ship a broken class. The Metadata API testLevel chooses how many tests run. Apex-only changes can name their tests. A profile or layout change should run the org's local tests. When the risk is unknown, run everything, including managed packages.
The smallest working example
if (change == 'apex') {
return 'RunSpecifiedTests';
}
if (change == 'profile' || change == 'layout') {
return 'RunLocalTests';
}
return 'RunAllTestsInOrg';
String == ignores case, so 'Apex' matches 'apex'.
Name the parts
- **
RunSpecifiedTests** — only the tests you name. Use it for Apex. - **
RunLocalTests** — every test in the org except managed packages. Use it for profile and layout. - **
RunAllTestsInOrg** — every test, including managed packages. The default, including null and blank. - **
NoTestRun** is not a passing answer here.
A common wrong version
if (change.equals('apex')) {
return 'RunSpecifiedTests';
}
equals throws when change is null. == does not.
The fix
Use == for the named cases. Return RunAllTestsInOrg from the last line so null and blank are covered.
Now you try
Finish testLevelFor. Keep the method name and the String parameter.
Learn more
The challenge
Write testLevelFor(String change) that returns the Metadata API test level name for that change.
Examples:
testLevelFor('apex')→RunSpecifiedTeststestLevelFor('profile')→RunLocalTeststestLevelFor('unknown')or null →RunAllTestsInOrg
Requirements:
testLevelFor('apex')should beRunSpecifiedTests.testLevelFor('profile')should beRunLocalTests.testLevelFor('layout')should beRunLocalTests.testLevelFor(null)should beRunAllTestsInOrg.testLevelFor('Apex')should beRunSpecifiedTests.
Starter code
String testLevelFor(String change) {
return null;
}
Related drills
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.