安卓工作室 : Include library test classes in app test

标签 android android-studio android-gradle-plugin android-espresso

我的项目文件夹结构的(相关)部分如下

├───lib
│   └───src
│       ├───androidTest
│       │   └───com.example.lib
│       │       └───utils
│       │       └───...
│       └───main
│           └───com.example.lib
│               └───...
└───mobile
    └───src
        ├───androidTest
        │   └───com.example.app
        │       └───...
        └───main
            └───com.example.app
                └───...

所以我有模块“lib”,提供可重用的功能和模块“mobile”,包含实际的应用程序。这两个模块都有自己的 androidTest(仪器测试),用于测试 Activity 。 lib 测试代码还包含实用程序类,例如lib/src/androidTest/com.example.app/utils/TestUtils.java:

package com.example.lib;

/**
 * Utility functions for tests
 */
public class TestUtils {

    public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) {
        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("with " + childPosition + " child view of type parentMatcher");
            }

            @Override
            public boolean matchesSafely(View view) {
                if (!(view.getParent() instanceof ViewGroup)) {
                    return parentMatcher.matches(view.getParent());
                }

                ViewGroup group = (ViewGroup) view.getParent();
                View child = group.getChildAt(childPosition);
                return parentMatcher.matches(view.getParent()) && child != null && child.equals(view);
            }
        };
    }
    ...

使用 lib 测试模块中的这个 TestUtils 类是可行的,但是当我从移动测试模块中调用它们时,编译器会提示:

Error:(28, 19) error: cannot find symbol class TestUtils

例如在文件 mobile/src/androidTest/com.example.app/SettingActivityTest.java 中:

package com.example.app;
import de.ioxp.lib.TestUtils; // This line results in the error, but IntelliJ opens the correct file when clicking on it.

@RunWith(AndroidJUnit4.class)
@LargeTest
public class SettingActivityTest {
    ...

所以我的问题是:如何在我的主应用的测试套件中使用我的库测试套件中的类?

我已经为我的 mobile/build.gradle 添加了一个明确的 androidTestCompile 库,但这没有任何结果:

dependencies {
    compile project(':lib')
    androidTestCompile project(':lib') // this line makes no difference, maybe I have to address the lib's testing directly. But how?

    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2';
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}   

最佳答案

那是因为你的库的 androidTest 部分没有编译成移动目标。有两种方法可以解决此问题。

您可以将测试实用程序类移动到您的库源(主要),或者您可以将测试实用程序移动到外部库并通过您的库和移动设备中的 testAndroidCompile 添加它。

关于安卓工作室 : Include library test classes in app test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769654/

相关文章:

android - 如何重复显示警告对话框或者用户单击确定?

java - 当我尝试使用 setIcon 或 SetLogo 添加应用程序时,Android Studio 中的操作栏上缺少图标

Android Studio 将所有 aar 文件包含在一个目录中

android - 找不到与包名称匹配的客户端 (Google Analytics) - 多个 productFlavors/application id

android - API 21 中的 SwipeRefreshLayout

具有两列的 Android 布局

android - react native :how to understand device is rooted?

java - Android Studio, ListView ,java

android - 警告 : API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'

php - JSON 异常没有返回结果