All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.MintLoggingExtension.kt Maven / Gradle / Ivy

There is a newer version: 12.1.30
Show newest version
package com.zegreatrob.testmints.logs

import io.github.oshai.kotlinlogging.KotlinLogging
import org.junit.jupiter.api.extension.AfterTestExecutionCallback
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback
import org.junit.jupiter.api.extension.ExtensionContext
import kotlin.jvm.optionals.getOrNull

class MintLoggingExtension :
    BeforeAllCallback,
    BeforeTestExecutionCallback,
    AfterTestExecutionCallback {

    private val logger by lazy { KotlinLogging.logger("testmints") }

    override fun beforeAll(context: ExtensionContext?) = StructuredLoggingTestMintsReporter.initialize()

    override fun beforeTestExecution(context: ExtensionContext?) {
        logger.atInfo {
            message = "test-start"
            payload = mapOf(
                "step" to "test",
                "state" to "start",
            )
        }
        logger.atInfo {
            message = "setup-start"
            payload = mapOf("step" to "setup", "state" to "start", "name" to context.testName())
        }
    }

    private fun ExtensionContext?.testName() = "${this?.parent?.getOrNull()?.displayName}.${this?.displayName}"

    override fun afterTestExecution(context: ExtensionContext?) = logger.atInfo {
        message = "test-finish"
        payload = mapOf("step" to "test", "state" to "finish")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy