11

Salesforce defines Test Suite as a predefined-set of test classes that you can execute at once.

  1. Can I use a single @testSetup method to set up the test data for all test methods in one Test Suite?
  2. Can someone explain this new feature with a use case and the advantage that we have now have over previously existing test executions?
3
  • Wasn't @testSetup introduced in Spring '15? jessealtman.com/2015/01/new-spring-15-feature-testsetup
    – mkorman
    Commented Jan 25, 2016 at 17:32
  • yes it was...but my question is on apex test suites. Using this new feature, can we leverage anything on apex test class executions?
    – sam_s
    Commented Jan 25, 2016 at 17:35
  • Looks like some suite new functionality!
    – Adrian Larson
    Commented Jan 25, 2016 at 18:08

3 Answers 3

5

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.

1
  • Can we find the code coverage for separate Test Suites (If I group Test classes written by an individual developer into different suites) ?
    – Jarvis
    Commented Feb 25, 2016 at 11:41
11

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

enter image description here

and it will run all the Test classes at once.

1
  • Stinks that it isn't available via Setup to run though. Would be helpful for the admins. Better yet, make it part of deployments to allow Admins to select suites to run in addition to specific test classes. Commented Feb 9, 2016 at 19:27
2

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: Test results

1
  • yeah, I tried it too. Although, I really like this new feature much better if that's the case.
    – sam_s
    Commented Jan 26, 2016 at 4:41

You must log in to answer this question.

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