As all of us know flows are the future of Salesforce. However, trigger flow is a new feature and we still bothering by its adolescence issues.
One of the most annoying problems is the lack of possibility to calculate the coverage of a flow. Of course, we have an object FlowTestCoverage but there is no possibility to use it to calculate aggregated coverage provided by many test methods.
Please consider following example:
- I have created a very simple custom object A__c with two fields X__c and Message__c
- I created a flow that checks if X__c > 0 and set the Message__c to „Is greater than 0” otherwise set the Message to „Is less or equal 0”
- It is simple, isn’t it?
Next I have written a unit test to provide the coverage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | @IsTest public class CheckXFlow_Test { @IsTest public static void check5(){ A__c theA = new A__c( X__c = 5 ); Test.startTest(); upsert theA; Test.stopTest(); A__c theAfromDB = [ SELECT Message__c FROM A__c ]; System.assertEquals('Is Greater Than 0', theAfromDB.Message__c); } @IsTest public static void checkMinus5(){ A__c theA = new A__c( X__c = -5 ); Test.startTest(); upsert theA; Test.stopTest(); A__c theAfromDB = [ SELECT Message__c FROM A__c ]; System.assertEquals('Is Less Or Equal 0', theAfromDB.Message__c); } } |
and I have executed all tests:
However, when we are going to check the coverage by executing the following query:
1 | SELECT id, NumElementsCovered, NumElementsNotCovered FROM FlowTestCoverage |
You can see we can find the coverage only for every given method but when we try to calculate the full coverage as:
I got (2+2) /(1+1+2+2)= 4/6 = 66% coverage, but we know that our tests covered both options (X<=0) and (X >0) therefore aggregated coverage should be 100% of the flow elements.
The reason behind it is that test method CheckXFlow_Test.check5 covered two out of three flow elements:
And test method CheckXFlow_Test.check5 covered also 2 out of 3 elements:
As we can see this is not the right approach therefore we should try something else. Another option is to find the coverage by querying FlowElementTestCoverage
1 | SELECT id, ElementName, FlowTestCoverage.ApexTestClass.FullName FROM FlowElementTestCoverage |
This query finds all flow elements covered by the unit test, but it does not find elements that are not covered.
Still far from perfect…, but let’s try to think out of the box.
1. As we learned before by querying the FlowElementTestCoverage we can find elements that are covered:
2. On top of that by querying the FlowTestCoverage object we can find the number of elements that are covered and not covered. Of course, the proportion of the elements covered and not covered might be different for every test method but the sum will not change
3. As we can see when we calculate [NumCoveredElements] from step 1 divided by [NumCoveredElements] + [NumUncoveredElements] from step 2 we will get 3/(2+1) = 100% coverage.
As we have learned in this article it is possible to calculate the coverage of a given flow, but it is not a trivial activity. Therefore in the next article, I will show how it can be automated.
Author: Bartosz Borowiec