Code Coverage in Salesforce
Last updated on Jun 12, 2024
Code Coverage in Salesforce
If you are a Salesforce CPQ developer, then you must be aware of the code coverage concepts in Salesforce. Code coverage is considered to be an important technique because it gives an idea of how much code is being tested in the Apex system. The topics which we are going to cover under the Code coverage in salesforce are a brief introduction to code coverage, the importance of code coverage, best practices, and testing of code coverage in the Apex system. So let’s begin;
Code coverage with an example:
Code coverage is a commonly used technique in the Apex system. Code coverage in Salesforce is a software testing metric that determines the number of code lines that will be validated under testing procedures. It also helps many software companies to verify the end products to deliver high-quality end products. The formula to calculate code coverage is as follows;
Code coverage = (number of lines of code executed / total numbers line of code )* 100
The higher the coverage, the more thorough the testing, that is more the coverage, higher may be the quality. But it is persistent to mention that even after going for the 100% code coverage, the quality factor still is not guaranteed.
The following are the different types of code average available;
- Statement coverage and block coverage
- Function coverage
- Function call coverage
- Branch coverage
- Modified conditions or decision coverage
Wish to make a career in the world of salesforce? Start with HKR'S salesforce online training !
Salesforce Training
- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning
Purpose of code coverage in salesforce:
The primary purpose behind code coverage is to ensure that no line of code or area of the program is left untouched for the testing purpose. Through code coverage, we may be able to get the answers to the following questions.
- How many lines of code are being executed during the time of test execution.
- How many lines of code or areas of the programming are still beyond the reach of the testing process?
- How many additional test cases may be required to increase the code coverage?
- Redundant test cases, useless for increasing the code coverage.
Importance of code coverage in Salesforce:
Here we have listed a few benefits of the code coverage in salesforce:
- Code coverage metrics are the best way to get rapid feedback on the tests and code being written.
- It shows which line of source code has been tested and which has not been tested.
- It allows you to improve your testing so you can improve your code, ultimately increasing revenue and reducing costs.
- Helps monitor the automated tests, as the code base and automated tests grow and change.
Testing and code coverage in Salesforce:
At the beginning only we have mentioned that code coverage is commonly used in the Apex system. The Apex testing framework generates the code coverage numbers to monitor the Apex classes and triggers issues when you run one or more test cases.
In the following image, you can see how does code coverage used in the Apex system;
To ensure the quality of the code you developed, unit test cases enable you to meet the code coverage requirements for deploying or packaging the Apex systems. To deploy the Apex test into the Salesforce AppExchange, a unit test should cover at least 75% Of accuracy.In a few scenarios where we can see those code coverage methods are filed to meet the test effectiveness in the Apex system. But you can make use of the code coverage as a tool to assess whether the codes require more testing or not. UAT (user assistance testing) is one such testing method that ensures the application behavior and quality of the code.
Become a DemandWare Certified professional by learning this HKR Demandware Training
Inspecting code coverage:
After running the code, you can see the code coverage information on the developer console of the test tab. In general, the code coverage panel consists of the following types of information;
Coverage information for each Apex class
Overall coverage for all the Apex codes in your organization.
Code coverage information is stored in two lightning platform tooling API projects such as ApexCodeCoverageAggregate and ApexCodeCoverage. The ApexCodeCoverageAggregate stores the sum of the lines covered for a specific class after checking all the test methods that test it. Whereas the ApexCodeCoverage stores the sum of the lines covered and uncovered by each individual test method. Users can query these objects using SOQL and API tooling integration to retrieve coverage information. Using SOQL is the best way to check for the coverage code and get more details.
Consider the below example;
In the above example, teh SOQL query gets the code coverage details for the class “TaskUtil”. The code coverage is aggregated from all the test cases in teh methods.
*note: to access SOQL query a tooling API is required. You can run SOQL queries with the help of the Query editor in the developer’s console -> check for the tooling API.
Here we have taken a few query examples that are partially covered by the test,
In the next example, you can see which test methods cover the classes in Apex. Usually, the query information consists of different object details, ApexCodeCoverage (it stores the coverage information by test class and methods).
The queasy result is as follows;
Code coverage VS test coverage:
Here we are going to explain the major differences between the code coverage and test coverage:
Code coverage:
- Code coverage is completely a white-box testing technique.
- In order to assess coverage, it needs to see the application code.
- It is just a measure of how the code is executing during the time of testing.
- It never uses a design specification.
- Code coverage is usually performed by the developers.
Test coverage:
- Test coverage is a black-box testing technique.
- Test coverage has minimal interaction with code.
- It is a measure of how many test cases have been executed during the test.
- It is aligned with the requirement specifications.
- Mainly this type of coverage is performed by the QA testers.
Click here to get frequently asked Salesforce Interview Questions & Answers
Subscribe to our YouTube channel to get new updates..!
Code coverage best practices:
Consider the following tips while working on code coverage in salesforce:
- You can refresh the code coverage numbers by running the tests. Sometimes code coverage numbers are not refreshed when you update the Apex codes in the organization’s source code unless you rerun the testing.
- If the organization’s source code has been updated since the last test run, then you will get an incorrect code coverage number. So to avoid such a scenario re-run the Apex tests to get a correct estimated code coverage time.
- The overall code coverage percentage in your organization does not include code coverage numbers from managed package tests.
- The code coverage in the organization is based on the total number of code coverage. Adding/ deleting or modifying the code changes the coverage percentage.
Calculate overall code coverage in Salesforce
Follow these steps to check for the reliability of the “estimated the code coverage”:
- First, navigate to the setup option.
- Then in the next window screen search for the “Quick find search type” as Apex teh select the “Apex test execution” option.
- Now click the “option” button.
- Deselect the option “store only aggregated code coverage” ->then click the “OK” option.
- Click the button “view test history”.
- Click the button “ clear all test history”.
- Now go to the option quick find search type as “Apex” and “click on the Apex classes”.
- Next click the option “compile all classes”.
- In the quick find search type as “Apex” and “Apex test execution”.
- The final step is to click the button “Run all tests”.
Wish to make a career in the world of salesforce? Start with HKR'S Salesforce Training In Sanfrancisco !
Final take:
The code coverage is a testing technique that is commonly used in the Apex system to evaluate the test executions. This code coverage in the salesforce, article enables you to learn the concepts like purposes, importance of using code coverage, testing and code coverage, inspecting code coverage, and the steps involved to calculate overall code coverage.
Related Articles:
About Author
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.
Upcoming Salesforce Training Online classes
Batch starts on 21st Dec 2024 |
|
||
Batch starts on 25th Dec 2024 |
|
||
Batch starts on 29th Dec 2024 |
|