Android studio - androidTest 文件夹丢失

Android studio - androidTest folder is missing

我正在使用 Espresso 进行自动化测试。

可以写入 Instrumentation 测试 类 的文件夹丢失

Screenshot of the project

我试过添加

android{    
        sourceSets{
        main { java.srcDirs = ['src/main/java'] }
        test { java.srcDirs = ['src/test/java'] }
        androidTest { java.srcDirs = ['src/androidTest/java'] }
    }

}

build.gradle 内,但没有用。

尝试使用问题中提供的其他解决方案生成仍然无效

复制并粘贴现有的测试文件夹,在对我有用的文件系统中将其重命名为 "androidTest"。

因为我的项目最近被转移到新的存储库,所以文件夹被删除了。

  1. 像创建任何其他文件夹一样创建您的 androidTest 文件夹。放在 app/src 下。如果需要,您甚至可以在 Android Studio 之外创建此文件夹。

  2. 将正确的依赖项放入 build.gradle 文件中。像这样:

    testImplementation 'junit:junit:4.12'
    testImplementation 'androidx.arch.core:core-testing:2.0.1'
    
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
    
  3. 将 testInstrumentationRunner 设置放入您的 build.gradle 文件中:

    android {
       defaultConfig {
          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
       }
    }
    
  4. 在您的测试 class 中,您将保存在 app/src/androidTest/[此处的命名空间] 下,它看起来像这样:

    import android.content.Context;
    import android.support.test.InstrumentationRegistry;
    import android.support.test.runner.AndroidJUnit4;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    import static org.junit.Assert.assertEquals;
    
    @RunWith(AndroidJUnit4.class)
    public class PrincipalTest {
    
        @Test
        public void testAppContext() {
            Context context = InstrumentationRegistry.getTargetContext();
            assertEquals("my.package.name", context.getPackageName());
        }
    
    }
    

最快的方法是自动生成文件夹并记录浓缩咖啡测试。在 Android Studio 的顶部菜单栏上,单击“运行”,然后单击 -> “录制 Espresso 测试”。

这将启动您的模拟器并打开记录器对话框。然后只需在您的模拟器上执行一些操作,然后在记录器对话框中单击“确定”。 Android Studio 将生成测试以及丢失的文件夹。您基本上会有一个模板测试设置供您开始编辑。

右键单击您的 src 文件夹并添加一个新目录,然后单击 androidTest\java