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

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

ErrorCollector.kt

Source:ErrorCollector.kt Github

copy

Full Screen

1package io.kotest.assertions2import io.kotest.mpp.stacktraces3expect val errorCollector: ErrorCollector4/**5 * Specifies an assertion mode:6 * - Hard: assertion errors are thrown immediately7 * - Soft: assertion errors are collected and throw together8 */9enum class ErrorCollectionMode {10 Soft, Hard11}12typealias Clue = () -> String13interface ErrorCollector {14 fun getCollectionMode(): ErrorCollectionMode15 fun setCollectionMode(mode: ErrorCollectionMode)16 /**17 * Returns the errors accumulated in the current context.18 */19 fun errors(): List<Throwable>20 /**21 * Adds the given error to the current context.22 */23 fun pushError(t: Throwable)24 /**25 * Clears all errors from the current context.26 */27 fun clear()28 fun pushClue(clue: Clue)29 fun popClue()30 /**31 * Returns the current clue context.32 * That is all the clues nested to this point.33 */34 fun clueContext(): List<Clue>35}36object NoopErrorCollector : ErrorCollector {37 override fun getCollectionMode(): ErrorCollectionMode = ErrorCollectionMode.Hard38 override fun setCollectionMode(mode: ErrorCollectionMode) {39 }40 override fun errors(): List<Throwable> = emptyList()41 override fun pushError(t: Throwable) {42 }43 override fun clear() {44 }45 override fun pushClue(clue: Clue) {46 }47 override fun popClue() {48 }49 override fun clueContext(): List<Clue> = emptyList()50}51open class BasicErrorCollector : ErrorCollector {52 protected val failures = mutableListOf<Throwable>()53 protected var mode = ErrorCollectionMode.Hard54 protected val clues = mutableListOf<Clue>()55 override fun getCollectionMode(): ErrorCollectionMode = mode56 override fun setCollectionMode(mode: ErrorCollectionMode) {57 this.mode = mode58 }59 override fun pushClue(clue: Clue) {60 clues.add(0, clue)61 }62 override fun popClue() {63 clues.removeAt(0)64 }65 override fun clueContext(): List<Clue> = clues.reversed()66 override fun pushError(t: Throwable) {67 failures.add(t)68 }69 override fun errors(): List<Throwable> = failures.toList()70 override fun clear() = failures.clear()71}72internal fun ErrorCollector.getAndReplace(errors: Collection<Throwable>): List<Throwable> {73 val old = errors()74 clear()75 errors.forEach { pushError(it) }76 return old77}78fun clueContextAsString() = errorCollector.clueContext().let {79 if (it.isEmpty()) "" else it.joinToString("\n", postfix = "\n") { f -> f.invoke() }80}81/**82 * If we are in "soft assertion mode" will add this throwable to the83 * list of throwables for the current execution. Otherwise will84 * throw immediately.85 */86fun ErrorCollector.collectOrThrow(error: Throwable) {87 when (getCollectionMode()) {88 ErrorCollectionMode.Soft -> pushError(error)89 ErrorCollectionMode.Hard -> throw error90 }91}92fun ErrorCollector.pushErrors(errors: Collection<Throwable>) {93 errors.forEach {94 pushError(it)95 }96}97fun ErrorCollector.collectOrThrow(errors: Collection<Throwable>) {98 pushErrors(errors)99 if (getCollectionMode() == ErrorCollectionMode.Hard) {100 throwCollectedErrors()101 }102}103/**104 * All errors currently collected in the [ErrorCollector] are throw as a single [MultiAssertionError].105 */106fun ErrorCollector.throwCollectedErrors() {107 collectiveError()?.let { throw it }108}109/**110 * All errors currently collected in the [ErrorCollector] are returned as a single [MultiAssertionError].111 */112fun ErrorCollector.collectiveError(): AssertionError? {113 val failures = errors()114 clear()115 return if (failures.isNotEmpty()) {116 if (failures.size == 1) {117 AssertionError(failures[0].message).also {118 stacktraces.cleanStackTrace(it)119 }120 } else {121 MultiAssertionError(failures).also {122 stacktraces.cleanStackTrace(it) // cleans the creation of MultiAssertionError123 }124 }125 } else null126}127inline fun <reified T> ErrorCollector.runWithMode(mode: ErrorCollectionMode, block: () -> T): T =128 getCollectionMode().let { original ->129 setCollectionMode(mode)130 try {131 block()132 } finally {133 setCollectionMode(original)134 }135 }...

Full Screen

Full Screen

ErrorCollector.pushErrors

Using AI Code Generation

copy

Full Screen

1val errorCollector = BasicErrorCollector()2errorCollector.pushErrors {3}4errorCollector.assertAll()5val errorCollector = BasicErrorCollector()6errorCollector.pushErrors {7}8errorCollector.assertAll()9val errorCollector = BasicErrorCollector()10errorCollector.pushErrors {11}12errorCollector.assertAll()13val errorCollector = BasicErrorCollector()14errorCollector.pushErrors {15}16errorCollector.assertAll()17val errorCollector = BasicErrorCollector()18errorCollector.pushErrors {19}20errorCollector.assertAll()21val errorCollector = BasicErrorCollector()22errorCollector.pushErrors {23}24errorCollector.assertAll()25val errorCollector = BasicErrorCollector()26errorCollector.pushErrors {27}28errorCollector.assertAll()29val errorCollector = BasicErrorCollector()

Full Screen

Full Screen

ErrorCollector.pushErrors

Using AI Code Generation

copy

Full Screen

1 "my test" {2 ErrorCollector.pushErrors("my custom error message") {3 }4 }5 "my test" {6 ErrorCollector.pushErrors {7 }8 }9 "my test" {10 ErrorCollector.pushErrors {11 }12 }13 "my test" {14 ErrorCollector.pushErrors {15 }16 }

Full Screen

Full Screen

ErrorCollector.pushErrors

Using AI Code Generation

copy

Full Screen

1val collector = BasicErrorCollector()2collector.pushErrors {3}4collector.pushErrors {5}6collector.pushErrors {7}8collector.pushErrors {9}10collector.pushErrors {11}12collector.assertAll()13val collector = BasicErrorCollector()14collector.pushErrors {15}16collector.pushErrors {17}18collector.pushErrors {19}20collector.pushErrors {21}22collector.pushErrors {23}24collector.assertAll()25val collector = BasicErrorCollector()26collector.pushErrors {27}28collector.pushErrors {29}30collector.pushErrors {

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