Need to create project directory explicitly before each unit test
已回答
I'm using light unit tests with default fixture:
class ProjectTest : BasePlatformTestCase() {
var file: String? = null
@BeforeEach
fun setup() {
super.setUp()
file = "${project.basePath}/${UUID.randomUUID()}.tmp"
Files.createDirectories(Path.of(project.basePath)) <-- without this second test fails because of missing directory
}
@AfterEach
fun teardown() {
super.tearDown()
}
@Test
fun test1() {
assertTrue(createFile()?.exists() == true)
}
@Test
fun test2() {
assertTrue(createFile()?.exists() == true)
}
private fun createFile(): VirtualFile? {
val tempFile = File("${project.basePath}/${UUID.randomUUID()}.tmp")
tempFile.createNewFile() <-- fails here 2nd time because parent dir does not exist
tempFile.deleteOnExit()
return VfsUtil.findFileByIoFile(tempFile, true)
}
}Seems like project directory is removed on tearDown but it is not created on setUp.
Please point me to right direction if above is wrong.
请先登录再写评论。
Use the provided
CodeInsightTestFixturefrom fieldmyFixtureto create files/directories in the current test project instance.Thank for hint, but above creates file in `temp://` which is not reachable in my project because I'm using findFileByIoFile due to processing file by io paths.
Why do you use regular
java.io.Fileinstead ofVirtualFileif they're located inside the project AFAIU?Otherwise, you can employ some hack to distinguish test mode filesystem