This topic describes the testing framework in Salesforce and its main features that can be utilized to ensure successful long-term development and success of the application.
When an application or managed package that you are releasing on AppExchange in Salesforce need to be deployed to customers, testing plays a key role and the testing framework makes it possible to validate that it works as expected.
It is also used to ensure that any changes and extensions to an application do not break functionality. In Salesforce, testing is required for successful deployment in production.
There are two ways of testing an application. One is through the Salesforce user interface, important, but merely testing through the user interface will not catch all of the use cases for your application. The other way is to test for bulk functionality: up to 200 records can be passed through your code if it’s invoked using SOAP API or by a Visualforce standard set controller
- Overall code coverage In organization must be 75% for successful deployment.
- Apex classes and triggers must be compiled successfully.
- Test classes and Test methods are not part of code coverage.
- Test methods can be modified and added to increase code coverage.
- Modifications to metadata should match in sandbox and production
The testing framework provides a framework to create unit tests, run unit tests, run only the code that is marked as tests, check the test results and code coverage to ensure that the code can be deployed to production.
Features of the testing framework:
- Write unit test
- Run unit test
- Check test results and code coverage.
- Generate code coverage results.
- Unit test can be created and executed to facilitate error-free code.
- Unit tests are class methods that ensure that the code works properly.
- Unit tests can test single and bulk actions.
- Tests can be written and executed for apex classes and triggers.
- Allows having a suite of regression test that can be rerun every time classes and triggers are updated.
- A test suite is a collection of apex test classes that can be run together.
- Includes execution and analysis testing tools.
- Includes support for loading test data.
- All code in unit tests must execute without throwing any uncaught exceptions.
- Governor limits are applied to test methods.
- Generate code coverage information for apex classes and triggers every time a test is run.
- All triggers must have some test coverage but tests do not have to cover every line.
- Allows parallel test execution from the Salesforce user interface, which can speed up test run time.
Unit test limitations
- Unit tests cannot send outbound email.
- Unit tests cannot perform callouts to external web services.
Requirements for Deploying code to Production
- At least 75% of apex code must be covered by unit test.
- 75% code coverage is the minimum code coverage to deploy to production. However, test coverage should be high as possible and testing should cover every use case of the application.
- All classes and triggers must compile successfully.
- Test methods and test classes are not counted as part of code coverage.
- Code coverage depends on the total number of code lines in the organization.
- When deploying new components that have 100% coverage to production, the deployment will fail if the average coverage of the new and existing code does not meet the 75% threshold.
- If a test run in the destination organization returns a coverage result of less than 75%, the existing test methods need to be modified or additional test methods written to raise the code coverage over 75%.
- The modified or new test methods can be deployed separately or with the new code that has 100% coverage.
- Test coverage results can differ when tests use the @isTest(SeeAllData=true) annotation, depending on the availability of data.
- Changes to metadata such as a user’s profile settings should match in sandbox and production before deployment.
- Unlike overall code coverage calculation in the developer console, organization’s code coverage computed in a deployment can cover managed package code through the ‘RunAllTestsInOrg’ test level.
For deployment to a production organization, all local tests in the org are run by default. No tests are executed if no test level is specified unless the deployment package contains apex classes or triggers. Tests that originate from installed managed packages aren’t run by default. If any test fails, the entire deployment is rolled back, the framework provides a framework to create unit tests, run unit tests, run only the code that is marked as tests, check the test results and code coverage to ensure that the code can be deployed to production.
Running selected tests:
- Running selected tests allows deployment time to be shortened by running a subset of tests when deploying apex components.
- Code coverage is computed for each class and trigger individually and is different than the overall coverage percentage.
- If the code coverage of an apex component in the deployment is less than 75%, the deployment fails.
- When running a subset of tests, code coverage is computed for each class and trigger individually and is different than the overall coverage percentage.
- If the deployment package contains apex classes and triggers, the executed tests must cover each class and trigger for a minimum of 75% code coverage.
- Running is a subset of tests can also be run in the Metadata API and is exposed in tools that are based on metadata API such as the force.com migration tool.
- Only test classes can be specified, not individual test methods
- Refactoring test classes can contribute to shorter test execution and deployment times.