Test Class
Q. Test classes
Test best practices:
- Used for unit testing the code
- @isTest, testmethod, @testSetup are used
- TestSetup is for setting the test data for the test class, as a test from one test method is not visible in another
- Use a data factory for creating test data
- Use mock responses for testing web services(static resource mock, httpcalloutmock, webservice mock)
- Use Test.startTest, Test.stopTest to maintain within the limits
- Using SeeAllData=true will allow the test class to access all the data of org.
- System.runAs helps in executing a test class in user mode.
Test best practices:
- If code uses conditional logic (including ternary operators), execute each branch.
- Make calls to methods using both valid and invalid inputs. (positive and negative testing)
- Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
- Always handle all exceptions that are caught, instead of merely catching the exceptions.
- Use System.assert methods to prove that code behaves properly.
- Use the runAs method to test your application in different user contexts.
- Exercise bulk trigger functionality—use at least 20 records in your tests.
- Use the ORDER BY keywords to ensure that the records are returned in the expected order.
- Not assume that record IDs are in sequential order.
- Do not user SeeAllData=true if it is not necessary.
No comments: