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

io.qameta.allure.kotlin.junit4.AllureJunit4.kt Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package io.qameta.allure.kotlin.junit4

import io.qameta.allure.kotlin.Allure
import io.qameta.allure.kotlin.AllureLifecycle
import io.qameta.allure.kotlin.model.*
import io.qameta.allure.kotlin.util.AnnotationUtils
import io.qameta.allure.kotlin.util.AnnotationUtils.getLabels
import io.qameta.allure.kotlin.util.AnnotationUtils.getLinks
import io.qameta.allure.kotlin.util.ResultsUtils
import io.qameta.allure.kotlin.util.ResultsUtils.createFrameworkLabel
import io.qameta.allure.kotlin.util.ResultsUtils.createHostLabel
import io.qameta.allure.kotlin.util.ResultsUtils.createLanguageLabel
import io.qameta.allure.kotlin.util.ResultsUtils.createPackageLabel
import io.qameta.allure.kotlin.util.ResultsUtils.createSuiteLabel
import io.qameta.allure.kotlin.util.ResultsUtils.createTestClassLabel
import io.qameta.allure.kotlin.util.ResultsUtils.createTestMethodLabel
import io.qameta.allure.kotlin.util.ResultsUtils.createThreadLabel
import io.qameta.allure.kotlin.util.ResultsUtils.getStatus
import io.qameta.allure.kotlin.util.ResultsUtils.getStatusDetails
import io.qameta.allure.kotlin.util.ResultsUtils.md5
import org.junit.Ignore
import org.junit.runner.Description
import org.junit.runner.Result
import org.junit.runner.notification.Failure
import org.junit.runner.notification.RunListener
import java.util.*

/**
 * Allure Junit4 listener.
 */
@RunListener.ThreadSafe
class AllureJunit4 @JvmOverloads constructor(val lifecycle: AllureLifecycle = Allure.lifecycle) :
    RunListener() {
    private val testCases: ThreadLocal = object : InheritableThreadLocal() {
        override fun initialValue(): String = UUID.randomUUID().toString()
    }

    override fun testRunStarted(description: Description) { //do nothing
    }

    override fun testRunFinished(result: Result) { //do nothing
    }

    override fun testStarted(description: Description) {
        val uuid = testCases.get()
        val result = createTestResult(uuid, description)
        lifecycle.scheduleTestCase(result)
        lifecycle.startTestCase(uuid)
    }

    override fun testFinished(description: Description) {
        val uuid = testCases.get()
        testCases.remove()
        lifecycle.updateTestCase(uuid) { testResult: TestResult ->
            if (testResult.status == null) {
                testResult.status = Status.PASSED
            }
        }
        lifecycle.stopTestCase(uuid)
        lifecycle.writeTestCase(uuid)
    }

    override fun testFailure(failure: Failure) {
        val uuid = testCases.get()
        lifecycle.updateTestCase(uuid) { testResult: TestResult ->
            with(testResult) {
                status = getStatus(failure.exception)
                statusDetails = getStatusDetails(failure.exception)
            }
        }
    }

    override fun testAssumptionFailure(failure: Failure) {
        val uuid = testCases.get()
        lifecycle.updateTestCase(uuid) { testResult: TestResult ->
            with(testResult) {
                status = Status.SKIPPED
                statusDetails = getStatusDetails(failure.exception)
            }
        }
    }

    override fun testIgnored(description: Description) {
        val uuid = testCases.get()
        testCases.remove()

        val result = createTestResult(uuid, description).apply {
            status = Status.SKIPPED
            statusDetails = getIgnoredMessage(description)
            start = System.currentTimeMillis()
        }
        lifecycle.scheduleTestCase(result)
        lifecycle.stopTestCase(uuid)
        lifecycle.writeTestCase(uuid)
    }

    private fun getDisplayName(result: Description): String? {
        return result.getAnnotation(DisplayName::class.java)?.value
    }

    private fun getDescription(result: Description): String? {
        return result.getAnnotation(io.qameta.allure.kotlin.Description::class.java)?.value
    }

    private fun extractLinks(description: Description): List {
        val result = ArrayList(getLinks(description.annotations))
        description.testClass
            ?.let(AnnotationUtils::getLinks)
            ?.let(result::addAll)
        return result
    }

    private fun extractLabels(description: Description): List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy