本文介绍了如何将测试文件夹添加到旧的 Android Studio 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在将用于测试的项目结构添加到 Android Studio 中的旧 Android 项目时遇到了一些问题.

I'm having some problems adding the project structure for testing, to an older Android project in Android Studio.

当您在 Android Studio 中创建新项目时,您将获得从一开始就创建的测试目录.

When you create a new project in Android Studio, you get the directories for testing created from start.

src/test/java - for unit testing
src/androidTest/java - for Android/UI testing.

但是我们的项目没有这些,因为它是我们从 Eclipse 迁移的一个较旧的项目.

But our project didn't have those, since it's an older project we have migrated from Eclipse.

我已经成功地将用于单元测试的目录(test)和java文件夹(test/java)添加到我们的项目中,其中java目录被识别为java目录,因此我可以添加新的包和java文件.

I have succesfully added the directory (test) and java folder (test/java) for unit testing to our project, where the java directory is recognized as a java directory, so I can add new packages and java files.

当我在 Android 项目视图中查看包时,它们被列为:

When I look at the packages in the Android Project view, they're listed as:

com.ourcompany.ourapp
com.ourcompany.ourapp (test) - And here I can see my testclasses.

到目前为止一切都很好.

All good so far.

但我似乎无法为 androidTest 添加结构.

But I cannot seem add the structure for androidTest.

如果我在目录下添加androidTest目录和java文件夹,java文件夹标记为绿色,可以添加类和包.但是 testpackage 没有标记为测试.

If I add the directory androidTest and a javafolder called java under it, the java folder is marked as green, and I can add classes and packages. But the testpackage is not marked as a test.

我在 android 项目视图中得到了这个结果.

I get this result in the android project view.

com.ourcompany.ourapp
com.ourcompany.ourapp - And here is the testclass, MainActivityTest.java
com.ourcompany.ourapp (test)

当我添加androidTest/java文件夹时,build.gradle文件中出现了这一行:

When I added the androidTest/java folder, this line appeared in the build.gradle file:

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

如果我删除该行,则 androidTest/java 文件夹不再被识别为 java 文件夹,并且我无法再在那里添加类/包.而在 Android 项目视图中,MainActivityTest 类根本不再可见.

If I remove that line, then the androidTest/java folder is no longer recognized as a javafolder, and I cannot add classes/packages there anymore. And in the Android Project View, the MainActivityTest class is no longer visible at all.

在添加 androidTest 文件夹以使其被识别为测试文件夹结构时,我缺少什么?

What am I missing, when it comes to adding the androidTest folders, for it to be recognized as a test folder structure?

有什么我需要添加到 gradles 文件中的吗?

Is there something I need to add to the gradles file?

android {
    compileSdkVersion 21
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.ourcompany.ourapp"
        minSdkVersion 15
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }

    lintOptions {
        checkReleaseBuilds true
        abortOnError false
    }

    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:22.2.1'
    }

}

dependencies {
    compile 'com.android.support:support-v4:22.1.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:appcompat-v7:22.1.1'
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.+"
}

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

推荐答案

首先在src文件夹下创建两个目录.

First of all, create two directory under the src folder.

名称是;

1) androidTest

1) androidTest

2) 测试

然后在这些目录下添加java文件夹.

Then add java folders under these directories.

应该是这样的androidTest>java"和test>java".

Should be like this "androidTest > java" and "test > java".

之后,打开当前项目中的任何源文件.按 Alt + Enter 然后单击创建测试"部分.选择您想要的测试目录并编写您的测试.

After that, open any source file in the current project. Press Alt + Enter than click "Create Test" section. Choose your test directory what do you want and write your tests.

这篇关于如何将测试文件夹添加到旧的 Android Studio 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:39