Before we begin, we have to know about unit testing i.e. what is unit testing and why do we test the code? All that stuff in your mind is right now.So first let’s deal with unit testing.
What is Unit Testing?
Unit Testing is the practice of writing code for the development of software in addition to business logic. A unit as the word suggests is the smallest possible code and generally it is considered as a method.
Don't forget to check out: 5 Expert Tips To Make Testing In Salesforce Easier
Why do we test code?
There are many reasons but I’m going to discuss important reasons which played an important role in the faster development of software.
In 2010, the IBM analyst team discussed the time taken in finding out the errors in the whole software development cycle. From their reports, they find out that the design phase costs approx one hour, and when the development is going on, finding and fixing the bug took around 6.5 hours, and when we test each function it costs around 15 hours. Up to this point everyone can manage but the real problem happens when this bug fixing took over 100 hours in production.
Now we know why we test code before deploying in Production. So now we have to discuss how to test code, what are the requirements for testing the data etc.
What are the methods for creating test data?
First we are going to discuss the Brute Force method in which we have to simply create and insert the data.They are simple and easy to follow up to a certain limit until the data created for shorter code. But you are not creating for shorter code, your business might have lengthy logics i.e code now when you create test data by Brute Force then it will become very complicated and difficult to understand. Now it felt there is a need for another method which are Test Factories.
Secondly, we talk about Test Factories which are very simple and easy to follow. The benefit of using Test Factories is they are reusable and are open-sourced means for general usage they are available online. As we know everything has disadvantages, so the disadvantage with this method is that we cannot call them for every method automatically that’s why we introduced a new method which comes up with this new feature.
Check out another amazing blog by Deepak here: Reports & Dashboards For Salesforce Lightning Experience
Thirdly, we talk about TestSetup which is created in order to meet the requirements for similar multiple methods in test class.
Till now we have created test data but the question arises what kind of data we created, Is it for positive or negative testing. It depends because there are various kinds of testing like Positive Testing, Negative Testing, Permission-Based Testing, Mock & Stub Testing.
As the name indicates Positive Testing returns expected data when valid data is passed to it. While Negative Testing returns expected data when invalid data is passed to it. It is sometimes difficult to write but it can be done with practice. Permission-Based Testing test on the basis of both positive and negative testing.