Android Studio: include library test classes in app test

The (relevant) parts of my project folder structure are as follows

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

Therefore, I have a module "lib", which provides reusable functions and a module "mobile", which contains actual applications. Both modules have their own Android test (instrumentation test), in which test activities. The Lib test code also contains utility classes, such as lib / SRC directory / Android test / 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);
            }
        };
    }
    ...

Using the testutils class in the Lib test module works, but when I call it from the mobile test module, the compiler complains:

For example, in the file mobile / SRC / Android test / 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 {
    ...

So my question is: how do I use the classes in my library's test suite in my main application's test suite?

I have added an explicit Android testcompile library for my mobile / build.gradle, but this has no results:

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'
}   

resolvent:

That's because the Android test part of your library is not compiled into a mobile target. There are two ways to solve this problem

You can move the test util class to the library source (main), or you can move the test util to an external library and add it through the library and testandroidcompile in the move

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>