eygraber
07/17/2025, 8:10 AM@BeforeTest
and @AfterTest
functions in my test classes to cleanup database files, and what was happening was that tests from one target would delete the database files while tests from other targets were running.
I ended up with what I think is a subpar solution (central function for creating and cleaning up the database with a test callback, and randomly generated database names) that I'd like to eventually improve. Is there a better way to handle this from commonTest
?Tóth István Zoltán
07/17/2025, 8:58 AM@Test
fun processJson() {
val path = clearedTestPath().resolve("test.json")
// ...
}
I've found this approach very comfortable.
Doc of `clearedTestPath`:
> Get the path to a directory where a specific unit test can store its temporary files.
>
> Composes a unique, fully qualified path for a unit test.
>
> Creates the directory if it does not exist.
>
> Deletes the content of the directory if it exists.
>
> When used with the plugin active (which is almost everywhere, except core, gradle-plugin and kotlin-plugin), callSiteName is the fully qualified name of the function that calls clearedTestPath.
>
> Example:
>
> package some.test.pkg
>
> class SomeTest {
> @Test
> fun someTest() {
> val testDir = clearedTestPath()
> }
>
> Creates the directory:
>
> ./build/adaptive/test/JVM/some.test.pkg.SomeTest.someTest
>
> On iOS the tests are put into a directory like this:
>
> /Users/tiz/Library/Developer/CoreSimulator/Devices/D6554821-AD5A-46BC-9E25-A83F1BA38C9E/data/build/adaptive/test/iOS/Tóth István Zoltán
07/17/2025, 9:00 AMeygraber
07/17/2025, 9:15 AMTóth István Zoltán
07/17/2025, 9:19 AMTóth István Zoltán
07/17/2025, 9:20 AMeygraber
07/17/2025, 9:25 AMephemient
07/17/2025, 9:41 AMexpect
-actual
Tóth István Zoltán
07/17/2025, 9:45 AMephemient
07/17/2025, 9:45 AM__FILE__
__TYPE__
__MEMBER__
__LINE__
supportTóth István Zoltán
07/17/2025, 9:46 AMephemient
07/17/2025, 9:46 AMTóth István Zoltán
07/17/2025, 9:47 AMTóth István Zoltán
07/17/2025, 9:48 AMephemient
07/17/2025, 9:51 AMephemient
07/17/2025, 9:52 AMephemient
07/17/2025, 9:54 AMephemient
07/17/2025, 9:55 AMTóth István Zoltán
07/17/2025, 9:58 AMTóth István Zoltán
07/17/2025, 9:59 AMTóth István Zoltán
07/17/2025, 10:00 AMephemient
07/17/2025, 10:01 AMrunTest
? that works the same as blocking tests on all platforms (except JS where they turn into promises that Mocha awaits)Tóth István Zoltán
07/17/2025, 10:04 AMrunTest
but not everywhere, depending the situation. I also start coroutines in other dispatchers to perform tasks.eygraber
07/17/2025, 10:33 AM