Good tests should do the following:
- Cover as many lines of code as possible. Before you can deploy Apex or package it for the Force.com AppExchange, the following must be true.
- In the case of conditional logic (including ternary operators),
execute each branch of code logic.
- Make calls to methods using both valid and invalid inputs.
- Complete successfully without throwing any exceptions, unless
those errors are expected and caught in a try…catch block.
- Always handle all exceptions that are caught, instead of merely
catching the exceptions.
- Use System.assert methods
to prove that code behaves properly.
- Use the runAs method
to test your application in different user contexts.
- Exercise bulk trigger functionality—use at least 20 records
in your tests.
- Use the ORDER BY keywords
to ensure that the records are returned in the expected order.
- Not assume that record IDs are in sequential order.
Record IDs
are not created in ascending order unless you insert multiple records
with the same request. For example, if you create an account A, and
receive the ID 001D000000IEEmT, then create account B, the ID of account B may or may not be sequentially
higher.
- Set up test data:
- Create the necessary data in test classes, so the tests do not
have to rely on data in a particular organization.
- Create all test data before calling the Test.startTest
method.
- Since tests don't commit, you won't need to delete any data.
- Write comments stating not only what is supposed to be tested,
but the assumptions the tester made about the data, the expected
outcome, and so on.
- Test the classes in your application individually. Never test
your entire application in a single test.
If you are running many tests, consider
the following:
- In the Force.com IDE, you may need to increase the Read timeout value for your Apex project. See https://developer.salesforce.com/page/Apex_Toolkit_for_Eclipse for details.
- In the Salesforce user
interface, you may need to test the classes in your organization
individually, instead of trying to run all of the tests at the same
time using the Run All Tests button.
Best Practices for Parallel Test Execution
Tests that
are started from the
Salesforce user
interface (including the
Developer Console)
run in parallel. Parallel test execution can speed up test run time.
Sometimes, parallel test execution results in data contention issues,
and you can turn off parallel execution in those cases. In particular,
data contention issues and
UNABLE_TO_LOCK_ROW errors might occur in the following cases.
- When tests update the same records at the same time. Updating
the same records typically occurs when tests don’t create their
own data and turn off data isolation to access the organization’s
data.
- When a deadlock occurs in tests that are running in parallel and
that try to create records with duplicate index field values. A deadlock
occurs when two running tests are waiting for each other to roll back
data, which happens if two tests insert records with the same unique
index field values in different orders.
You can prevent receiving those errors by turning off
parallel test execution in the
Salesforce user
interface:
- From Setup, enter Options... in the Quick Find box, then select Options....
- In the Apex Test Execution Options dialog, select Disable
Parallel Apex Testing and than click OK.