Unit testing Groovy:junit面向测试的输出
我正试图为我的Groovy代码编写一些JUnit测试,但我无法让它输出任何有用的东西。例如,如果我有:Unit testing Groovy:junit面向测试的输出,unit-testing,groovy,junit,Unit Testing,Groovy,Junit,我正试图为我的Groovy代码编写一些JUnit测试,但我无法让它输出任何有用的东西。例如,如果我有: class Test extends GroovyTestCase { void testCase1() { assertTrue true assertEquals 1, 1 } } 然后我得到这个: $ groovy Test.groovy . Time: 0.045 OK (1 test) 作为一个基本输出,这很好,但如果我能得到一些
class Test extends GroovyTestCase {
void testCase1() {
assertTrue true
assertEquals 1, 1
}
}
然后我得到这个:
$ groovy Test.groovy
.
Time: 0.045
OK (1 test)
作为一个基本输出,这很好,但如果我能得到一些关于实际运行的测试用例的信息,那就太好了。然而,更大的问题是,当某些东西出现故障时输出:
class Test extends GroovyTestCase {
void testCase1() {
assertTrue true
assertEquals 1, 1
}
void testCase2() {
assertEquals 1, 2
}
void testCase3() {
assertEquals 1, 2
}
}
结果:
$ groovy Test.groovy
..F.F
Time: 0.046
There were 2 failures:
1) testCase2(Test)junit.framework.AssertionFailedError: expected:<1> but was:<2>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1467)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:65)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)
at Test.testCase2(Test.groovy:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1446)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:951)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:83)
at groovy.lang.GroovyShell.runJUnit3Test(GroovyShell.java:375)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:295)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
2) testCase3(Test)junit.framework.AssertionFailedError: expected:<1> but was:<2>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1467)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:65)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)
at Test.testCase3(Test.groovy:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1446)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:951)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:83)
at groovy.lang.GroovyShell.runJUnit3Test(GroovyShell.java:375)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:295)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
FAILURES!!!
Tests run: 3, Failures: 2, Errors: 0
$groovy Test.groovy
…F.F
时间:0.046
有两次失败:
1) testCase2(测试)junit.framework.AssertionFailedError:应为:但为:
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
位于groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
位于groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1467)
位于org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:65)
位于org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)
在Test.testCase2(Test.groovy:10)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
位于groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
位于groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1446)
位于org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:951)
位于org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:83)
在groovy.lang.GroovyShell.runJUnit3Test(GroovyShell.java:375)上
位于groovy.lang.GroovyShell.RunScriptorManorteStorRunnable(GroovyShell.java:295)
运行(GroovyShell.java:518)
运行(GroovyShell.java:507)
在groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
在groovy.ui.GroovyMain.run(GroovyMain.java:384)
位于groovy.ui.GroovyMain.process(GroovyMain.java:370)
位于groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
位于groovy.ui.GroovyMain.main(GroovyMain.java:109)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
位于org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
2) testCase3(测试)junit.framework.AssertionFailedError:应为:但为:
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
位于groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
位于groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1467)
位于org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:65)
位于org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)
在Test.testCase3(Test.groovy:14)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
位于groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
位于groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1446)
位于org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:951)
位于org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:83)
在groovy.lang.GroovyShell.runJUnit3Test(GroovyShell.java:375)上
位于groovy.lang.GroovyShell.RunScriptorManorteStorRunnable(GroovyShell.java:295)
运行(GroovyShell.java:518)
运行(GroovyShell.java:507)
在groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
在groovy.ui.GroovyMain.run(GroovyMain.java:384)
位于groovy.ui.GroovyMain.process(GroovyMain.java:370)
位于groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
在groovy.ui.GroovyMain.main(Gro
testCase1: PASS
testCase2: FAIL
junit.framework.AssertionFailedError: expected:<1> but was:<2>
testCase3: FAIL
junit.framework.AssertionFailedError: expected:<1> but was:<2>
FAILURES!!!
Tests run: 3, Failures: 2, Errors: 0
void testCase2() {
assert 1 == 2, 'expected differs from actual'
}
@Grab('org.spockframework:spock-core:1.1-groovy-2.4')
import spock.lang.Specification
class Test extends Specification {
def "test case 1"() {
expect:
1 == 1
}
def "test case 2"() {
expect:
1 == 2
}
def "test case 3"() {
expect:
1 == 2
}
}
import org.codehaus.groovy.runtime.StackTraceUtils
class Test extends GroovyTestCase {
void testCase1() {
assertTrue true
assertEquals 1, 1
}
void testCase2() {
assertEquals 1, 1
}
void testCase3() {
try {
assertEquals 1, 2
} catch (Throwable e) {
throw StackTraceUtils.sanitize(e)
}
}
}
...F
Time: 0.047
There was 1 failure:
1) testCase3(Test)junit.framework.AssertionFailedError: expected:<1> but was:<2>
at Test.testCase3(Test.groovy:15)
at org.apache.groovy.plugin.DefaultRunners$Junit3TestRunner.run(DefaultRunners.java:99)
FAILURES!!!
Tests run: 3, Failures: 1, Errors: 0