Android Studio中:在应用测试包括图书馆测试类
问题描述:
我的项目的文件夹结构的(相关)部分如下Android Studio中:在应用测试包括图书馆测试类
├───lib
│ └───src
│ ├───androidTest
│ │ └───com.example.lib
│ │ └───utils
│ │ └───...
│ └───main
│ └───com.example.lib
│ └───...
└───mobile
└───src
├───androidTest
│ └───com.example.app
│ └───...
└───main
└───com.example.app
└───...
所以我有模块“LIB”,提供可重复使用的功能和模块“移动“,包含实际的应用程序。两个模块都有自己的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) 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 {
...
所以我的问题是:如何使用从我的媒体库的测试套件类在我的主要应用程序的测试套件?
我已经加入了对库到我的移动/的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部分编译成机动目标。有两种方法可以解决这个问题。
您可以将test util类移动到您的库源文件(main),也可以将test util移动到外部lib文件夹中,并通过库和移动设备中的testAndroidCompile添加它。
感谢您的建议,我试过了,但我遇到了以下问题:测试实用程序类需要来自lib模块的类。所以我得到一个循环依赖''lib testLib''这是不可能的。示例:我为自定义View类提供了一个特殊的IdlingResource。这两个都需要在库测试和实际应用程序的测试 – PhilLab
这就是为什么你应该做测试库,这将通过androidTest编译在两个项目中添加。顺便说一句 - 很高兴知道有些开发人员正在编写测试;) –
我的testlib有一个''MyViewIdlingResource'',用于测试库项目中的'MyView''。因此,测试库在格式文件中添加库:''编译项目(':lib')''。现在,我不能在我的库gradle文件''androidTestCompile project(':testLib')''中写入,因为这会导致循环依赖。我也在主项目的测试中使用了''MyViewIdlingResource'',所以我不能将它移动到lib的测试文件中。 – PhilLab