Best Kotest code snippet using io.kotest.engine.launcher.console
Dependencies.kt
Source:Dependencies.kt
...104 const val kotest_assertions_core =105 "io.kotest:kotest-assertions-core-jvm:${Versions.kotest_junit}"106 const val kotest_property =107 "io.kotest:kotest-property-jvm:${Versions.kotest_junit}"108 const val kotest_console =109 "io.kotest:kotest-runner-console-jvm:${Versions.kotest_console}"110 const val kotest_junit =111 "io.kotest:kotest-runner-junit5-jvm:${Versions.kotest_junit}"112 // Kotlin faker test dependencies113 const val kotlin_faker =114 "io.github.serpro69:kotlin-faker:${Versions.kotlin_faker}"115 // Kotlin junit5 test dependencies116 const val kotlin_test_junit =117 "io.kotlintest:kotlintest-runner-junit5:${Versions.kotlin_test}"118 // reflections test dependencies119 const val reflections =120 "org.reflections:reflections:${Versions.reflections}"121 // spek2 test dependencies122 const val spek2_dsl =123 "org.spekframework.spek2:spek-dsl-jvm:${Versions.spek2}"...
Kotest.kt
Source:Kotest.kt
1package io.kotest.gradle2import jetbrains.buildServer.messages.serviceMessages.ServiceMessagesParser3import org.gradle.api.GradleException4import org.gradle.api.file.FileCollection5import org.gradle.api.internal.file.FileCollectionFactory6import org.gradle.api.internal.file.FileResolver7import org.gradle.api.internal.tasks.testing.DefaultTestSuiteDescriptor8import org.gradle.api.internal.tasks.testing.results.DefaultTestResult9import org.gradle.api.tasks.TaskAction10import org.gradle.api.tasks.options.Option11import org.gradle.api.tasks.testing.Test12import org.gradle.api.tasks.testing.TestListener13import org.gradle.api.tasks.testing.TestOutputListener14import org.gradle.api.tasks.testing.TestResult15import org.gradle.internal.concurrent.ExecutorFactory16import org.gradle.process.internal.DefaultExecActionFactory17import org.gradle.process.internal.JavaExecAction18import java.io.PipedInputStream19import java.io.PipedOutputStream20import javax.inject.Inject21import kotlin.concurrent.thread22// gradle seems to require the class be open23open class Kotest @Inject constructor(24 private val fileResolver: FileResolver,25 private val fileCollectionFactory: FileCollectionFactory,26 private val executorFactory: ExecutorFactory27) : Test() {28 companion object {29 private const val IntellijTestListenerClassName = "IJTestEventLogger"30 private const val ReporterArg = "--reporter"31 private const val TermArg = "--termcolor"32 private const val TagsArg = "--tags"33 private const val TeamCityReporter = "teamcity"34 private const val TaycanReporter = "io.kotest.engine.reporter.TaycanConsoleReporter"35 private const val PlainColours = "ansi16"36 private const val TrueColours = "ansi256"37 }38 private val listeners = mutableListOf<TestListener>()39 private val outputListeners = mutableListOf<TestOutputListener>()40 private var tags: String? = null41 // gradle will call this if --tags was specified on the command line42 @Option(option = "tags", description = "Set tag expression to include or exclude tests")43 fun setTags(tags: String) {44 this.tags = tags45 }46 // intellij will call this to register its listeners for the test event run window47 override fun addTestListener(listener: TestListener) {48 listeners.add(listener)49 }50 override fun addTestOutputListener(listener: TestOutputListener) {51 outputListeners.add(listener)52 }53 /**54 * Returns args to be used for the tag expression.55 *56 * If --tags was passed as a command line arg, then that takes precedence over the value57 * set in the gradle build.58 *59 * Returns empty list if no tag expression was specified.60 */61 private fun tagArgs(): List<String> {62 tags?.let { return listOf(TagsArg, it) }63 project.kotest()?.tags?.orNull?.let { return listOf(TagsArg, it) }64 return emptyList()65 }66 // -- reporter was added in 4.2.167 private fun args() = when {68 isIntellij() -> listOf(ReporterArg, TeamCityReporter, TermArg, PlainColours) + tagArgs()69 else -> listOf(ReporterArg, TaycanReporter, TermArg, TrueColours) + tagArgs()70 }71 private fun exec(classpath: FileCollection): JavaExecAction {72 val exec =73 DefaultExecActionFactory.of(fileResolver, fileCollectionFactory, executorFactory, null).newJavaExecAction()74 copyTo(exec)75 exec.main = "io.kotest.engine.launcher.MainKt"76 exec.classpath = classpath77 exec.jvmArgs = allJvmArgs78 exec.args = args()79 // this must be true so we can handle the failure ourselves by throwing GradleException80 // otherwise we get a nasty stack trace from gradle81 exec.isIgnoreExitValue = true82 return exec83 }84 /**85 * Returns true if we are running inside intellij.86 * We detect this by looking to see if intellij added it's own test listener to this task, which it87 * does for any task tat extends Test.88 * The intellij test listener is an instance of IJTestEventLogger.89 */90 private fun isIntellij(): Boolean {91 return listeners.map { it::class.java.name }.any { it.contains(IntellijTestListenerClassName) }92 }93 private fun rerouteTeamCityListener(exec: JavaExecAction) {94 val input = PipedInputStream()95 exec.standardOutput = PipedOutputStream(input)96 thread {97 val root = DefaultTestSuiteDescriptor("root", "root")98 listeners.forEach {99 it.beforeSuite(root)100 }101 input.bufferedReader().useLines { lines ->102 val parser = ServiceMessagesParser()103 val callback = KotestServiceMessageParserCallback(root, listeners, outputListeners)104 lines.forEach { parser.parse(it, callback) }105 }106 listeners.forEach {107 it.afterSuite(root, DefaultTestResult(TestResult.ResultType.SUCCESS, 0, 0, 0, 0, 0, emptyList()))108 }109 }110 }111 @TaskAction112 override fun executeTests() {113 //val testResultsDir = project.buildDir.resolve("test-results")114 val sourceset = project.javaTestSourceSet() ?: return115 val result = try {116 val exec = exec(sourceset.runtimeClasspath)117 if (isIntellij()) rerouteTeamCityListener(exec)118 exec.execute()119 } catch (e: Exception) {120 println(e)121 e.printStackTrace()122 throw GradleException("Test process failed", e)123 }124 if (result.exitValue != 0) {125 throw GradleException("There were test failures")126 }127 }128}...
build.gradle.kts
Source:build.gradle.kts
...34 // JUnit Platform Launcher + Console35 testRuntimeOnly("org.junit.platform:junit-platform-launcher") {36 because("allows tests to run from IDEs that bundle older version of launcher")37 }38 testRuntimeOnly("org.junit.platform:junit-platform-console") {39 because("needed to launch the JUnit Platform Console program")40 }41 // Mainrunner42 testImplementation("de.sormuras.mainrunner:de.sormuras.mainrunner.engine:2.1.4") {43 because("executes Java programs as tests")44 }45 // jqwik46 testImplementation("net.jqwik:jqwik:1.5.6") {47 because("allows jqwik properties to run")48 }49 // Spek250 val spekVersion = "2.0.17"51 testImplementation("org.spekframework.spek2:spek-dsl-jvm:$spekVersion")52 testRuntimeOnly("org.spekframework.spek2:spek-runner-junit5:$spekVersion")53 // Spock254 testImplementation("org.spockframework:spock-core:2.0-groovy-3.0") {55 because("allows Spock specifications to run")56 }57 // Kotest58 testImplementation("io.kotest:kotest-runner-junit5:4.6.3")59 testRuntimeOnly("org.slf4j:slf4j-nop:1.7.32") {60 because("defaulting to no-operation (NOP) logger implementation")61 }62 // TestNG63 testImplementation("org.testng:testng:7.4.0") {64 because("allows writing TestNG tests")65 }66 testRuntimeOnly("org.junit.support:testng-engine:1.0.1") {67 because("allows running TestNG tests on the JUnit Platform")68 }69}70tasks {71 withType<JavaCompile>().configureEach {72 options.release.set(8)73 }74 val consoleLauncherTest by registering(JavaExec::class) {75 dependsOn(testClasses)76 val reportsDir = file("$buildDir/test-results")77 outputs.dir(reportsDir)78 classpath = sourceSets.test.get().runtimeClasspath79 main = "org.junit.platform.console.ConsoleLauncher"80 args("--scan-classpath")81 args("--include-classname", ".*((Tests?)|(Spec))$")82 args("--details", "tree")83 args("--reports-dir", reportsDir)84 }85 test {86 // useJUnitPlatform() ... https://github.com/gradle/gradle/issues/491287 dependsOn(consoleLauncherTest)88 exclude("**/*")89 }90}...
main.kt
Source:main.kt
...12 "com.github.ajalt.clikt.core.CliktCommand",13 "com.github.ajalt.mordant.TermColors"14)15private val kotest420RequiredJars = listOf(16 // the launcher and console writers17 "io.kotest.framework.console.TeamCityMessages",18 // for discovery of specs19 "io.kotest.framework.discovery.Discovery",20 // clickt to parse command line21 "com.github.ajalt.clikt.core.CliktCommand",22 // mordaunt for pretty printing23 "com.github.ajalt.mordant.TermColors"24)25private val kotest41xparams = listOf(26 "--writer", "teamcity"27)28private val kotest420params = listOf(29 "--writer", "teamcity"30)31private val kotest421params = listOf(32 "--reporter", "teamcity"33)34private fun is421(project: Project): Boolean {35 val scope = GlobalSearchScope.allScope(project)36 return JavaPsiFacade.getInstance(project).findClass(test421, scope) != null37}38private fun is420(project: Project): Boolean {39 val scope = GlobalSearchScope.allScope(project)40 return JavaPsiFacade.getInstance(project).findClass(test420, scope) != null41}42/**43 *44 * In 4.2.1, the launcher(main method) is part of engine to accommodate the gradle plugin45 * In 4.2.0, the launcher was in its own module - io.kotest.framework.launcher46 * In 4.1.3, we included the launcher as a separate dependency explicitly47 * In <4.1.3 users were required to add console dependency to the build48 */49fun determineKotestLauncher(project: Project): LauncherConfig {50 return when {51 is421(project) -> LauncherConfig(mainClass421, emptyList(), kotest421params)52 is420(project) -> LauncherConfig(mainClass420, kotest420RequiredJars, kotest420params)53 else -> LauncherConfig(mainClass41x, kotest41xRequiredJars, kotest41xparams)54 }55}56data class LauncherConfig(57 val mainClass: String,58 val requiredJars: List<String>,59 val params: List<String>60)...
console.kt
Source:console.kt
...4import io.kotest.engine.listener.TeamCityTestEngineListener5import io.kotest.engine.listener.TestEngineListener6import io.kotest.engine.listener.EnhancedConsoleTestEngineListener7/**8 * Creates a [TestEngineListener] that will write to the console, using the provided9 * [args] to determine which output format to use.10 */11internal fun createConsoleListener(args: LauncherArgs): TestEngineListener {12 return try {13 // we support "teamcity", "taycan", "enhanced" as special values14 // taycan was the name for the fancy kotest output but has been renamed to simply enhanced15 when (args.listener) {16 "teamcity" -> TeamCityTestEngineListener()17 "taycan", "enhanced" -> EnhancedConsoleTestEngineListener(colours(args))18 null -> defaultConsoleListener()19 else -> Class.forName(args.listener).getDeclaredConstructor().newInstance() as TestEngineListener20 }21 } catch (t: Throwable) {22 println(t.message)...
console
Using AI Code Generation
1import io.kotest.engine.launcher.console2fun main() {3console().execute(args)4}5import io.kotest.engine.launcher.console6fun main() {7console().execute(args)8}9import io.kotest.engine.launcher.console10fun main() {11console().execute(args)12}13import io.kotest.engine.launcher.console14fun main() {15console().execute(args)16}17import io.kotest.engine.launcher.console18fun main() {19console().execute(args)20}21import io.kotest.engine.launcher.console22fun main() {23console().execute(args)24}25import io.kotest.engine.launcher.console26fun main() {27console().execute(args)28}29import io.kotest.engine.launcher.console30fun main() {31console().execute(args)32}33import io.kotest.engine.launcher.console34fun main() {35console().execute(args)36}37import io.kotest.engine.launcher.console38fun main() {39console().execute(args)40}41import io.kotest.engine.launcher.console42fun main() {43console().execute(args)44}45import io.kotest.engine.launcher.console46fun main() {47console().execute(args)48}49import io.kotest.engine.launcher.console50fun main() {51console().execute(args)52}53import io.kotest.engine.launcher.console54fun main() {55console().execute(args)56}
console
Using AI Code Generation
1class ConsoleLauncherExample {2fun main() {3val launcher = ConsoleLauncher()4launcher.execute(listOf("path/to/test.jar"))5}6}7class ConsoleLauncherExample {8fun main() {9val launcher = ConsoleLauncher()10launcher.execute(listOf("path/to/test.jar"))11}12}13class ConsoleLauncherExample {14fun main() {15val launcher = ConsoleLauncher()16launcher.execute(listOf("path/to/test.jar"))17}18}19class ConsoleLauncherExample {20fun main() {21val launcher = ConsoleLauncher()22launcher.execute(listOf("path/to/test.jar"))23}24}25class ConsoleLauncherExample {26fun main() {27val launcher = ConsoleLauncher()28launcher.execute(listOf("path/to/test.jar"))29}30}31class ConsoleLauncherExample {32fun main() {33val launcher = ConsoleLauncher()34launcher.execute(listOf("path/to/test.jar"))35}36}37class ConsoleLauncherExample {38fun main() {39val launcher = ConsoleLauncher()40launcher.execute(listOf("path/to/test.jar"))41}42}43class ConsoleLauncherExample {44fun main() {45val launcher = ConsoleLauncher()46launcher.execute(listOf("path/to/test.jar"))47}48}49class ConsoleLauncherExample {50fun main() {51val launcher = ConsoleLauncher()52launcher.execute(listOf("path/to/test.jar"))53}54}55class ConsoleLauncherExample {56fun main() {57val launcher = ConsoleLauncher()58launcher.execute(listOf("path/to/test.jar"))59}60}61class ConsoleLauncherExample {62fun main() {
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!!