What's the difference between `beforeTest`, `befor...
# kotest
k
What's the difference between
beforeTest
,
beforeEach
and
beforeAny
? I've read the documentation but I'm still confused.
l
I feel a bit confused as well. AFAIK they're supposed to be functionally equal, just aliases to one another, but I'm not completely sure
k
I think I figured it out - correct me if I'm wrong.
beforeTest
has the same usage as
beforeAny
and is invoked at the start of both a container and a test, whereas
beforeEach
is invoked only before a test. To illustrate:
Copy code
class MyTest : FreeSpec({
    "container1" - {
        "test1" {
            println("test1")
        }
    }

    beforeAny { println("beforeAny $it") }
    beforeEach { println("beforeEach $it") }
})
In the above, the
beforeAny
(or
beforeTest
) is invoked twice: once for the container and once again for the test. On the other hand,
beforeEach
is invoked only once, for the test.
577 Views