1

How many setup methods are allowed in 1 test class? Documentation says only 1 but I am able to add 4 and save to salesforce and run test successfully.

  • You can have only one test setup method per test class.
0

1 Answer 1

5

Starting with API Version 39.0, you can only have one method per class with the @TestSetup annotation. Saving with a lower API Version will for example allow you to compile this class:

@IsTest
class DemoTests
{
    @TestSetup static void setup1() { }
    @TestSetup static void setup2() { }
}

But saving with the more recent versions will yield this error:

Only one method per type can be defined with: TestSetup

In the lower versions, there does not appear to be any limit in place. I stopped checking at 1011.

@IsTest
class DemoTests
{ // compiles below API Version 39.0
    @TestSetup static void setup1() { }
    @TestSetup static void setup2() { }
    @TestSetup static void setup3() { }
    //...
    @TestSetup static void setup1010() { }
    @TestSetup static void setup1011() { }
}
6
  • +1 Good to know. I completely missed the change notes on limiting test classes to one @testSetup annotated method. Out of curiosity, how many iterations did it take for you to arrive at 1011? Unless you had a script running that, it'd have to be a pretty slow day to increase the methods one per iteration.
    – Derek F
    Sep 27, 2017 at 17:40
  • I started off adding a few score at a time, then beyond a hundred started adding a couple hundred at a time.
    – Adrian Larson
    Sep 27, 2017 at 17:42
  • Cool! I remember you and I had posts on a same topic not too long ago, and then it was all legal to have more than one @testSetup methods in single test class.
    – Mahmood
    Sep 27, 2017 at 17:44
  • Guess, I need to update that post. :-D
    – Mahmood
    Sep 27, 2017 at 17:45
  • There' was never any guarantee that multiple @testSetup methods would execute in any particular order. That might be a driver behind "Only one method per type..." in version 39 Sep 27, 2017 at 20:59

You must log in to answer this question.

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