unit-testing - Groovy:junit 的面向测试的输出

标签 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 获得更多面向测试的输出?

最佳答案

我相信,您可以在内置方法 assert 中使用用户消息。

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

关于unit-testing - Groovy:junit 的面向测试的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46414192/

相关文章:

android - 线程 "Test worker"java.lang.IllegalStateException : Module with the Main dispatcher had failed to initialize 中的异常

angular - 单元测试因 Angular 中的 NullInjectorError 而失败

gradle - 使用 gradle plugin 配置另一个 gradle 插件

groovy - 从 Gradle 运行 Groovy 脚本

java - 无法从 Class<SpringRunner> 转换为 Class<?延伸运行者>

testing - 在相同的浏览器 session 中使用 Selenium

reactjs - 使用 Jest 进行测试,如何期望结果是组件数组

android - 如何从使用 PowerMockRunner 运行的 android 单元测试加载 json 文件?

grails - Grails:如何从命令行脚本访问应用程序配置?

java - 尝试加载InitialContext时出现ClassNotFoundException : org. jboss.naming.remote.client.InitialContextFactory