I am trying to add custom settings data into my Test DataFactory so as to increase test coverage.

I currently create data for custom settings in my test datafactory.This is both time consuming and tedious to maintain.

Here is how a simplified version of the datafactory

public class Test_DataFactory {

public static void createMyAppCustomSettings() {
        List<MyAppCustomSettings__c> applst = new List<MyAppCustomSettings__c>();
        applst.add(new MyAppCustomSettings__c(Name = 'Test 1'));
        applst.add(new MyAppCustomSettings__c(Name = 'Test 2'));


Then i call the createMyAppCustomSettings() method in testSetup in a test class.How can i add make MyAppCustomSettings__c without me having to create the data manually.

private class Test_MyAppCustomSettings {
    static void buildTestClassData() {


2 Answers 2


You're meant to insert Custom Settings during a unit test (at least, with isolation enabled). You should choose to insert custom settings when testing code that depends on those custom settings. If you really want to use existing settings, you can use SeeAllData=true, but this is not recommended. Do not do it. Insert your Custom Settings as you're meant to do.

  • Is there a way i can make only the data for the custom settings only visible.I already tried adding SeeAllData=true in the testdatafactory but i still wasn't getting the records Commented Jun 10, 2018 at 8:01
  • @user2650277 You need to use SeeAllData=true on the class where the test runs. A utility class can't specify their own isolation.
    – sfdcfox
    Commented Jun 10, 2018 at 8:03
  • Ohh but if i do that for the Test_MyAppCustomSettings , then all data for the org will be visible.Is there a way to write the Test_MyAppCustomSettings in such a way that only MyAppCustomSettings__c is visible Commented Jun 10, 2018 at 8:09
  • @user2650277 Nope. It's all or nothing. The official recommendation is to insert any custom settings that you need.
    – sfdcfox
    Commented Jun 10, 2018 at 8:11
  • developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… seems to be a better option.What do you think Commented Jun 10, 2018 at 8:16

You can use custom metadata types instead of custom setting, it behaves same as custom settings and it is also available in test classes without using seealldata=true annonation

here is the salesforce trailhead link for the it describing same https://trailhead.salesforce.com/en/modules/custom_metadata_types/units/custom_metadata_types_create#

  • That's partially true. Custom Metadata Types are often less suitable for application configuration (as opposed to support data) because they cannot be manipulated in Apex as Settings can - there's currently no support for deleting them from Apex, and Create/Update workflows are much clunkier.
    – David Reed
    Commented Jun 10, 2018 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .