1

While reading this article TestSetup Annotation, I found the following explanation of how TestSetup annotation works:

If a test method changes those records, such as record field updates or record deletions, those changes are rolled back after each test method finishes execution. The next executing test method gets access to the original unmodified state of those records.

Now it makes me think of how it works with Parallel Test Execution. Following the explanation from the help article, it looks like unit tests in a test class should run sequentially to make sure that

...executing test method gets access to the original unmodified state of those records.

Is it true, or am I missing something?

1 Answer 1

4

Note: This is opinion and experience based and may not be accurate.

I believe parallel test execution is about executing multiple test classes in parallel, rather than the individual test methods in parallel.

From my understanding, during test execution (sequential or parallel):

  1. A virtualized database is initiated for a given Apex test class, with or without content based on the @SeeAllData annotation.
  2. The @TestSetup annotated method is executed once before any of the tests in that class are executed, with the infrastructure effectively calling Database.setSavepoint when the method completes.
  3. The tests methods are invoked, one-by-one and in some order, and after each method completes the infrastructure effectively calls Database.rollback to restore the database to the after-setup state.
  4. At the end of the test class execution the virtualized database is discarded.

Note that this virtualized database doesn't fully isolate various tests from each other, so you should be careful. A common gotcha is test methods that insert custom settings; these will often clash during parallel execution with locking issues.

1
  • This is more or less accurate from what I've observed as well.
    – sfdcfox
    Commented Nov 10, 2022 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .