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?
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.

  • Can we find the code coverage for separate Test Suites (If I group Test classes written by an individual developer into different suites) ? – Jarvis Feb 25 '16 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.

  • 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. – Bob Nunemaker Feb 9 '16 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.