groovy、Junit4 单元测试和相关测试运行程序
我正在尝试为我的 Groovy 代码编写 Junit4 测试用例。 Junit 4 测试用例在我的 Eclipse IDE(SpringSource Tool Suite)中运行良好。但是,我无法运行测试来运行所有测试用例。
这是我目前对测试运行程序的尝试。它几乎直接取自 Groovy 网站本身:
import groovy.util.GroovyTestSuite;
import org.codehaus.groovy.runtime.ScriptTestAdapter
import junit.framework.*;
class allTests {
static Test suite() {
def gsuite = new GroovyTestSuite()
gsuite.addTest(new ScriptTestAdapter(gsuite.compile("test/GSieveTest.groovy"), [] as String[]))
return gsuite
}
}
junit.textui.TestRunner.run(allTests.suite())
结果:出了
org.codehaus.groovy.runtime.InvokerInvocationException:
org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack:
No signature of method: GSieveTest.main() is applicable for argument types: () values: []
什么问题?哦,这是 GSieveTest.groovy。我使用“Run As Junit test...”运行良好
import static org.junit.Assert.assertEquals;
import org.junit.Test;
class GSieveTest {
@Test
public void Primes1To10() {
def sieve = (0..10).toList()
GSieve.filter(sieve); // [1,2,3,5,7]
assertEquals("Count of primes in 1..10 not correct", 5, (sieve.findAll {it -> it != 0}).size());
}
@Test
public void FiftyNineIsPrime() {
def sieve = (0..60).toList()
GSieve.filter(sieve);
assertEquals("59 must be a prime", 59, sieve[59]);
}
@Test
public void Primes1To100() {
def sieve = (0..100).toList()
GSieve.filter(sieve);
def list = sieve.findAll {it -> it != 0}
def primes = [1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
println list
println primes
assertEquals(true, list == primes)
}
}
I'm trying to write Junit4 test cases for my Groovy code. The Junit 4 test case works fine inside my Eclipse IDE which is SpringSource Tool Suite. I can't get a test running to run the all of the test cases, however.
Here is my current attempt at a test runner. It's pretty much taken directly from the Groovy website itself:
import groovy.util.GroovyTestSuite;
import org.codehaus.groovy.runtime.ScriptTestAdapter
import junit.framework.*;
class allTests {
static Test suite() {
def gsuite = new GroovyTestSuite()
gsuite.addTest(new ScriptTestAdapter(gsuite.compile("test/GSieveTest.groovy"), [] as String[]))
return gsuite
}
}
junit.textui.TestRunner.run(allTests.suite())
Results in:
org.codehaus.groovy.runtime.InvokerInvocationException:
org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack:
No signature of method: GSieveTest.main() is applicable for argument types: () values: []
What's wrong? Oh, here is the GSieveTest.groovy. I runs fine using "Run As Junit test..."
import static org.junit.Assert.assertEquals;
import org.junit.Test;
class GSieveTest {
@Test
public void Primes1To10() {
def sieve = (0..10).toList()
GSieve.filter(sieve); // [1,2,3,5,7]
assertEquals("Count of primes in 1..10 not correct", 5, (sieve.findAll {it -> it != 0}).size());
}
@Test
public void FiftyNineIsPrime() {
def sieve = (0..60).toList()
GSieve.filter(sieve);
assertEquals("59 must be a prime", 59, sieve[59]);
}
@Test
public void Primes1To100() {
def sieve = (0..100).toList()
GSieve.filter(sieve);
def list = sieve.findAll {it -> it != 0}
def primes = [1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
println list
println primes
assertEquals(true, list == primes)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也为此奋斗了很长时间,但无法让 Groovy 记录的示例与 JUnit 4 一起使用。这是我的解决方案,最终非常简单:
创建一个名为 RunAllTestScripts.groovy 的脚本:
就是这样。 JUnitCore (JUnit4) 运行程序将运行这些测试类。 TestSuites 可能有一个更优雅的解决方案,但这足以满足我的需求。结果对象包含运行、忽略、失败计数以及易于查询的详细失败列表。
I also battled with this for ages and could not get the Groovy documented samples to work with JUnit 4. This was my solution which was trivially simple in the end:
Create a script called RunAllTestScripts.groovy:
That's it. The JUnitCore (JUnit4) runner will run these test classes. There's probably a more elegant solution with TestSuites to be had but this is sufficient for my needs. The Result object contains the ran, ignored, failed counts as well as the detailed failure list which is easily interrogated.
现在我明白了——Groovy 没有用于 JUnit4 测试的测试运行器。以下是 JUnit4 支持的范围:
http://groovy.codehaus.org/Using+JUnit+4+with+ Groovy
脚本测试适配器 (ScriptTestAdapter) 允许您运行非 JUnit 测试的 groovy 脚本。这是一个例子:
Now I got it -- Groovy doesn't have a test runner for JUnit4 tests. Here is the extent of JUnit4 support:
http://groovy.codehaus.org/Using+JUnit+4+with+Groovy
What the script test adapter (ScriptTestAdapter) allows you to do is run groovy scripts that are NOT JUnit tests. Here's an example: