Anton Hlinisty Anton Hlinisty - 2 months ago 11
Groovy Question

Creating spock tests in runtime

Is there any possibility to create spock tests in runtime? I tried to create several meta-methods in a loop within a GebSpec but they are ignored during the tests execution.

Or maybe somebody could suggest any workarounds?

Thanks in advance!


As @Tim mentioned, datatables are the way to go. You don't need to duplicate code in data tables, you can make them fully dynamic.

def "Check form submit params: '#login', '#pwd'. Expected - #result"(String login, String pwd, boolean result) {
      // do your test
    where: [login, pwd, result] << [ [ "user", "qwerty",  true], [ "user", "1234",  false] ]

Please note nested arrays in where clause. It can actually be a fully dynamic array created at runtime. Also please notice an @Unroll annotation, as it will give you nice test method names.