gpt4 book ai didi

unit-testing - 从 Spock 测试中动态排除类

转载 作者:行者123 更新时间:2023-12-02 04:28:30 24 4
gpt4 key购买 nike

我们想从我们的 Spock 测试中排除一组特定的测试类,这取决于是否设置了某些系统属性。现在我们得到了一些代码:

runner {
// skip all slow tests automatically unless test.include.slow=true
if (!Boolean.getBoolean('test.include.slow')) { exclude Slow }

// skip all api tests unless test.include.api=true
if (!Boolean.getBoolean('test.include.api')) { exclude ApiTest }
}

在我们的 SpockConfig.groovy 中。问题是,第二次调用排除实际上覆盖了第一次调用中定义的排除。我们还尝试构建一个类数组并将其处理到 exclude 函数,如下所示:

runner {

Class[] toExclude = []

// skip all slow tests automatically unless test.include.slow=true
if (!Boolean.getBoolean('test.include.slow')) { toExclude << Slow }

// skip all api tests unless test.include.api=true
if (!Boolean.getBoolean('test.include.api')) { toExclude << ApiTest }

exclude toExclude
}

然而,这会产生非常奇怪的异常:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.junit4.IdeaSuite.getDescription(IdeaSuite.java:55)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:43)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: groovy.lang.MissingMethodException: No signature of method: SpockConfig.runner() is applicable for argument types: (SpockConfig$_run_closure1) values: [SpockConfig$_run_closure1@66d33a32]
Possible solutions: run(), run(), run(java.io.File, [Ljava.lang.String;), use([Ljava.lang.Object;)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at SpockConfig.run(SpockConfig.groovy:4)
at org.spockframework.builder.DelegatingScriptBlueprint.evaluate(DelegatingScriptBlueprint.java:33)
at org.spockframework.builder.Sculpturer.$form(Sculpturer.java:32)
at org.spockframework.builder.GestaltBuilder.build(GestaltBuilder.java:19)
at org.spockframework.runtime.ConfigurationBuilder.build(ConfigurationBuilder.java:30)
at org.spockframework.runtime.RunContext.<init>(RunContext.java:54)
at org.spockframework.runtime.RunContext.createBottomContext(RunContext.java:150)
at org.spockframework.runtime.RunContext.get(RunContext.java:130)
at org.spockframework.runtime.Sputnik.runExtensionsIfNecessary(Sputnik.java:86)
at org.spockframework.runtime.Sputnik.getDescription(Sputnik.java:55)
at org.junit.runners.Suite.describeChild(Suite.java:123)
at com.intellij.junit4.IdeaSuite.describeChild(IdeaSuite.java:68)
at com.intellij.junit4.IdeaSuite.getChildren(IdeaSuite.java:85)
at org.junit.runners.ParentRunner.getFilteredChildren(ParentRunner.java:351)

那么在 SpockConfig.groovy 中处理此类排除的正确方法是什么?

最佳答案

好的,我们找到了一些解决方案:

runner {
// skip all slow tests automatically unless test.include.slow=true
if (!Boolean.getBoolean('test.include.slow')) { exclude.annotations << Slow }

// skip all api tests unless test.include.api=true
if (!Boolean.getBoolean('test.include.api')) { exclude.annotations << ApiTest }
}

仍然想知道这是正确的方法还是一些蹩脚的 hack。

关于unit-testing - 从 Spock 测试中动态排除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25138251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com