Best Kotest code snippet using io.kotest.extensions.junitxml.JunitXmlReporter.write
JunitXmlReporter.kt
Source:JunitXmlReporter.kt
...27import kotlin.time.DurationUnit28@Deprecated("Now called JunitXmlReporter. Deprecated since 4.6.")29typealias JunitXmlListener = JunitXmlReporter30/**31 * A JUnit xml legacy format writer.32 *33 * This implementation handles nesting, whereas the junit implementation will only output for leaf tests.34 *35 * @param includeContainers when true, all intermediate tests are included in the report as36 * tests in their own right. Defaults to false.37 *38 * @param useTestPathAsName when true, the full test path will be used as the name. In other39 * words the name will include the name of any parent tests as a single string.40 */41class JunitXmlReporter(42 private val includeContainers: Boolean = false,43 private val useTestPathAsName: Boolean = true,44 private val outputDir: String = "test-results/test"45) : PrepareSpecListener, FinalizeSpecListener {46 companion object {47 const val DefaultBuildDir = "./build"48 // sets the build directory, to which test-results will be appended49 const val BuildDirKey = "gradle.build.dir"50 const val AttributeName = "name"51 }52 private val formatter = getDisplayNameFormatter(ProjectConfiguration().registry, ProjectConfiguration())53 private var marks = ConcurrentHashMap<KClass<out Spec>, Long>()54 private fun outputDir(): Path {55 val buildDir = System.getProperty(BuildDirKey)56 return if (buildDir != null)57 Paths.get(buildDir).resolve(outputDir)58 else59 Paths.get(DefaultBuildDir).resolve(outputDir)60 }61 override suspend fun prepareSpec(kclass: KClass<out Spec>) {62 marks[kclass] = System.currentTimeMillis()63 }64 private fun filterResults(results: Map<TestCase, TestResult>) = when (includeContainers) {65 true -> results66 false -> results.filter { it.key.type == TestType.Test }67 }68 override suspend fun finalizeSpec(kclass: KClass<out Spec>, results: Map<TestCase, TestResult>) {69 val start = marks[kclass] ?: System.currentTimeMillis()70 val duration = System.currentTimeMillis() - start71 val filtered = filterResults(results)72 val document = Document()73 val testSuite = Element("testsuite")74 testSuite.setAttribute("timestamp", ISO_LOCAL_DATE_TIME.format(getCurrentDateTime()))75 testSuite.setAttribute("time", (Duration.milliseconds(duration).toDouble(DurationUnit.SECONDS)).toString())76 testSuite.setAttribute("hostname", hostname())77 testSuite.setAttribute("errors", filtered.filter { it.value.isError }.size.toString())78 testSuite.setAttribute("failures", filtered.filter { it.value.isFailure }.size.toString())79 testSuite.setAttribute("skipped", filtered.filter { it.value.isIgnored }.size.toString())80 testSuite.setAttribute("tests", filtered.size.toString())81 testSuite.setAttribute(AttributeName, formatter.format(kclass))82 document.addContent(testSuite)83 filtered.map { (testcase, result) ->84 val name = when (useTestPathAsName) {85 true -> formatter.formatTestPath(testcase, " -- ")86 false -> formatter.format(testcase)87 }88 val e = Element("testcase")89 e.setAttribute(AttributeName, name)90 e.setAttribute("classname", kclass.java.canonicalName)91 e.setAttribute("time", result.duration.toDouble(DurationUnit.SECONDS).toString())92 when (result) {93 is TestResult.Error -> {94 val err = Element("error")95 result.errorOrNull?.let {96 err.setAttribute("type", it.javaClass.name)97 err.setText(it.message)98 }99 e.addContent(err)100 }101 is TestResult.Failure -> {102 val failure = Element("failure")103 result.errorOrNull?.let {104 failure.setAttribute("type", it.javaClass.name)105 failure.setText(it.message)106 }107 e.addContent(failure)108 }109 else -> Unit110 }111 testSuite.addContent(e)112 }113 write(kclass, document)114 }115 private fun write(kclass: KClass<*>, document: Document) {116 val path = outputDir().resolve("TEST-" + formatter.format(kclass) + ".xml")117 path.parent.toFile().mkdirs()118 val outputter = XMLOutputter(Format.getPrettyFormat())119 val writer = Files.newBufferedWriter(path, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)120 outputter.output(document, writer)121 writer.flush()122 writer.close()123 }124 private fun hostname(): String? {125 return try {126 InetAddress.getLocalHost().hostName127 } catch (e: UnknownHostException) {128 null129 }130 }131 private fun getCurrentDateTime(): LocalDateTime {132 return LocalDateTime.now(Clock.systemDefaultZone()).withNano(0)133 }134}...
write
Using AI Code Generation
1val writeMethod = junitXmlReporterClass.getDeclaredMethod("write", File::class.java)2writeMethod.invoke(junitXmlReporter, File("junit-report.xml"))3val writeMethod = junitXmlReporterClass.getDeclaredMethod("write", File::class.java)4writeMethod.invoke(junitXmlReporter, File("junit-report.xml"))5@kotest.Tag("integration")6@kotest.Tag("integration")7val junitXmlReporter = JunitXmlReporter()8val writeMethod = junitXmlReporterClass.getDeclaredMethod("write", File::class.java)9writeMethod.invoke(junitXmlReporter, File("junit-report.xml"))10@kotest.Tag("integration")11@kotest.Tag("integration")12val junitXmlReporter = JunitXmlReporter()13val writeMethod = junitXmlReporterClass.getDeclaredMethod("write", File::class.java)14writeMethod.invoke(junitXmlReporter, File("junit-report.xml"))15@kotest.Tag("integration")16@kotest.Tag("integration")17val junitXmlReporter = JunitXmlReporter()18val writeMethod = junitXmlReporterClass.getDeclaredMethod("write", File::class.java)19writeMethod.invoke(junitXmlReporter, File("junit-report.xml"))20@kotest.Tag("integration")21@kotest.Tag("integration")22val junitXmlReporter = JunitXmlReporter()23val writeMethod = junitXmlReporterClass.getDeclaredMethod("write", File::class.java)24writeMethod.invoke(junitXmlReporter, File("junit-report.xml"))25@kotest.Tag("integration")26@kotest.Tag("integration")27val junitXmlReporter = JunitXmlReporter()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!