How to use ErrorCollector.throwCollectedErrors method of io.kotest.assertions.BasicErrorCollector class

Best Kotest code snippet using io.kotest.assertions.BasicErrorCollector.ErrorCollector.throwCollectedErrors

ErrorCollector.kt

Source:ErrorCollector.kt Github

copy

Full Screen

1package io.kotest.assertions2import io.kotest.mpp.stacktraces3val errorCollector: ErrorCollector = BasicErrorCollector()4enum class ErrorCollectionMode {5 Soft, Hard6}7interface ErrorCollector {8 fun getCollectionMode(): ErrorCollectionMode9 fun setCollectionMode(mode: ErrorCollectionMode)10 /**11 * Returns the errors accumulated in the current context.12 */13 fun errors(): List<Throwable>14 /**15 * Adds the given error to the current context.16 */17 fun pushError(t: Throwable)18 /**19 * Clears all errors from the current context.20 */21 fun clear()22 fun pushClue(clue: Any)23 fun popClue()24 /**25 * Returns the current clue context.26 * That is all the clues nested to this point.27 */28 fun clueContext(): List<Any>29}30open class BasicErrorCollector : ErrorCollector {31 private val failures = mutableListOf<Throwable>()32 private var mode = ErrorCollectionMode.Hard33 private val clues = mutableListOf<Any>()34 override fun getCollectionMode(): ErrorCollectionMode = mode35 override fun setCollectionMode(mode: ErrorCollectionMode) {36 this.mode = mode37 }38 override fun pushClue(clue: Any) {39 clues.add(0, clue)40 }41 override fun popClue() {42 clues.removeAt(0)43 }44 override fun clueContext(): List<Any> = clues.toList()45 override fun pushError(t: Throwable) {46 failures.add(t)47 }48 override fun errors(): List<Throwable> = failures.toList()49 override fun clear() = failures.clear()50}51fun clueContextAsString() = errorCollector.clueContext().let {52 if (it.isEmpty()) "" else it.joinToString("\n", postfix = "\n")53}54/**55 * If we are in "soft assertion mode" will add this throwable to the56 * list of throwables for the current execution. Otherwise will57 * throw immediately.58 */59fun ErrorCollector.collectOrThrow(error: Throwable) {60 when (getCollectionMode()) {61 ErrorCollectionMode.Soft -> pushError(error)62 ErrorCollectionMode.Hard -> throw error63 }64}65/**66 * The errors for the current execution are thrown as a single67 * throwable.68 */69fun ErrorCollector.throwCollectedErrors() {70 // set the collection mode back to the default71 setCollectionMode(ErrorCollectionMode.Hard)72 val failures = errors()73 clear()74 if (failures.isNotEmpty()) {75 val t = if (failures.size == 1) failures[0] else MultiAssertionError(failures)76 stacktraces.cleanStackTrace(t)77 throw t78 }79}...

Full Screen

Full Screen

ErrorCollector.throwCollectedErrors

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.BasicErrorCollector2val errorCollector = BasicErrorCollector()3try {4} catch (e: Throwable) {5errorCollector.add(e)6}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful