2

I am trying to add test classes in the test suite.

But when I click on save, I am getting below error:

enter image description here

I am trying to add more test classes using Apex like below using Anonymous execution:

List<ApexClass> ac = [SELECT Id, Name, Body FROM ApexClass];
List<ApexClass> testClasses = new List<ApexClass>();
List<TestSuiteMembership > testSuiteMembership = new List<TestSuiteMembership>();
List<Id> existingApexClassInTestSuiteIdList = new List<Id>();
for( TestSuiteMembership testSuiteMembership : [SELECT ApexClassId FROM TestSuiteMembership WHERE ApexTestSuiteId ='<Test suite id>']){
    existingApexClassInTestSuiteIdList.add( testSuiteMembership.ApexClassId);
}
TestSuiteMembership testSuiteMembershipObject;
for(ApexClass a : ac){
    if(!existingApexClassInTestSuiteIdList.contains(a.Id) && (a.Body.containsIgnoreCase('@istest') || a.Body.contains('static testmethod'))){
        testSuiteMembershipObject = new TestSuiteMembership();
        testSuiteMembershipObject.ApexClassId = a.Id;
        testSuiteMembershipObject.ApexTestSuiteId = '<Test suite id>';
        testSuiteMembership.add( testSuiteMembershipObject);
    }
}
insert testSuiteMembership;

But not able to insert the objects and getting below error:

enter image description here

Alternative option: I can create multiple test suites and add 200 classes in each suite.

How can we add more than 200 test cases in single test suite?

1
  • 1
    As per the error message in first screenshot, it says to insert the records via rest, soap or tooling API. Developer console is still apex and inside Salesforce.
    – Raul
    Dec 23, 2019 at 11:44

1 Answer 1

3

To use the REST api for the insertions as the error message and Raul suggest, try replacing this:

insert testSuiteMembership

With this:

String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
String endpoint = baseURL + '/services/data/v46.0/sobjects/TestSuiteMembership';      
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint); 
req.setMethod('POST'); 
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());  
req.setBody(json.serializePretty(testSuiteMembership));
Http http = new Http();   
HttpResponse res = http.send(req);  
System.debug('Response Code > ' + res.getStatusCode());
System.debug(' Response > ' + res.getBody());
1
  • Thanks I was able to create the TestSuiteMembership records. But when I was passing the list in body, I was getting error. So I passed each testSuiteMembershipObject in body and called the above API. As We can not call more than 100 API in one transaction so added check for the number of APIs called(<100) and ran the whole snippet multiple times. Jun 27, 2022 at 15:19

You must log in to answer this question.

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