Skip to main content
added 339 characters in body
Source Link
Benj
  • 45
  • 5

I'm trying to understand something so I can use @testSetup annotation wisely. As i read here:

https://developer.salesforce.com/docs/atlas.en-us.202.0.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

By setting up records once for the class, you don’t need to re-create records for each test method. Also, because the rollback of records that are created during test setup happens at the end of the execution of the entire class, the number of records that are rolled back is reduced

But if you run a test class, you can see that all of the DMLs are recreate for each test method and not just once.

So what is the purpose of using it if I need to add an extra SOQL to retrieve the data I created in each test method? why not just using it in the test class itself and use it directly? just to reduce a code duplication?

Edit: Seems like I was confused because of what I read here:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testsetup.htm

If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method in the class.

Thanks for your comment Phil.

I'm trying to understand something so I can use @testSetup annotation wisely. As i read here:

https://developer.salesforce.com/docs/atlas.en-us.202.0.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

By setting up records once for the class, you don’t need to re-create records for each test method. Also, because the rollback of records that are created during test setup happens at the end of the execution of the entire class, the number of records that are rolled back is reduced

But if you run a test class, you can see that all of the DMLs are recreate for each test method and not just once.

So what is the purpose of using it if I need to add an extra SOQL to retrieve the data I created in each test method? why not just using it in the test class itself and use it directly? just to reduce a code duplication?

I'm trying to understand something so I can use @testSetup annotation wisely. As i read here:

https://developer.salesforce.com/docs/atlas.en-us.202.0.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

By setting up records once for the class, you don’t need to re-create records for each test method. Also, because the rollback of records that are created during test setup happens at the end of the execution of the entire class, the number of records that are rolled back is reduced

But if you run a test class, you can see that all of the DMLs are recreate for each test method and not just once.

So what is the purpose of using it if I need to add an extra SOQL to retrieve the data I created in each test method? why not just using it in the test class itself and use it directly? just to reduce a code duplication?

Edit: Seems like I was confused because of what I read here:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testsetup.htm

If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method in the class.

Thanks for your comment Phil.

Source Link
Benj
  • 45
  • 5

@testSetup best practice

I'm trying to understand something so I can use @testSetup annotation wisely. As i read here:

https://developer.salesforce.com/docs/atlas.en-us.202.0.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

By setting up records once for the class, you don’t need to re-create records for each test method. Also, because the rollback of records that are created during test setup happens at the end of the execution of the entire class, the number of records that are rolled back is reduced

But if you run a test class, you can see that all of the DMLs are recreate for each test method and not just once.

So what is the purpose of using it if I need to add an extra SOQL to retrieve the data I created in each test method? why not just using it in the test class itself and use it directly? just to reduce a code duplication?