Free computer code on screen

Apex test cases encounter failures when executed concurrently.

At present, when running Apex test cases through either Apex Test Execution or ApexTestQueueItem records, two cases are executed simultaneously.

Typically, this is not problematic; however, issues may arise if the test cases lack complete isolation (e.g., they operate on the same records) and data isolation is disabled. Failures might occur that wouldn’t manifest if the test cases were executed sequentially.

In my scenario, I encountered an error message indicating the inability to update the PriceBook2 (01s keyprefix):

System.DmlException: Update failed. First exception on row 0 with id 01s300000001OuYAAU; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []

With the Summer ’12 release, a new option allows you to execute test cases individually. Navigate to Your Name > Setup > Develop > Apex Test Execution > Options, and disable Parallel Apex Testing.

Turn off Parallel Apex Testing: This setting ensures that asynchronous tests are executed sequentially. It serves to mitigate potential test interference on shared data when multiple tests run simultaneously and access the same dataset. This situation arises when tests don’t generate their own data and disable data isolation to interact with the organization’s data. It’s important to note that this option doesn’t impact the asynchronous execution of tests, which still continue to run asynchronously from the Apex Test Execution page.

The message “unable to obtain exclusive access to this record” has been observed during the deployment of a change set from the sandbox to production. An unusual aspect was that the same change set had passed validation successfully but encountered deployment failures. Disabling parallel apex testing proved to be the solution for resolving this issue as well.