All Ideas

Idea Details

Post an Idea
1,050  Points
Open
Idea has been posted. Give it an upvote or downvote.

Remember static variables set during testSetup annotated methods

Apex & Visualforce

Many of the records created during testSetup need to be referenced during the main unit test. However, there is no way to get these records back from the database without performing queries, which partially defeats the purpose of pre-configuring those records to begin with. The most usual problem with using test methods is that developers have to query data back from the database, thus raising the number of queries we have to use for the convenience of saving DML statements and reducing total execution time of our tests. It would be convenient if the state of the heap (particularly all static members of classes that are used), would also be included in the snapshot that the testSetup annotation provides, thus reducing the number of queries we have to perform. As a compromise to storing all data that may be unnecessary, variables could be marked as transient (or some other, perhaps new keyword) that would prevent them from being serialized as part of the test setup state.

Merge Idea · Flag

Latest Comment from Salesforce

  • Josh Kaplan - 4 years ago

    We intentionally clear out static variables between each test method.  If we did not, each test would cease to be an independent trial. You could modify the static in one test method, which would make the order in which tests operate relevant to the results.  This is precisely what you don't want - data dependent tests.

    If you want information that is common to all tests, it can be inserted in the test setup method and queried in each test method.  The idea here is not to reduce the number of SOQL queries, it is to reduce the amount of data being inserted into the system.  If you insert 1000 records in test setup, run fifteen test methods, and you run a query 15 times to get the 1000 records each time, that's still less expensive (and faster) than inserting 1000 records 15 times.
  • Upvotes
  • Downvotes

Ideas

Apps

from AppExchange

Questions

Help us to keep IdeaExchange clean by pointing out overlapping ideas. We'll investigate your suggestion and merge the ideas if it makes sense.



 

 

Thanks for your merge suggestion. We will review it shortly and merge the ideas if applicable.

Salesforce takes abuse situations very seriously. Examples of abuse include but are not limited to posting of offensive language or fraudulent statements. To help us process your request as quickly as possible, please fill out the form below describing the situation. For privacy and security reasons, the final outcome of an abuse case may not be revealed to the person who reported it.


 

Thank you for your feedback. We take abuse seriously and will investigate this issue and take appropriate action.