gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:46 28 4
gpt4 key购买 nike

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

├───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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com