Groovy for testing an API

别说谁变了你拦得住时间么 提交于 2019-12-03 08:22:43

JUnit, TestNG, and Spock are all reasonable choices for writing (not just unit) tests in Groovy. Pick one of them and run the tests with your build system of choice. A build system like Gradle that can bootstrap itself will make life easier for QA (no installation required).

Disclaimer: I'm Spock founder and Gradle committer.

I use Groovy for this purpose too. I just wrote JUnit 4 test cases in Groovy and then wrote my own custom groovy runner script which gathers the results and prints them out for me. This is the script to call the JUnit 4 Groovy tests:

def (Result result, Duration duration) = time {
      JUnitCore.runClasses(TestA, TestB, TestC)
}

String message = "Ran: " + result.getRunCount() + ", Ignored: " + result.getIgnoreCount() + ", Failed: " + result.getFailureCount()
println ""
println "--------------------------------------------------"
println "Tests completed after " + duration
println "--------------------------------------------------"
if (result.wasSuccessful()) {
    println "SUCCESS! " + message
    println "--------------------------------------------------"
} else {
    println "FAILURE! " + message
    println "--------------------------------------------------"
    result.getFailures().each {
        println it.toString() 
    }
    println "--------------------------------------------------"
}

def time(closure) {
    DateTime start = new DateTime()
    Object result = closure()
    [result, new Duration(start, new DateTime())]
}

I wrote this script because I couldn't find a reusable JUnit 4 runner for Groovy at the time. There may be one now but this works for me.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!