Gaugeでscenarioごとにオブジェクトを初期化する

Gaugeのドキュメントを眺めていたら気がついたのでメモします。

Gaugeのドキュメントっていうのはこちら↓です。

docs.gauge.org

GaugeをJavaで利用しているのであれば、 env/default/java.properties の設定でscenarioまたは特定のスコープでオブジェクトを初期化することができます。というか、デフォルトでscenarioごとに初期化されます。初期状態で以下のように設定されているはずです。

# specify the level at which the objects should be cleared
# Possible values are suite, spec and scenario. Default value is scenario.
gauge_clear_state_level = scenario

この .properties ファイルは言語ごとの設定を書くファイルのようです。

例えば次のようなstepの実装があったとします。この httpResponse はscenarioごとに初期化されnullに戻されます。

package playground.booookstore

import com.thoughtworks.gauge.Step
import io.github.nomisrev.JsonPath
import io.github.nomisrev.path
import io.github.nomisrev.string
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlin.test.assertEquals

class Steps {

    private val httpClient = HttpClient(CIO)

    private var httpResponse: HttpResponse? = null

    @Step("オーダーIDが<orderId>であるオーダーを取得する")
    fun getOrderByOrderID(orderId: String) = runBlocking {
        httpResponse = httpClient.get("http://localhost:8080/order/$orderId")
    }

    @Step("レスポンスステータスが<httpResponseStatusCode>である")
    fun assertHttpResponseStatusCode(httpResponseStatusCode: String) {
        assertEquals(200, httpResponse!!.status.value)
    }

    @Step("レスポンスのJSONの<jsonPath>は<orderId>である")
    fun assertRespondOrderId(jsonPath: String, orderId: String) = runBlocking {
        val body = httpResponse!!.body<String>()
        val respondJsonElement: JsonElement = Json.decodeFromString<JsonElement>(body)
        val jsonPathOrderId = JsonPath.path(jsonPath).string
        assertEquals(orderId, jsonPathOrderId.getOrNull(respondJsonElement), "respond body is $body")
    }

}

scenarioごとで使いまわしたいのであれば、次のようにspecification headingの下に対象のオブジェクトを初期化するstepを持ってくるようにします。

# GET /order/{id}
* オーダーIDが"ad86ffdd-d891-4261-b29b-76ee631c28fa"であるオーダーを取得する

## オーダーIDを指定しオーダーを取得できる
* レスポンスステータスが"200"である
* レスポンスのJSONの"id"は"ad86ffdd-d891-4261-b29b-76ee631c28fa"である
* レスポンスのJSONの"orderDateTime"は"2023-09-23T22:18:11"である

## ショップ情報を取得できる
* レスポンスのJSONの"shop.id"は"SA0078"である
* レスポンスのJSONの"shop.name"は"さいたま川越一号店"である