The Test Framework in Salesforce is important to make sure that our applications do not break functionality and works as expected.
After read this post you will learn to describe the main features and functionality of the test framework in Salesforce. In addition, the considerations and options available for running selected tests when deploying Apex components.
Why Test Framework in Salesforce?
- To ensure sucessful long-term development and that the application works as expected.
- Also, to ensure that any changes and extensions to an application do not break functionality.
- Is required to deploy a Salesforce application to customer.
Features of the Test Framework in Salesforce
- Write Unit Test for Apex classes and triggers.
- Run Unit Test or a bunch of running tests together.
- Check Test Results whether tests succeed or fail in the Developer Console.
- Generate Code Coverage for code every time a test is run.
Deployment Requirements
- Overall code coverage in an org must be 75% for successful deployment.
- Apex classes and triggers should be compiled successfully.
- Test classes and methods are not included from the code coverage requirement.
Unit Tests of Test Framework in Salesforce
- Are created and executed to help facilitate error-free code.
- Can test code on a single record as well as on multiple records.
- Allows to having a suite of regression tests to re-run every time code is updated.
- Test suites (collection of tests) can be created to execute multiple test classes.
- Built-in tools are provided to allow unit test execution and analysis.
- Includes support for loading test data from different sources.
- Are executed without throwing any uncaught or limit exceptions.
- Apex code executed in test methods are subject to governor limits.
- Code coverage is generated for Apex classes and triggers each time a test is run.
- All Apex trigger is required to have at least 1 line of code coverage.
- Parallel Apex Testing enables executing unit test in parallel and helps speed up run time.
Unit Test Limitations
- Cannot send outbound emails. Apex code that sends an email message runs successfully, but not email is actually sent out from the org.
- Unit tests cannot perform HTTP callouts. In order to test Apex callouts can use mock responses that are created by using the HttpCalloutMock interface.
Running Selected Tests
Selected tests is a testing option which allows a specifying the only test classes to execute.
- To clarify the coverage of some Apex code.
- The coverage is computed individually for each class and trigger and is different from the overall code coverage percentage.
- Running a subset of tests results to quicker test runs.
- Only for test classes, not for invididual test methods.
Test Options for Change Sets
To deploy code from Sandbox to Production we can use Change Sets. However, when we use it, four test options are available.
- Default: The default option in Production is run local tests. On the other side, in sandbox, no tests are executed.
- Run Local Test: Run all tests except Managed Package Tests.
- All Tests: Run all tests including Managed Package Tests.
- Specified Tests: Test classes to execute are specified in a list.
More Information about Test Framework in Salesforce
- What are Apex Unit Tests?
- What to Test in Apex
- Get Started withi Apex Unit Tests
- Choose Which Tests to Run in a Deployment
- Choose Test Options for Change Sets
- Running a Subset of Tests in a Deployment
- Code Coverage Best Practices
To learn more about Salesforce can go to Static and Instance in Apex Salesforce