Activity Forums Salesforce® Discussions Why do we need to write test classes in salesforce?

  • Saddam

    Member
    September 24, 2019 at 6:23 am

    Hi,

    Test class in salesforce
    Testing is an important part of SDLC. So, before deploying our code to production environment, Salesforce requires at least 75% of your code to be covered by our test classes whic. Salesforce has done that to make sure that our code doesn’t break in any situation in Production. Today we’ll see how we write the test class with example in Salesforce.

    Some points regarding the test classes in Salesforce, which you have to know for sure:

    At least 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully. But this should not be our focus. We should aim for 100% code coverage, which ensures that you cover each positive and negative use case of your code to cover and test each and every branch of your code.
    Calls to System.debug are not counted as part of Apex code coverage.
    Test methods and test classes are not counted as part of Apex code limit. So, no worries about writing long test class with more methods just to make sure that all your code branches are covered.
    Every trigger you are trying to deploy should have at least 1% coverage, but yes overall coverage of your production org after getting your code deployed should be 75%, otherwise Salesforce won’t let you deploy your code.
    Class can be deployed on 0% coverage as well, but as I told in last point, that overall coverage of your production org after getting your code deployed should be 75%, otherwise Salesforce won’t let you deploy your code.

     

    The key points while writing a test class are:

    You have to start your class with @isTest annotation, then only Salesforce will consider this class as test class.
    Keep your class as Private, and the best practice is to name your test class as your original Class or trigger Name + ‘Test’.
    Methods of your test class have to be static, void and testMethod keyword has to be used.
    Prepare your test data which needs to be existing before your actual test runs. There are multiple techniques of creating test data now a days, for example, setup method, static resources etc. For more details about @testsetup method check below link @testSetup method in apex test class
    Use Test.startTest() and Test.stopTest() to make sure that the actual testing of your code happens with the fresh set of governer limits. These methods help you to reset your governor limits just before your actual code of testing get executed.
    Once your test code runs between Test.startTest() and Test.stopTest(), you must use assert statements to test whether your actual code is executing correctly and giving the results as expected. In our case, we are test whether book’s price has been set to 90 or not. If this assert statement returns false, then your test class will fail, and will let you know, that something is not correct in your code, and you need to fix your original code.
    Because we are testing a simple trigger, we could not show the testing using negative use cases, but in an ideal world, you should write multiple methods in your test class, few should test your positive use cases, and other should test your negative test cases.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos