2017-09-25 18 views
1

我试图按照the Groovy Testing Guide为我的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 
     } 

     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 

这不是很大的输出。我不需要像这样的大量堆栈跟踪,尤其是那种实际上并没有告诉我任何关于测试代码的东西。它会好得多,如果我能得到的东西更多的测试为导向,如:

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 

有没有办法在Groovy获得从JUnit的更多面向测试的输出?

+1

你试过斯波克? –

+0

@tim_yates我无法运行spock。显然它没有安装,我找不到任何关于如何安装它的指导,而不是使用gradle或maven,我也没有使用它。 – ewok

+1

发布了导致类似情况的独立spock规范 –

回答

1

我相信,你可以使用建立的方法assert与用户消息。

void testCase2() { 
     assert 1 == 2, 'expected differs from actual' 
} 
+0

的代码。我用我的自定义消息得到完整的堆栈跟踪,而不是预期的消息:预期:<1>但是:<2>' – ewok

0

如果你想尝试斯波克,这里的代码,你需要:

@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 
    } 

} 

我也建议(根据您的使用情况 - 你不指定)使用的构建工具,如Maven或Gradle。

0

我认为他们的问题是你正在用Java运行单元测试,这正是如何声明工作,他们抛出一个异常。

尝试运行org.junit.runner.JUnitCore并让它运行测试,这应该知道如何总结结果并打印它们(以各种格式,甚至是XML)而没有例外。

顺便说一下,XML输出在Groovy的XML解析能力方面效果很好,groovy的assert也很好,但是期望一个固定宽度的字体来绘制它的小图片 - 如果你只是自己查看结果,但如果你正在与他们一起务实地工作,junit的Assert类可能会留下更好的消息。

0

拥有堆栈跟踪是一个好主意,因为如果你正在测试的类实际上抛出了一个异常,而不是仅仅产生一个AssertionFailedError,你会需要它。

但其往往与常规的例外,你只需要知道你自己的方法,但并非所有底层的时髦的东西,所以你使用StackTraceUtils.sanitize情况:

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 

也许有人有办法就可以避免在每一个测试用例写这个...