如何将测试文件夹添加到较旧的Android Studio项目 [英] How to add test folders to an older Android Studio Project
问题描述
在将要测试的项目结构添加到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.
我已经成功地将目录(测试)和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 Project视图中查看软件包时,它们被列为:
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的javafolder,则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文件夹将不再被识别为javafolder,并且我无法再在其中添加类/包.并且在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项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!