运行groovy测试套件时出错

时间:2014-05-18 15:24:32

标签: eclipse groovy junit

在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任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

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

您应该为测试选择JUnit3或JUnit4样式(我个人倾向于选择JUnit4)。