Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share test classes between test and androidTest

I have android app project, working in Android Studio. My application files are in the my.package package My unit tests are in my.package.unittest package And my espresso tests are in my.package.androidtest package

In one of my espresso tests I need to use one class that I have under the unittest package, but I am not able to.

Unittest class, that I need to use is located in app/src/test/java folder:

package my.package.unittest;
public class HelperClass {
...
}

And the file I am trying to use it in is in app/src/androidTest/java folder:

package my.package.androidtest;

import static my.package.unittest.*;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AppTest {
    HelperClass.staticMethod();
}

The error I get is: cannot resolve symbol HelperClass

Additional info:

import my.package.unittest.HelperClass;

This itself gives "cannot resolve symbol" error.

What is the correct way to use this HelperClass from my UnitTests in my Espresso tests.

like image 326
Jeni Avatar asked Sep 15 '16 19:09

Jeni


People also ask

When should you use the androidTest directory to store your test classes?

When should you use the androidTest directory to store your test classes? when the tests consist only of unit tests. when the number of tests to run is large(500+).

What are instrumented tests?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.


1 Answers

The correct answer was pointed by @Jeremy Kao.

1 -> Create a directory inside app/src. You can call it testShared.

2 -> Put your classes inside this directory.

3 -> inside app/build.gradle put:

android.sourceSets {       test {         java.srcDirs += "$projectDir/src/testShared"     }      androidTest {         java.srcDirs += "$projectDir/src/testShared"     } } 

Put this anywhere outside the android closure.

4 -> Have fun!

Resources:

http://trickyandroid.com/android-test-tricks-sharing-code-between-unit-ui-tests/

like image 141
Leandro Borges Ferreira Avatar answered Oct 02 '22 15:10

Leandro Borges Ferreira