【问题标题】:Run GroovyTestCase in embedded Groovy?在嵌入式 Groovy 中运行 GroovyTestCase?
【发布时间】:2018-02-11 02:26:38
【问题描述】:

如何在 Java 程序中运行的 Groovy 脚本中运行 GroovyTestCase 单元测试?

这里的用途是在 SoapUI 中运行 GroovyTestCases。

给定:

class ExperimentClassTest extends GroovyTestCase {
    void testExperimentClass() {
        assertEquals( new ExperimentClass(name: "Hi").name, "Hi" )
    }
}
class ExperimentClass {String name}

然后(工作):

C:\groovytest>\groovy2.6.0\bin\groovy .\ExperimentClassTest.groovy
.
Time: 0.041

OK (1 test)

但鉴于此(SoapUI 运行的 Groovy 脚本的内容):

evaluate (new File ('C:/groovytest/ExperimentClassTest.groovy'))

然后(不起作用):

groovy.lang.MissingMethodException: No signature of method: ExperimentClassTest.main()

也试过了:

new ExperimentClassTest().run()

...确实返回了一个 junit.framework.TestResult,但测试并没有真正运行...

new ExperimentClassTest().run().failures().collect {it}

返回

"TestCase.fName cannot be null"

【问题讨论】:

    标签: java unit-testing groovy


    【解决方案1】:

    这应该可行:

    new GroovyShell().run( new File('./ExperimentClassTest.groovy'), [] )
    

    实际上当你以groovy .\ExperimentClassTest.groovy运行它时

    GroovyShell 被调用并按类检测如何运行它

    https://github.com/groovy/groovy-core/blob/master/src/main/groovy/lang/GroovyShell.java#L295

    最后,GroovyShell 大约使用这段代码来运行测试用例类:

    def scriptClass = this.getClass().getClassLoader().parseClass( new File('./ExperimentClassTest.groovy') )
    def suite = new junit.framework.TestSuite(scriptClass)
    def result = junit.textui.TestRunner.run(suite)
    

    【讨论】:

    • 谢谢。正是我需要的。
    猜你喜欢
    • 2016-12-06
    • 2013-09-01
    • 2014-07-15
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多