- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
假设我们有一个如下所示的 Spock 规范类。
class SomeFeature extends Specification {
def "some scenario"() {
given: "some resource"
def resource = someResource()
when: "some action is taken"
someAction()
then: "some condition must be met"
true == someCondition()
}
}
如何提取 BDD 元语句,例如 some scenario
、given
、when
、then
?处理源文件很简单,但我想知道是否可以使用反射来实现。
顺便说一句,获取这些信息的动机是为了促进产品所有者和开发人员之间的沟通,这样产品所有者就可以在不查看源代码的情况下知道已经实现和验证了哪些行为。
非常感谢。
最佳答案
在寒冷的阳光下,这有一些问题。
我认为更好更稳定的解决方案是@PeterNiederwieser 上面的评论中的解决方案。
不过,为了繁荣起见,我会把它留在这里,因为它是一个很好的例子,说明如何从一些 Groovy 代码生成 AST 作为字符串...
我认为反射不会有帮助,因为它不会让您获得方法的内容。
您可以通过从源代码生成 AST,然后遍历它寻找感兴趣的节点来实现。
所以给定字符串中的代码,如下所示:
def code = '''import spock.*
class SomeFeature extends Specification {
def "some scenario"() {
given: "some resource"
def resource = someResource()
when: "some action is taken"
someAction()
then: "some condition must be met"
true == someCondition()
}
def "another"() {
given: 'a value 1'
def value = 1
then: '1 == 1'
value == 1
}
}'''
您可以生成一个 AST:
import org.codehaus.groovy.antlr.*
import org.codehaus.groovy.antlr.parser.*
def ast = new GroovyRecognizer(
new GroovyLexer(
new StringReader( code ) ).plumb() ).with { p ->
p.compilationUnit()
p.AST
}
然后你可以做这样的事情(这可能不是最干净的方法,我是在时间限制下);-)
while( ast ) {
if( ast.type == GroovyTokenTypes.CLASS_DEF ) {
def child = ast.firstChild.nextSibling
println "Specification '${child.text}'"
while( child && child.type != GroovyTokenTypes.OBJBLOCK ) {
child = child.nextSibling
}
if( child ) {
child = child.firstChild
while( child ) {
if( child.type == GroovyTokenTypes.METHOD_DEF ) {
def method = child.firstChild
println " Scenario '${method.nextSibling?.nextSibling?.text}'"
while( method ) {
if( method.type == GroovyTokenTypes.SLIST ) {
def statements = method.firstChild
while( statements ) {
if( statements.type == GroovyTokenTypes.LABELED_STAT ) {
def label = statements.firstChild
println " ${label.text.toUpperCase()} '${label.nextSibling?.firstChild?.text}'"
}
statements = statements.nextSibling
}
}
method = method.nextSibling
}
}
child = child.nextSibling
}
}
}
ast = ast.nextSibling
}
这给了我输出:
Specification 'SomeFeature'
Scenario 'some scenario'
GIVEN 'some resource'
WHEN 'some action is taken'
THEN 'some condition must be met'
Scenario 'another'
GIVEN 'a value 1'
THEN '1 == 1'
希望对你有帮助...
关于java - 如何从 Spock 规范类中提取 BDD 元语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887399/
在特征方法中,在 when: 中指定特征 Action 。块,其结果在后续 then: 中得到测试堵塞。通常需要准备,这在 given: 中完成条款(或 setup: 或夹具方法)。包含前提条件同样有
我尝试使用 Spy 测试但没有成功。下面的类是一个 Sut。 public class FileManager { public int removeFiles(String director
我希望能够在运行一些自动化测试时记录 spock 功能名称和子句标签。这将有助于在使用 headless 浏览器进行自动化时调试测试问题,特别是 phantomjs。原因是,phantomjs 的行为
如何以编程方式跳过 Spock 框架中的测试?我知道我可以 annotate a test with @Ignore 跳过它,或使用 @IgnoreIf跳过基于环境变量等的测试。但是有没有办法运行任意
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
为简单起见,我们来看一个非常简单的类: public class TestingClass { public void method1(){ System.out.printl
Spock 只允许从 where 块访问静态变量。 是否有任何解决方法可以在 where 块中使用哪些实例变量? 最佳答案 您可以使用 @Shared 注释实例变量,见 http://spockfra
我正在使用 Spock 框架进行测试,一切都很好,直到今天;我不知道发生了什么。 Intellij 说“配置 Groovy sdk”所以我下载了 groovy sdk 2.4.9 并配置了它,但是在我
我正在为 grails 2.1.1 应用程序的一组现有测试添加第一个 spock 集成测试。使用以下方法运行时,测试运行和测试通过: grails test-app integration:spock
我过去曾在其他项目中使用旧版本的 robolectric 使用 robospock 和 electricspock 对 robolectric 进行过 spock 测试。我的新项目使用 robolec
我正在使用 Maven Surefire 插件运行一组 Spock 测试作为集成测试用例。我知道我们可以使用 @Shared 关键字在单个文件中跨规范的固定装置共享资源。 但是,是否可以在不同的规范文
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: def "findAll() Expected"(){ given: "The UR
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: @FailsWith(java.lang.AssertionError.class) def
我正在为我的插件创建 Spock 测试 project-plugin我的主要项目名称是 main-project正在使用 project-plugin作为插件。因此,当我为我的插件创建 Spock 测
在JUnit 3中,我可以这样获得当前正在运行的测试的名称: public class MyTest extends TestCase { public void testSomething(
我有一些类似Java的东西: public interface EventBus{ void fireEvent(GwtEvent event); } public class SaveCom
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
我遇到的问题是当我尝试在 then 中验证时阻止已抛出异常,并且已进行模拟调用。 看看下面的设置: class B { def b(A a) { a.a() } } c
我是一名优秀的程序员,十分优秀!