Groovy:junit的面向测试的输出

时间:2017-09-25 21:06:11

标签: unit-testing groovy junit

我试图按照the Groovy Testing Guide为我的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获得更多面向测试的输出?

4 个答案:

答案 0 :(得分:1)

我相信,您可以在构建方法void testCase2() { assert 1 == 2, 'expected differs from actual' } 中使用用户消息。

'<a href="javascript:toast(' + k + ');">'

答案 1 :(得分:1)

如果你想尝试spock,这里是你需要的代码:

@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)(取决于您的用例 - 您未指定)。

答案 2 :(得分:0)

我认为他们的问题是你正在使用Java运行单元测试,这正是断言的工作原理,它们会抛出异常。

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

顺便说一下,XML输出在groovy的XML解析功能中运行得很好,同时groovy的断言很好但是需要一个固定宽度的字体来绘制它的小图片 - 如果你只是自己查看结果这很好,但如果你是务实地使用它们,junit的Assert类可能会留下更好的信息。

答案 3 :(得分:0)

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

但通常情况下,你只想了解自己的方法,而不是所有底层的groovy东西,所以你使用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

也许某人有办法避免在每个测试案例中都写这个......