How to get test name of GebReportingSpec in setup

1.1k Views Asked by At

I am using GebReportingSpec. I want to get name of test that will be executed by using setup method. I am get null output from testName.methodName my code snippet is as below:

class CRMAppLoginSpec extends GebReportingSpec {
@Rule
public TestName testName = new TestName()

setup()
{
   String methodName = testName.methodName;     
   println (methodName)
}
def "mytest"(String myName)
{
    given: "This is my test case in geb-spock"


    when: "capture the parameter"
    def var1 = myName

    then: "Display input parameter"
    println ("Welcome to geb-spock : ${var1}" )

    where:
    myName << ["Debasish", "Prashant"]
}
1

There are 1 best solutions below

0
On

You should probably start with code that runs.

Check out this answer about working with errors on each test, it will show you how to do something at each phase of the test lifecycle. Answer

If you are trying to improve console reporting it would be best to use Spock's AbstractRunListener. Check this out

You have iterations where each method name will be identical you could use the @Unroll annotation to differentiate between them.

class TestSpec extends GebReportingSpec {
    @Rule
    public TestName testName = new TestName()

    def setup() {
        String methodName = testName.methodName;
        println(methodName)
    }

    @Unroll("Iteration: mytest #myName")
    def "mytest"() {
        given: "This is my test case in geb-spock"

        when: "capture the parameter"

        then: "Display input parameter"
        println("Welcome to geb-spock : ${myName}")

        where:
        myName << ["Debasish", "Prashant"]
    }
}