How to get name of currently running test in spock?

本小妞迷上赌 提交于 2019-12-04 02:47:15

问题


In JUnit 3, I could get the name of the currently running test like this:

public class MyTest extends TestCase {
    public void testSomething() {
        assertThat(getName(), is("testSomething"));
    }
}

How do I do this in spock? I would like to use the test name as a key in a shared resource so that tests don't interfere with each other.


回答1:


One solution is to leverage JUnit's TestName rule:

import org.junit.Rule
import org.junit.rules.TestName

class MySpec extends Specification {
    @Rule TestName name = new TestName()

    def "some test"() {
        expect: name.methodName == "some test"
    }
}

This requires JUnit 4.7 or higher.




回答2:


For spock 1.0-groovy-2.4 you can try :

def "Simple test"() {

    expect:
    specificationContext.currentIteration.name == "Simple test"
}


来源:https://stackoverflow.com/questions/8365491/how-to-get-name-of-currently-running-test-in-spock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!