https://kotlinlang.org logo
j

Joffrey

10/07/2021, 6:05 PM
Is there any way to define
@BeforeClass
/
@AfterClass
in multiplatform common tests? AFAICT these annotations are JVM-only at the moment 😞 I could add cleanup after each test, but that's a bit overkill, and may not work for every use case.
e

ephemient

10/07/2021, 6:09 PM
there's kotlin.test.BeforeClass/kotlin.test.AfterClass on native, but right, nothing for JS/common
j

Joffrey

10/07/2021, 6:22 PM
@ephemient Ok, thanks, that's what I feared... 😭
@Big Chungus this is
@Before
/`@After` behaviour, where setup/cleanup is executed for each test. I'm looking for a single setup/cleanup for all tests.
b

Big Chungus

10/07/2021, 6:24 PM
@Before Old can easily be added to that class. @After might be more tricky
Oops, accidentally tagged someone
But in most cases beforeEach and afterEach are good alternatives to before and after if you don't mind losing a bit of performance
j

Joffrey

10/07/2021, 6:31 PM
@BeforeTest
and
@AfterTest
are already supported out of the box on all platforms, though: https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/-before-test/ https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/-after-test/ So I already have this option without extra hack (which is what I'm doing now), but I wanted to have class-level hooks to avoid unnecessary work (I use these hooks to generate reports).
b

Big Chungus

10/07/2021, 6:32 PM
Hmm, can't recall why I've made my own. But I was aware of those already.
e

ephemient

10/07/2021, 6:39 PM
it looks like QUnit, Jasmine, Mocha, Jest, and Karma all support some form of beforeAll()/afterAll() (called before()/after() in some), so I feel like Kotlin's test adapter should be able to translate somehow… I don't know how to make that happen, though.
j

Joffrey

10/07/2021, 6:43 PM
Yeah I have the same feeling. Since it's explicitly stated in the
BeforeClass
docs that it's not supported in common code, I don't think the Kotlin team just "forgot". There is likely some blocker they must have faced but I'd be interested in knowing what exactly.
I haven't found any open issue in YouTrack regarding this, btw. Will probably open one for tracking
3 Views