先看我的截图 比较文件夹是否真的存在。
您还可以转到应用程序文件夹 diceroller/app/src/test/java/com/example/diceroller 中的 Windows 文件资源管理器,查看 ExampleUnitTest.kt 是否存在。
也许最后 3 个文件夹是包文件夹可能不存在。
老实说,我不知道包文件夹和 ExampleUnitTest.kt 是否存在。
首先,为了能够创建包文件夹,创建目录文件夹,里面有包文件夹:
然后,双击“java”文件夹创建:
当然对 androidTest 文件夹做同样的事情。右键单击 androidTest 文件夹 -> 新建 -> 目录。然后双击“java”。如您所知,androidTest 是仪器测试文件夹,而 test 文件夹是单元测试文件夹。
要创建包文件夹,请右键单击“java”->new->package:
然后:
并输入 com.example.diceroller 然后回车。
如果你愿意,我也会告诉你如何创建 ExampleUnitTest.kt。
抱歉,如果我不知道显示文件夹和测试的任何按钮。
有什么想问我的!
创建 ExampleUnitTest.kt:
右键单击您创建的“测试”文件夹 -> “新建” -> “Kotlin 类/文件”。
然后通过键入 ExampleUnitTest 命名它并输入。
将此源代码复制粘贴到您的 ExampleUnitTest 文件中:
package com.example.myapplication
import org.junit.Test
import org.junit.Assert.*
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
对创建插桩测试文件执行相同操作,但不是右键单击“test”文件夹,而是右键单击“androidTest”文件夹 -> “New” -> “Kotlin Class/File”。
然后将其命名为 ExampleInstrumentedTest 并输入。
之后将此源代码复制粘贴到您的 ExampleInstrumentedTest 文件中:
package com.example.myapplication
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.myapplication", appContext.packageName)
}
}
如果要保存所有这些,请转到左上角的“文件”->“全部保存”。
现在一切都好吗?你想要的,我都在!