【问题标题】:Android Studio: Include library test classes in app testAndroid Studio:在应用测试中包含库测试类
【发布时间】:2019-09-04 23:24:16
【问题描述】:

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

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

所以我有模块“lib”,提供可重用的功能和模块“mobile”,包含实际的应用程序。两个模块都有自己的androidTest(仪器测试),用于测试活动。 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) 错误:找不到符号类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'
}   

【问题讨论】:

  • @PhiLab,您找到解决方案了吗?

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


【解决方案1】:

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

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

【讨论】:

  • 感谢您的建议,我试过了,但遇到了以下问题:测试实用程序类需要 lib 模块中的类。所以我得到了一个循环依赖lib &lt;-&gt; testLib,这是不可能的。示例:我有一个用于自定义 View 类的特殊 IdlingResource。在库测试和实际应用程序的测试中都需要两者
  • 这就是为什么你应该制作测试库,它将由 androidTest Compile 在两个项目中添加。顺便说一句 - 很高兴知道有一些开发人员正在编写测试;)
  • 我的 testlib 有一个 MyViewIdlingResource,它从库项目中测试 MyView。所以测试库在gradle文件中添加了库:compile project(':lib')。现在,我不能在我的库 gradle 文件中写入 androidTestCompile project(':testLib'),因为这会导致循环依赖。而且我在主项目的测试中也使用了MyViewIdlingResource,所以我不能将它移动到lib的测试文件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
相关资源
最近更新 更多