2. In every Test Class you should :
Create your own test data
Test your code
Assert the results of your test
What is a Test Class
3. Test Class
Usual Test Class
Method 1
Create Data
Execute Test 1
Assert Results 1
Method 2
Create Data
Execute Test 2
Assert Results 2
Method 3
Create Data
Execute Test 3
Assert Results 3
4. Test Class
Usual Test Class
Method 1
Create Data
Execute Test 1
Assert Results 1
Method 2
Create Data
Execute Test 2
Assert Results 2
Method 3
Create Data
Execute Test 3
Assert Results 3
Execute Test 1
Assert Results 1
Execute Test 2
Assert Results 2
Execute Test 3
Assert Results 3
This is the aim and heart of your tests
5. Test Class
Usual Test Class
Method 1
Create Data
Execute Test 1
Assert Results 1
Method 2
Create Data
Execute Test 2
Assert Results 2
Method 3
Create Data
Execute Test 3
Assert Results 3
Create Data Create Data Create Data
This must be done, but potentially doing X times the same thing !!
6. Test Class
Usual Test Class
Method 1
Create Data
Execute Test 1
Assert Results 1
Method 2
Create Data
Execute Test 2
Assert Results 2
Method 3
Create Data
Execute Test 3
Assert Results 3
ex: 30s ex: 30s ex: 30s
This must be done, but potentially doing X times the same thing !!
Meaning execution time lost for nothing !!
ex: 1mn30 instead of 30s
Create Data Create Data Create Data
8. What if we could do the same job
only once ?
Here comes @testsetup !
9. Test Class
Usual Test Class
Method 1
Create Data
Execute Test 1
Assert Results 1
Method 2
Create Data
Execute Test 2
Assert Results 2
Method 3
Create Data
Execute Test 3
Assert Results 3
10. Test Class
@testsetup
Method 1
Execute Test 1
Assert Results 1
@testsetup method : Create Data
Method 2
Execute Test 2
Assert Results 2
Method 3
Execute Test 3
Assert Results 3
11. Test Class
@testsetup
Method 1
Execute Test 1
Assert Results 1
@testsetup method : Create Data
Method 2
Execute Test 2
Assert Results 2
Method 3
Execute Test 3
Assert Results 3
@testsetup method : Create Data
• Creation of test data is now common to all methods
• Executed once
• Rollbacked between all methods
Test classes execute faster !!
13. Test setup methods are supported only with the default data
isolation mode for a test class
Multiple test setup methods are allowed in a test class, but the
order in which they’re executed by the testing framework isn’t
guaranteed
If a fatal error occurs during the execution of a test setup method,
the entire test class fails, and no further tests in the class are
executed
If a test setup method calls a non-test method of another class, no
code coverage is calculated for the non-test method
What else ?