Search code examples
unit-testingscalaspecs2parameterized-unit-test

How do you write a parameterized test using specs?


I have several different implementations of a trait that I would like to test, and the test only uses the method signatures of the trait, so it seems like I should be able to use parameterized tests. However, the specs2 website doesn't seem to describe a straightforward way of writing parameterized tests. The closest is how to "share examples" but you still need to write every combination of tests and tested code, where I want to be able to specify:

A. Tests
B. Classes to test

That can be specified separately, but will test the cartesian product of the two.


Solution

  • Write some thing like:

    trait TraitTest extends Specification {
        val thingWithTrait: TraitWithVariousImplementations
    
    //TESTS GO HERE
    
    }
    
    class TestFoo extends TraitTest {
        val thingWithTrait = new Foo
    }
    
    class TestBar extends TraitTest {
        val thingWithTrait = new Bar
    }