【问题标题】:error when running groovy test suite运行 groovy 测试套件时出错
【发布时间】:2014-07-06 13:48:28
【问题描述】:

在 groovy 中创建了两个测试,能够将它们作为 groovy 测试用例不雅地运行,但是当我创建一个测试套件并在 cmd 行上像 groovy MyTestSuite.groovy 一样运行它们时,我收到以下错误:

F.F
Time: 0
There were 2 failures:
1) warning(junit.framework.TestSuite$1)junit.framework.AssertionFailedError: No tests found in mypack.ArithmeticTest
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
        at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1318)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:927)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:77)
        at groovy.lang.GroovyShell.runJUnit3TestSuite(GroovyShell.java:370)
        at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:277)
        at groovy.lang.GroovyShell.run(GroovyShell.java:502)
        at groovy.lang.GroovyShell.run(GroovyShell.java:491)
"a.txt" 53L, 3408C

测试套件类如下

package mypack

import junit.framework.TestSuite
import junit.framework.JUnit4TestAdapter

public class myTestSuite extends TestSuite {
     // Since Eclipse launches tests relative to the project root,
     // declare the relative path to the test scripts for convenience
     private static final String TEST_ROOT = "src/mypack/";
     public static TestSuite suite() throws Exception {
         TestSuite suite = new TestSuite();
         GroovyTestSuite gsuite = new GroovyTestSuite();

         suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArithmeticGroovy.groovy"));
         suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArrayTest.groovy"));
         return suite;
     }
}

ArithmeticGroovy.groovy

package mypack

import org.junit.Test
import static org.junit.Assert.assertEquals

class ArithmeticTest {
    @Test
    void additionIsWorking() {
        assertEquals 4, 2+2
    }

    @Test(expected=ArithmeticException)
    void divideByZero() {
        println 1/0
    }
}

当我通过 Eclipse 运行它时,我得到了初始化错误。我是 groovy 的新手,感谢您的帮助

【问题讨论】:

  • 尝试重命名测试方法,以test开头。

标签: eclipse groovy junit


【解决方案1】:

如果您混淆(请原谅双关语:-) JUnit3 和 JUnit4 样式,Groovy 似乎会感到困惑。使用TestSuiteaddTestSuite 是JUnit3 风格,它应该与派生自TestCase(或最终Test)的类结合使用。如this post 所示,混合 JUnit4 注释样式在此设置中不起作用。

您应该为您的测试选择 JUnit3 或 JUnit4 样式(我个人更喜欢 JUnit4)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 2014-08-07
    • 2021-05-21
    • 2011-10-15
    • 2020-06-14
    • 2019-01-19
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多