Apex Test Suites - Spring 16 release
Test suites is pretty useful when you want to run # number of classes inside your organization.
Suppose you have 50 test classes in your org on Opportunity object and every time any developer makes any change in your apex code you want to make sure that test cases are no failing so you manually run every single class by going to
Setup-> Apex test execution
but with Test Suites rather than selecting all the relevant classes each time, you can simply run test suites. you can bundle them inside one Test Suites which can be run from Developer Console.
Developer Console-> Test-> New Suite Run
and it will run all the Test classes at once.
Can I use a single @testSetup method to set up the test data for all test methods in one Test Suite?
No. @testSetup only works on the class it is defined in. As a bonus, you cannot define a test class to be abstract or virtual, which would allow code reuse in multiple classes using nothing more than a simple extends clause.
// This code won't compile. This annoys me.
@isTest public abstract class BaseTestSetup {
@testSetup protected static void initialize() {
// Do stuff here
}
}
As a workaround to this, I've now defined a test class that contains no unit tests by itself, but includes static methods I can call from a @testSetup function. That way, I only need to copy/paste about three lines of code into each class that uses this utility test class.
Can someone explain this new feature with a use case and the advantage that we have now have over previously existing test executions?
Before this feature, we had to select the tests we wanted to run. Typically, this was almost painful when we wanted to, say, Run All Tests, or, even more importantly, test a single feature. For example, say we have a half-dozen pages that all use a single feature (say, looking up addresses in a third party system), but we want some end to end tests that verify that each page using this utility class still works, and therefore we want to run a total of seven test classes: one just to protect the utility class plus six more to verify that each controller works as expected.
Without test suites, we have to select all seven tests individually every time we want to run them. With test suites, we can test them all at once with a click. This saves time compared to having to select those same classes over and again, and reduces the possibility that you'll waste time by selecting too many/too few tests. Properly configured, it can help you maintain your features by grouping tests together logically.
As a confirmation to part of the OPs question, @testSetup does not appear to apply to other test methods within a test suite. Created the following classes, added them to a new test suite, and ran it:
@isTest private class TestClass1 {
@testSetup static void setup(){
insert new Account(Name = 'Acme, Inc.');
}
@isTest static void testA(){
List<Account> accts = [SELECT Id, Name FROM Account];
System.assertEquals(1, accts.size(), accts);
}
}
@isTest private class TestClass2 {
@isTest static void testB(){
List<Account> accts = [SELECT Id, Name FROM Account];
System.assertEquals(1, accts.size(), accts);
}
}
Results: