Does a Permission Set in @TestSetup get removed after Test.stopTest()?

Question
If we create a Permission Set inside a @TestSetup
method in an Apex test class, will it be automatically removed after Test.stopTest()
, similar to how standard or custom object records are deleted at the end of a test execution?
For example, consider the following code:
@TestSetup
static void setup() {
PermissionSet ps = new PermissionSet();
ps.Label = 'dummy-test';
ps.Name = 'dummy_test';
insert ps;
}
After Test.stopTest()
, will this Permission Set be automatically deleted, or do we need to manually remove it?
Answer
No, a Permission Set created inside a @TestSetup
method will not be automatically removed after Test.stopTest()
.
Test.stopTest()
is primarily used to reset governor limits and execute any asynchronous code synchronously. However, it does not clean up data created during the test, including Permission Set records. Unlike standard or custom object records, metadata objects like Permission Set are not automatically rolled back at the end of a test execution.
If you need to ensure that the Permission Set is removed, you should explicitly delete it in your test method. Here’s an example:
@isTest
private class PermissionSetTest {
@TestSetup
static void setup() {
PermissionSet ps = new PermissionSet();
ps.Label = 'dummy-test';
ps.Name = 'dummy_test';
insert ps;
}
@isTest
static void testPermissionSet() {
Test.startTest();
// Your test logic here
Test.stopTest();
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'dummy_test' LIMIT 1];
delete ps;
}
}
The PermissionSetTest
class is an Apex test class designed to verify the behavior of Permission Sets in a test environment. It consists of two key methods: setup()
and testPermissionSet()
.
The @TestSetup
method, setup()
, is executed once before any test methods in the class run. It creates a new PermissionSet
record with the label “dummy-test” and the API name “dummy_test”, then inserts it into the database. This allows the same test data to be shared across multiple test methods within the class. However, since metadata objects like Permission Sets are not automatically removed after a test, they may persist beyond the test execution.
The test method testPermissionSet()
begins with Test.startTest()
, which resets governor limits to simulate a fresh transaction. The actual test logic would be placed here (though it is not included in this example). After executing the test logic, Test.stopTest()
is called, which ensures that any asynchronous operations, such as future methods or batch jobs, complete execution.
Finally, the test method queries the database for the previously inserted Permission Set using SOQL. If the record is found, it is explicitly deleted to prevent any unintended persistence after the test execution. This deletion step is necessary because, unlike standard or custom objects, Permission Sets do not automatically roll back at the end of a test, meaning they need to be manually removed if required.
Alternative Approaches
If possible, instead of creating a Permission Set dynamically in a test, you can use an existing one or a packaged Permission Set available in your org. This avoids the need to clean it up manually and ensures consistency across tests.
If metadata creation is necessary, you might consider using the Metadata API or a setup script outside of Apex tests, but this would generally require deployment and manual cleanup.
Job-Oriented Salesforce Training with 100% Money Back Guarantee
Our Salesforce Course is structured to provide a comprehensive understanding of the Salesforce platform, equipping you with the essential skills needed to excel in the CRM industry. The program covers important modules like Salesforce Admin, Developer, and AI, seamlessly integrating theoretical concepts with hands-on application. Through practical assignments and real-world projects, you will develop the expertise to solve complex business challenges using Salesforce solutions. Our experienced instructors ensure that you gain both technical proficiency and industry-relevant insights to succeed in the Salesforce ecosystem.
Beyond technical expertise, our Salesforce Training in UK includes personalized mentoring, certification preparation, and interview coaching to enhance your career opportunities. You’ll have access to extensive study materials, live project exposure, and dedicated support throughout your learning journey. By the end of the course, you will be fully prepared for certification exams and possess the real-world skills that employers seek. Start your Salesforce career with us and explore limitless career possibilities. Sign up for a Free Demo today!