【问题标题】:How to make unit test folder and files appear in Android Studio Chipmunk如何使单元测试文件夹和文件出现在 Android Studio Chipmunk 中
【发布时间】:2022-12-11 03:44:37
【问题描述】:

我正在学习 Google 的“Kotlin 中的 Android 基础知识”课程,并且我在测试和单元测试部分。

在使用 Android Studio 2020 版的网站中,有 2 个文件夹没有出现在我的版本(花栗鼠)中。

Link to the part I struggle with the Google course

尝试过的解决方案,例如在 src/test y en /src/test/java/ 中手动创建文件夹,但在这两种情况下,IDE 都警告我文件夹已经存在。

Google pov My pov

我怎样才能让他们像在网站上一样可见以推进课程?

显然,这些文件夹会在您启动项目时自动创建。

先感谢您。

【问题讨论】:

标签: android-studio unit-testing kotlin


【解决方案1】:

先看我的截图 比较文件夹是否真的存在。

您还可以转到应用程序文件夹 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)
    }
}

如果要保存所有这些,请转到左上角的“文件”->“全部保存”。 现在一切都好吗?你想要的,我都在!

【讨论】:

  • 很抱歉回复晚了,我一直很忙。我也做了一些实验,事实上,这个测试文件夹在项目中不存在,即使在谷歌课程中说包或文件夹和 .kt 文件是自动创建的。我会看看我是否可以用文件和里面的编码创建两个包。如果您知道这样做很热,我将不胜感激。感谢您的回复。
  • @Mijarax 检查我对如何创建文件的编辑!
猜你喜欢
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 2016-12-17
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多