在Gradle / Spock土地上有测试套件的概念吗?

时间:2016-02-24 21:08:53

标签: gradle groovy spock

这里使用Spock进行单元测试的Groovy / Gradle项目。

Spock和/或Gradle是否支持测试套件或命名的测试集?由于超出此问题范围的原因,CI服务器无法运行某些Spock测试(Specifications)。

所以将我的所有应用程序的Spock测试分成两组是很好的:

  1. ci-tests”;和
  2. local-only-tests
  3. 然后也许我们可以通过以下方式调用它们:

    ./gradlew test --suite ci-tests
    

    等。这可能吗?如果是这样,setup / config是什么样的?

4 个答案:

答案 0 :(得分:6)

您可以使用Spock注释@IgnoreIf()注释不应在CI服务器中运行的测试。

请参阅此处的文档:https://spockframework.github.io/spock/docs/1.0/extensions.html#_ignoreif

您需要做的就是让CI服务器设置一个环境变量,如果设置了该变量,则排除测试类。

Spock甚至在封闭内部都有属性以使其变得简单:

@IgnoreIf({sys.isCiServer})

答案 1 :(得分:1)

我会在build.gradle中设置子模块self.layout = QtGui.QVBoxLayout() self.select_all_cb = QtGui.QCheckBox('Check All', self.ui.tab) self.select_all_cb.setTristate(False) # Only enable tristate when necessary so the user doesn't click it through to partially checked self.select_all_cb.setChecked(True) self.select_all_cb.setStyleSheet('margin-left: 5px; font: bold') self.select_all_cb.clicked.connect(self.selectAllCheckChanged) # clicked instead of stateChanged so this doesn't get triggered by ListView's changes self.layout.addWidget(select_all_cb) self.listview = QtGui.QListView(self.ui.tab) self.listview.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.listview.setSelectionMode(QtGui.QAbstractItemView.NoSelection) self.listview.setSelectionRectVisible(False) model = QStandardItemModel() for checkItem in self.checkItems: item = QStandardItem(checkItem) item.setCheckable(True) item.setSelectable(False) item.setCheckState(QtCore.Qt.Checked) model.appendRow(item) self.listview.setModel(model) self.listview.clicked.connect(self.listviewCheckChanged) self.layout.addWidget(listview) def selectAllCheckChanged(self): ''' updates the listview based on select all checkbox ''' model = self.listview.model() for index in range(model.rowCount()): item = model.item(index) if item.isCheckable(): if self.select_all_cb.isChecked(): item.setCheckState(QtCore.Qt.Checked) else: item.setCheckState(QtCore.Qt.Unchecked) def listviewCheckChanged(self): ''' updates the select all checkbox based on the listview ''' model = self.listview.model() items = [model.item(index) for index in range(model.rowCount())] if all(item.checkState() == QtCore.Qt.Checked for item in items): self.select_all_cb.setTristate(False) self.select_all_cb.setCheckState(QtCore.Qt.Checked) elif any(item.checkState() == QtCore.Qt.Checked for item in items): self.select_all_cb.setTristate(True) self.select_all_cb.setCheckState(QtCore.Qt.PartiallyChecked) else: self.select_all_cb.setTristate(False) self.select_all_cb.setCheckState(QtCore.Qt.Unchecked) ,其中包含以下内容:

my-app-ci-test

然后将测试放在test { enabled = false } task functionalTest(type: Test) { } 中并运行src/test/groovy

或者,您可以将它们包含在同一模块中,并使用./gradlew functionalTest / test

配置functionalTestincludes任务
excludes

答案 2 :(得分:0)

如果将Junit测试运行程序用于Spock测试,则可以使用@Category批注。示例by articleofficial documentation

 public interface FastTests {
 }

 public interface SlowTests {
 }

 public interface SmokeTests
 }

 public static class A {
     @Test
     public void a() {
         fail();
     }

     @Category(SlowTests.class)
     @Test
     public void b() {
     }

     @Category({FastTests.class, SmokeTests.class})
     @Test
     public void c() {
     }
 }

 @Category({SlowTests.class, FastTests.class})
 public static class B {
     @Test
     public void d() {
     }
 }
test {
    useJUnit {
        includeCategories 'package.FastTests'
    }
    testLogging {
        showStandardStreams = true
    }
}

答案 3 :(得分:0)

您可以使用以下 SpockConfiguration.groovy 来允许通过系统属性传递包含/排除

    runner {
        exclude {
            System.properties['spock.exclude.annotations']
                ?.split(',')
                *.trim()
                ?.each {
                    try {
                        annotation Class.forName(it)
                        println "Excluding ${it}"
                    } catch (ClassNotFoundException e) {
                        println "Can't load ${it}: ${e.message}"
                    }
                }
        }
        include {
            System.properties['spock.include.annotations']
                ?.split(',')
                *.trim()
                ?.each {
                    try {
                        annotation Class.forName(it)
                        println "Including ${it}"
                    } catch (ClassNotFoundException e) {
                        println "Can't load ${it}: ${e.message}"
                    }
                }
        }
    }