简体   繁体   English

Android检测测试和Library项目

[英]Android instrumentation tests and Library projects

I have one library project and some main projects, that depends on that library project. 我有一个图书馆项目和一些主要项目,具体取决于那个图书馆项目。

I want to write tests for main projects, but the tests common functions should be placed in the library project to reduce code duplication. 我想为主要项目编写测试,但是应该将测试常用功能放在库项目中,以减少代码重复。

I added the following lines in the library's build.gradle : 我在库的build.gradle添加了以下几行:

dependencies {
    compile Dependencies.appCompat
    compile Dependencies.recyclerView
    compile Dependencies.cardView
    compile Dependencies.playAds
    compile Dependencies.googleAnalytics

    compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
        transitive = true
    }

    // Unit testing dependencies
    androidTestCompile 'junit:junit:4.12'
    // Set this dependency if you want to use the Hamcrest matcher library
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test:runner:+'
    androidTestCompile 'com.android.support.test:rules:+'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'org.mockito:mockito-core:1.+'
    androidTestCompile "com.google.dexmaker:dexmaker:1.2"
    androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}

in the main build.gradle there is: 在主build.gradle有:

dependencies {
    compile project(':library')
}

But in the tests for main project I get the following error: error: package android.support.test.espresso does not exist for the following line 但是在主项目的测试中,我得到以下错误: error: package android.support.test.espresso does not exist对于以下行, error: package android.support.test.espresso does not exist

import android.support.test.espresso.UiController;

But espresso is imported in library project. 但是浓缩咖啡是在图书馆项目中导入的。 This looks like, that library project is not compiled as android test. 看起来,该库项目未编译为android测试。

Can this problem be fixed? 这个问题可以解决吗?

You should try creating a separate library for the common testing code. 您应该尝试为通用测试代码创建一个单独的库。 Otherwise, you will have testing code in the app apk. 否则,您将在应用程序APK中包含测试代码。

Create a new testing library (ie "test-library"), put the common code in it. 创建一个新的测试库(即“ test-library”),然后将通用代码放入其中。 Then create the following in "build.gradle" in the app directory. 然后在app目录的“ build.gradle”中创建以下内容。

dependencies {
    compile project(':library')
    androidTestCompile(':test-library')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM