【问题标题】:Running a specific @Issue via gradle通过 gradle 运行特定的@Issue
【发布时间】:2018-03-29 13:47:07
【问题描述】:

我使用 Spock 编写测试并使用 Gradle 运行它们。我使用@Issue 对它们进行注释。同一问题的测试不一定在同一个文件中:

FooTest.groovy:

class FooTest extends Specification {
  @Issue("FOOBAR-123")
  def "should foo"() {
    ...
  }
  @Issue("FOOBAR-234")
  def "should bar"() {
    ...
  }
}

BarTest.groovy:

class BarTest extends Specification {
  @Issue("FOOBAR-123")
  def "should quux"() {
    ...
  }
}

我想针对单个问题 (FOOBAR-123) 运行所有测试。

在 rspec 中很容易:

describe 'foo' do
  it "should foo", foobar-123: true do
    ...
  end

rspec --tag foobar-123

但我不知道如何使用 Spock 和 Gradle 来做到这一点。

【问题讨论】:

    标签: java gradle groovy junit spock


    【解决方案1】:

    这可以通过使用扩展来实现。你可以通过在你的项目中创建一个文件来做到这一点

    IssueIncludeExtension.groovy

    package com.tarun.lalwani
    
    import org.spockframework.runtime.extension.AbstractGlobalExtension
    import org.spockframework.runtime.model.FeatureInfo
    import org.spockframework.runtime.model.SpecInfo
    import spock.lang.Issue
    
    import java.lang.annotation.Annotation
    
    class IssueIncludeExtension extends AbstractGlobalExtension{
        @Override
        void visitSpec(SpecInfo spec) {
            def issuesList
            issuesList = System.properties["spock.issues"]
    
            if (issuesList) {
                def arrIssues = issuesList.split(",").toList()
    
                System.out.println('I was called')
                for (FeatureInfo feature : spec.getAllFeatures())
                {
                    def method, ann;
                    method = feature.getFeatureMethod();
                    def issueAnnotation = method.getAnnotation(Issue.class);
                    if (issueAnnotation) {
                        if (issueAnnotation.value().size() > 0)
                        {
                            if (issueAnnotation.value().toList().intersect(arrIssues))
                            {
                                //we have a matching issue
                                feature.setExcluded(false)
                            } else {
                                feature.setExcluded((true))
                            }
    
    
                        }
                    } else {
                        // this doesn't belong to any issue
                        feature.setExcluded(true)
                    }
                }
    
            } else {
                super.visitSpec(spec)
            }
        }
    
    
    }
    

    然后在下面创建文件

    META-INF/services/org.spockframework.runtime.extension.IGlobalExtension

    com.tarun.lalwani.IssueIncludeExtension
    

    之后你可以更新你的 gradle 脚本并添加一个测试

    task issueTest(type: Test) {
        // This task belongs to Verification task group.
        group = 'Verification'
    
        // Set Spock configuration file when running
        // this test task.
        systemProperty 'spock.issues', 'Issue4'
    }
    

    现在我在 groovy 中有以下测试

    package com.mrhaki.spock
    
    import spock.lang.Issue
    import spock.lang.Specification
    
    class WordRepositorySpec extends Specification {
    
    
        @Remote  // Apply our Remote annotation.
        @Issue(["Issue1", "Issue2"])
        def "test remote access"() {
            given:
            final RemoteAccess access = new RemoteAccess()
    
            expect:
            access.findWords('S') == ['Spock']
        }
    
        @Issue("Issue4")
        def "test local access"() {
            given:
            final LocalAccess access = new LocalAccess()
    
            expect:
            access.findWords('S') == ['Spock']
        }
    
    }
    

    运行测试只是运行Issue4相关测试

    【讨论】:

    • 谢谢您,您是 Spock 巫师! :)
    • 谢谢!老实说,只有通过你的问题我才知道 Spock 的存在 :-)
    【解决方案2】:

    根据this 文章,您可以为任何注解创建配置。

    不幸的是(经过一些尝试)我无法根据注释的值过滤测试,IncludeExcludeCriteria 类只接受类或注释。

    runner {
      include spock.lang.Issue
    }
    

    我认为您可以通过为每个问题创建注释并使用Annotations Collectors 来解决它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 2017-07-31
      • 1970-01-01
      • 2014-09-01
      • 2023-02-01
      相关资源
      最近更新 更多