【发布时间】: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