You need to sign in to do that
Don't have an account?
TROG
Is there any way to create a test class "subroutine"?
Hi, I repeatedly have to create contacts, accounts, opportunities, and countless other custom objects as dependencies for testing x, y, and z. Perhaps I'm doing this wrong, but when I'm implementing a new trigger, I'm creating a test class for testing that trigger, and within that test class I have a number of methods which correspond one-to-one with my list of test cases that I'm intending to evaluate. Within each of those methods I'm repeating all the setup tasks required over and over and over again....unnessessarily (I assume) adding significant (redundant) lines of code for test prep.
Is there any way I can abstract this repeated test prep into a "subroutine"? My current understanding (and education) tells me that I can't, that each test method needs to be 100% "self contained"...but I feel that this can't possibly be right due to the high level of inefficiency.
Thanks!
Is there any way I can abstract this repeated test prep into a "subroutine"? My current understanding (and education) tells me that I can't, that each test method needs to be 100% "self contained"...but I feel that this can't possibly be right due to the high level of inefficiency.
Thanks!
I can explain with an example consider you need to insert Account information with your desired name and in an other test class you need to insert 5 different test Account and return them.
So now write a class with methods to insert account which can be called from multiple classes:
Now, you have a test class where you would like to insert an account
Try it!
Thanks,
balaji
All Answers
You can create the test data setup in multiple ways:
1) You can have an utility class which contains multiple methods, ideally one method per object and use method overloading(based on your requirment if necessary) to accept different set of parameters which you might need to set while inserting a record. Refer to these methods from all of your test classes, where ever you need to insert test data.
- Make the utility class annotated with @isTest so that it wont count towards the characters of code limit.
- This helps you to modify the test data easily, when ever a new validation rule or validation through trigger is written.(one place to maintain)
2) You can also have your test data setup in CSV file which is stored in static resources and use it from all of your test clases! For more info on that, take a look at below link:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_load_data.htm
Hope it helps.,
Thanks,
balaji
Thanks, Tim
I can explain with an example consider you need to insert Account information with your desired name and in an other test class you need to insert 5 different test Account and return them.
So now write a class with methods to insert account which can be called from multiple classes:
Now, you have a test class where you would like to insert an account
Try it!
Thanks,
balaji
Also...TestUtilities.create_Account() - can the two methods create_Account(), one creating an individual record and the other creating multiple records, have the same name?
Thanks so much - this will really help me!
Also, you can create static test data to use across all your test cases in in the event you want to re-use data. It would like similar to:
Hopefull this helps as well.
So to update your code you would have the following: