Loading
Salesforce から送信されるメールは、承認済ドメインからのみとなります続きを読む

テストメソッドの制限の使用状況の確認方法について

公開日: Nov 24, 2022
説明
本文書では、テストメソッドの制限の使用状況の確認方法について説明します。
解決策
デバッグログを取得しCUMULATIVE_LIMIT_USAGEの欄で制限を確認することが出来ます。
Test.startTest()/Test.stopTest() 内における制限を確認するためには、CUMULATIVE_LIMIT_USAGE欄の TESTING_LIMITS で確認することが出来ます。

以下の Apex クラスおよびテストクラスを例に確認方法を説明します。

Apexクラス
public class ApexClass{
    public void doInsert(String accountName){
        List<Account> acs = [ select Id from Account where Name = :accountName ];
        List<Contact> cs = new List<Contact>();
        for( Account a: acs ){
           Contact c = new Contact();
           c.AccountId = a.Id;
           c.LastName = 'ContactName';
           cs.add(c);
        }
        Insert cs;
    }
}

テストクラス
@isTest
public class ApexClassTest{
    static testmethod void test(){
        String accountName = 'testAccount';
        Account ac = new Account( name = accountName );
        Insert ac;
        Test.startTest();
        ApexClass a = new ApexClass();
        a.doInsert(accountName);
        Test.stopTest();
    }
}

デバッグログ
16:06:06.485 (485815843)|CUMULATIVE_LIMIT_USAGE
16:06:06.485 (485815843)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:06:06.485 (485815843)|TESTING_LIMITS
16:06:06.485 (485815843)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:06:06.485 (485815843)|CUMULATIVE_LIMIT_USAGE_END

上記、テストクラスでは取引先(Account)にレコードを登録した後に、Test.startTest()/Test.stopTest() にて ApexClass クラスを呼び出しています。ApexClass クラスでは取引先(Account)に対しSOQLを実行した後に取引先責任者(Contact)にレコードを登録します。

Test.startTest()/Test.stopTest() 内の処理における制限の使用状況を確認するには CUMULATIVE_LIMIT_USAGE の TESTING_LIMITS を確認します。以下欄より SOQL の発行数が1、DMLでの更新レコード数が1 ということが確認出来ます。
16:06:06.485 (485815843)|TESTING_LIMITS
16:06:06.485 (485815843)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

以下欄では Test.startTest()/Test.stopTest() 以外の該当のメソッドにおける処理の制限の使用状況が確認出来ます。
以下では、DMLでの更新レコード数が1 ということが確認出来ます。
16:06:06.485 (485815843)|CUMULATIVE_LIMIT_USAGE
16:06:06.485 (485815843)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10
ナレッジ記事番号

000387802

 
読み込み中
Salesforce Help | Article