stojan
01/20/2023, 10:23 AMval testContext: Context = ApplicationProvider.getApplicationContext()
val testCoroutineDispatcher = StandardTestDispatcher()
val testCoroutineScope = TestScope(testCoroutineDispatcher + Job())
val testDataStore: DataStore<Preferences> = PreferenceDataStoreFactory.create(
scope = testCoroutineScope,
produceFile = { testContext.preferencesDataStoreFile("test_datastore") }
)
in my tests I use testCoroutineScope.runTest {}
what is the proper way to run a suspend function (to do some clean up) after every test using @After
?
things I tried and didn't work:
testCoroutineScope.runTest {}
runBlocking {}
runBlocking(testCoroutineDispatcher) {}
Sam
01/20/2023, 10:35 AM@After
method.
One way to do it is with a wrapper function:
fun TestScope.runTestAndCleanup(body: TestScope.() -> Unit) = runTest {
try {
body()
} finally {
cleanup()
}
}