---
schema_version: "1"
id: d2eb48e90b2d3613224cefad3c7245d90d8db9654b98c3f4739e4543df4e0fde
title: Permission Set Groups
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_permission_set_group_testing.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_permission_set_group_testing.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: b0bae1e7e028494e40028827771357c706ac88574ce2f54337422001d113a08b
description: To provide Apex test coverage for permission set groups, write tests using the calculatePermissionSetGroup() method in the System.Test class.
---
# Permission Set Groups

To provide Apex test coverage for permission set groups, write tests using the calculatePermissionSetGroup() method in the System.Test class.

The [calculatePermissionSetGroup()](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_System_Test_class.htm) method forces an immediate calculation of aggregate permissions for a specified permission set group. As the forced calculation counts against Apex CPU limits, and can require complex data setup, it’s a best practice to minimize the number of times you perform this operation. Recalculating complex permission set groups with a large number of included permission sets or overall enabled permissions can cause Apex test failures because Apex CPU limits are exceeded. Apex CPU limits can also be exceeded if the included permission sets in the permission set group aren’t licensed and the permission set group is assigned to many users.

Set this test to run once in a Test setup method, then reuse the data in subsequent tests.

@IsTest
public with sharing class PSGTest {
    @IsTest 
    static void testPSG() {
        // get the PSG by name (may have been modified in deployment)
        PermissionSetGroup psg = \[SELECT Id, Status FROM PermissionSetGroup WHERE DeveloperName='MyPSG' WITH SYSTEM\_MODE\];
    
    // force calculation of the PSG if it is not already Updated
    if (psg.Status != 'Updated') {
      Test.calculatePermissionSetGroup(psg.Id);
    }
    
    // assign PSG to current user (this fails if PSG is Outdated)
    insert as system new PermissionSetAssignment(PermissionSetGroupId = psg.Id, AssigneeId = UserInfo.getUserId());

    // additional tests to validate permissions granted by PSG
  }
}

#### See Also

- [Salesforce Help: Permission Set Groups](https://help.salesforce.com/s/articleView?id=platform.perm_set_groups.htm&type=5&language=en_US "Salesforce Help: Permission Set Groups - HTML (New Window)")
- [_Apex Reference Guide_: Test Class](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_methods_system_test.htm "Apex Reference Guide: Test Class - HTML (New Window)")