Test Class

Q. Test classes


  1. Used for unit testing the code
  2. @isTest, testmethod, @testSetup are used
  3. TestSetup is for setting the test data for the test class, as a test from one test method is not visible in another
  4. Use a data factory for creating test data
  5. Use mock responses for testing web services(static resource mock, httpcalloutmock, webservice mock)
  6. Use Test.startTest, Test.stopTest to maintain within the limits
  7. Using SeeAllData=true will allow the test class to access all the data of org.
  8. System.runAs helps in executing a test class in user mode.


Test best practices:

  1. If code uses conditional logic (including ternary operators), execute each branch.
  2. Make calls to methods using both valid and invalid inputs. (positive and negative testing)
  3. Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
  4. Always handle all exceptions that are caught, instead of merely catching the exceptions.
  5. Use System.assert methods to prove that code behaves properly.
  6. Use the runAs method to test your application in different user contexts.
  7. Exercise bulk trigger functionality—use at least 20 records in your tests.
  8. Use the ORDER BY keywords to ensure that the records are returned in the expected order.
  9. Not assume that record IDs are in sequential order.
  10. Do not user SeeAllData=true if it is not necessary.

No comments:

Powered by Blogger.