Hi all, is there a way in Spek 2.0 to run some tes...
# spek
s
Hi all, is there a way in Spek 2.0 to run some test initialization code once before all tests (of all groups) are run? So something like
beforeGroup
but with a greater scope?
r
Can you post a snippet on what you are trying to achieve?
s
Please see here https://github.com/rewe-digital/katana/blob/master/core/src/test/kotlin/org/rewedigital/katana/InjectionTests.kt#L18 This works however I would like to execute this line once before all tests from all groups are run.
r
@svenjacobs your doing the correct way, you can move the
beforeGroup
inside
desribe("Injection")
which will be only executed for that group.
s
@raniejade However as I said earlier 😉 I want this code to be run only once before all groups. Not just this test group, but all other test groups, too.
r
then having on the root scope is fine. You get an extra execution for the root scope, is that ok?
s
How do I declare a root scope? Can you please show an example?
r
you’re already doing it 🙂 declaring a
beforeGroup
just before the first describe. Just realised that I have a typo in my previous reply 😂
then having on the root scope is fine -> then having one on the root scope is fine
s
But how can I assure that the test group with
beforeGroup
is run as the first test when I run
./gradlew test
? Are they called in alphabetical order? So will
A_Test.kt
run before
B_Test.kt
? And what if an order is not guaranteed?
r
beforeGroup
is not global and won’t affect other classes.
Copy code
class A: Spek({
  beforeGroup { ... }
})

class B: Spek({
  beforeGroup { ... }
})
beforeGroup
on
A
and
B
are isolated.
s
I know but I need a global solution
r
there’s no global solution. You can create an extension then just invoke it on all of your tests.