Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: Include library test classes in app test

The (relevant) part of my project's folder structure is as follows

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

So I have the module "lib", providing reusable functionalities and the module "mobile", containing the actual app. Both modules have their own androidTest(Instrumentation test) where activities are tested. The lib test code also contains utility classes, e.g. 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);
            }
        };
    }
    ...

Using this TestUtils class from the lib test module works but when I call them from the mobile test module, the compiler complains:

Error:(28, 19) error: cannot find symbol class TestUtils

e.g. in the file 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 {
    ...

So my question is: How can I use classes from my library's test suite in my main app's test suite?

I already added an explicit androidTestCompile for the library to my mobile/build.gradle , but this did not have any result:

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'
}   
like image 817
PhilLab Avatar asked Oct 16 '17 11:10

PhilLab


Video Answer


1 Answers

That's because androidTest part of your library in not compile into mobile target. There's two ways to resolve this issue.

You can move test util class to your library sources(main), or you can move test util to external lib and add it through testAndroidCompile in your library and mobile.

like image 178
Karol Kulbaka Avatar answered Oct 20 '22 01:10

Karol Kulbaka