0

I'm trying to create a class that will act like a BaseClass for my Test classes.

It must include several variables, constants and methods common to my test methods.

The kicker is that Test methods are static and we can't inherit static variables/methods from a virtual or abstract class.

Therefore I wonder if you guys have ever had to develop something like that and, if yes, what was your approach to avoid repeating the same constants over and over again into each test class.

1
  • This post has already generated a decent amount of conversation and seems like there could be quite a bit more. As such, I've moved it to chat.
    – Adrian Larson
    Mar 30, 2017 at 19:32

1 Answer 1

3

Even if the inheritance approach was not blocked by the static method declarations, composition is arguably a better way to go - see e.g. Composition vs Inheritance and many other discussions.

You can create separate classes that you create and invoke from your static test methods to do various aspects of the work. If these classes are shared between test classes, you can make them top-level classes or if they are relevant to only one test class you can make them nested classes of the test class. These classes can use various designs, from just being simple holders of constants, to patterns such as the builder pattern. Both data setup and assertion methods can be added.

Remember that each test method is isolated from the other test methods: there is no mechanism to communicate values between the methods. So each method needs to setup its own environment.

(The @TestSetup annotation allows some shared data setup to be done but references to that data need to be queried in each test method.)

3
  • Actually what I was trying to do is something much more like having access to Constants (DO_INSERT, ALL_OR_NOTHING, things like that) that are common to all my test classes, instead of having to declare these Constants on every test class. The current solution involves moving these Constants to our TestFactory and access it by using TestFactory.CONSTANT_NAME. The idea was to have a base class that contain this constant and extend it in the Test Classes, inheriting the constants. But I don't see a way to do that and, at this point, I don't think this is the best possible design. Mar 31, 2017 at 21:12
  • @toadgeek Yeah referencing them from a separate class if they are unique to the tests and shared by several test classes makes sense. As you are probably already aware, if they are already defined as private constants in the product code you can make them accessible to tests using @TestVisible.
    – Keith C
    Apr 1, 2017 at 9:05
  • Yeah, that's how my code currently is: a separated class, unique to the tests, shared by several test classes, with all the common variables and constants. And they're also accessible to tests with the @TestVisible keyword. I was just trying to imagine a way to improve the code accessibility, but it turns out I'm already following the best practices related to it. But thanks you all for your inputs. Apr 3, 2017 at 12:33

You must log in to answer this question.

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