function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DaveGraupnerDaveGraupner 

Inconsistent Test Method Results

I am getting some strange results with test methods. I have a class that contains several test methods. If I run the test for that class only, some of my System.Assert statements fail. All the failures have to do with a custom picklist field. However if I take just one of the test methods and run it in its own class then the same statements that failed before now pass the System.Assert. I was under the impression that each test method in a class runs independently of all other test methods. My results don't seem to agree with this?

 

Upon further investigation the problem only happens when I set a custom picklist field to a value not in the picklist. The field is not a restricted picklist. I can see from the debug log that the value I have set in the field (Owner_Assigned__c = Hardware1) is present immediately before the validation rules fire (last line of the insert trigger is a system.debug statement). One of the validation rules references this field and the log shows that this has been set to another value (Owner_Assigned__c=12). This value of 12 is not a real value because in the code that follows the case insert in the test method the system.Assert that fails shows the value as null. Also if I create a validation rule to fire when the value of this field is 12 it never fails.

 

If I add the test value for this field that I am using to the picklist, all the tests run fine. Note that the test methods are never returning any DML or other exception errors. It is only the system.assert statements that are catching something.

 

Is there something I am doing incorrectly in my testing that would explain all of this?

 

Thanks

 

Dave

ahab1372ahab1372

is Hardware1 a variable or the string your are trying to assigne to the field? Then it should be

Owner_Assigned__c = 'Hardware1'

DaveGraupnerDaveGraupner

Hardware1 is a string. Either way it makes no difference to the problem being described. A field is being set in code, the debug statement shows it is set and then when SF runs the validation rules it shows a different value.

 

Thanks

 

Dave

ahab1372ahab1372

validation rules don't change field values, they just throw an error if a values matches certain criteria. What I think you see in the debug log is the internal representation of the picklist value (12 is maybe the 12. option on your picklist?). I am not 100% sure but I think I have seen that before.

DaveGraupnerDaveGraupner

If I look at the debug log for a run where all the system.asserts pass I can see the log has the owner_assigned__c=Hardware1. In the case where it doesn't work it says owner_assigned=12. In both of these tests the value Hardware1 is NOT in the list of values for the picklist.

 

Can you explain why when I run one test method it works but when I run all the tests I get test failures on the system.assert. Are not test methods totally independant of each other?

 

Thanks