Groovy for testing an API

最后都变了- 提交于 2020-01-01 03:26:14

问题


I'm writing a series of automated, end-to-end test cases that exercise a RestFUL API.

I have several good test scripts, written in Groovy, that provide the sort of tests and build the confidence we need, and we're looking at integrating these into a nightly build, as well as allow the QA team to run them. This is a step above Unit testing, as we are looking at complete end-to-end workflows, rather than atomic steps.

The output is currently human readable, with each test condition printing out a line which defines the test, the value being read, and a true/false to show if the test condition passes.

I'd like to wrap this into a higher level script that calls each script individually and then analyzes the outputs. I can do this easily enough myself, but was wondering if there's a Groovy Test framework out there already so I don't re-invent the wheel.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4868467/groovy-for-testing-an-api

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