SoapUIでRESTログ取得

概要

SoupUIでRESTのログを取得するサンプルを備忘録として残しておきます。

参考:https://www.soapui.org/rest-testing/getting-started.html

swagger:https://petstore.swagger.io/

[findByStatus]は応答に時間がかかるので、 この例では、Find pet by ID を使用します。

SoupUIプロジェクト構成

groovyソース

「Test Steps」にマウスカーソルを合わせて、右クリック。「Add Step」–>「Groovy Script」の順で追加します。

★LoggerJson

// TestCaseName
def testCaseName = testRunner.testCase.name
// Date format
def fileNameDate = new Date().format("yyyyMMddHHmmss")
def resultListFileNameDate = new Date().format("yyyyMMddHH")

// TestTime
def testTime = new Date()
// Log File Path
def dir =  testRunner.testCase.testSuite.project.getPropertyValue("outLogPath")
// Log File Name
def fileName = "${testCaseName}_${fileNameDate}.log"
def testCaseFilePrefix = testRunner.testCase.testSuite.getPropertyValue("resultListFilePrefix")
def testResultListCsvFileName = "${testCaseFilePrefix}_${resultListFileNameDate}.csv"
def testAssertionFIlePrefix = testRunner.testCase.testSuite.getPropertyValue("assertionListFilePrefix")
def testAssertionListCsvFileName = "${testAssertionFIlePrefix}_${resultListFileNameDate}.csv"

// Output File
def outputFile = new File(dir, fileName)
def testResultListFile = new File(dir, testResultListCsvFileName)
def testAssertionListFile = new File(dir, testAssertionListCsvFileName)

// Log title
outputFile.append("########################################\n")
outputFile.append("# " + testCaseName + "\n")
outputFile.append("########################################\n")

// Test time
outputFile.append("Test time: " + testTime + "\n")

// Current Step Index
def currentStepIdx = context.currentStepIndex
// Previous Step
def previousStep = testRunner.testCase.getTestStepAt(currentStepIdx -1 )
outputFile.append("\n")

// Request message
outputFile.append("### Request message ###\n")
outputFile.append(previousStep.testRequest.messageExchange.rawRequestData)
outputFile.append("\n\n")

// Response message
outputFile.append("### Response message ###\n")
outputFile.append(previousStep.testRequest.messageExchange.rawResponseData)
outputFile.append("\n")

// Step Index
def stepIdx = 0
// Assertion Index
def assertionIdx = 0

// testRunner size
def resCount = testRunner.results.size()

// Test Target
def testTarget = ""

// Get testCase result
for (testCaseResult in testRunner.results){
    // Step Index
    stepIdx = stepIdx +1

    // Judge Test Target
    if (resCount==stepIdx){
        testTarget = "Target"
    } else {
        testTarget = "NotTarget"
    }
    testResultListFile.append("${testCaseName}")
    testResultListFile.append(","+stepIdx)
    testResultListFile.append(","+testTarget)
    testResultListFile.append(","+testCaseResult.getStatus().toString())
    testResultListFile.append("\n")

    // Initialize Assertion Index
    assertionIdx = 0
    // Get Assertion List
    for (assertion in testCaseResult.getTestStep().getAssertionList()) {
        // Increment
        assertionIdx = assertionIdx +1
        // Assertion Result
        testAssertionListFile.append("${testCaseName}")
        testAssertionListFile.append(","+stepIdx)
        testAssertionListFile.append(","+testTarget)
        testAssertionListFile.append(","+assertionIdx)
        testAssertionListFile.append(","+testCaseResult.getTestStep().name)
        testAssertionListFile.append(","+assertion.getLabel())
        testAssertionListFile.append(","+assertion.getStatus())
        testAssertionListFile.append(","+assertion.getErrors())
        testAssertionListFile.append("\n")
   }
}

★SaveResponseJson

// JsonSlurper
import groovy.json.JsonSlurper;

// Current Step Index
def currentStepIdx = context.currentStepIndex;
// Previous Step
def previousStep = testRunner.testCase.getTestStepAt(currentStepIdx -2 );

// Response message
def resMsg = new String(previousStep.testRequest.response.contentAsString);
def jSlurper = new groovy.json.JsonSlurper().parseText(resMsg);
def name = "${jSlurper.name}";

// Save Response
if (name == "") {
    testRunner.testCase.testSuite.setPropertyValue("resName","Name Error!")
} else {
    testRunner.testCase.testSuite.setPropertyValue("resName",name)
}

テスト実行

「Test Suite 1」の実行ボタン(右三画ボタン)を押下します。

ログの内容(サンプル)

TestCase 1_20191104133131.log

########################################
# TestCase 1
########################################
Test time: Mon Nov 04 13:31:31 JST 2019

### Request message ###
GET https://petstore.swagger.io/v2/pet/1845563262948980215 HTTP/1.1
Accept-Encoding: gzip,deflate
Host: petstore.swagger.io
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)


### Response message ###
HTTP/1.1 200 OK
Date: Mon, 04 Nov 2019 04:31:30 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Headers: Content-Type, api_key, Authorization
Content-Type: application/json
Connection: close
Server: Jetty(9.2.9.v20150224)

{"id":1845563262948980215,"category":{"id":0,"name":"string"},"name":"doggie","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}

resultList_2019110413.csv

TestCase 1,1,Target,OK

assertList_2019110413.csv

TestCase 1,1,Target,1,Method 1 - Request 1,JsonPath Count,VALID,null
TestCase 1,1,Target,2,Method 1 - Request 1,JsonPath Match,VALID,null
タイトルとURLをコピーしました