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.