I am trying to write a bunch of test codes against a Java class library and I am using specs2 on Scala.
I have a group of test cases that should be ran for all the subclasses of class T,
but could not figure out the best way to implement this.
Coming from Ruby, I naturally sought something like *shared_examples_for* macro as in Rspec, but there seems to be no such thing in specs2.
Then I thought, subclassing my own Specification subclass with test cases may be one way to go.
Is there any tidier solution to this?
You can find an example of shared examples in the User Guide. Basically this uses the fact that you can define a method taking a specific instance to test and declare examples for it (see the
nonEmptyStackmethod).If this helps let me kickstart you with a quick example using a mutable
Specification:The only thing you need to check before you do that is the possible side-effects between examples. In the specification above, all the examples are executed concurrently and they share the same object. If that creates any issue, you can either:
add
sequentialat the top of the specification to avoid concurrencychange
subclassesandexamplestodef subclasses: Seq[() => T]anddef examples(t: () => T)so that you will create a brand new instance for each example