Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create test classes that can be used in both Android tests and unit tests?

I often find myself duplicating the exact same set of test classes such as mocks or helpers for both Android tests /androidTest and unit tests /test when writing tests for an application module.

For example, I have some static functions that help me set up mocks quickly in /test/MockUtils.java However, I cannot reuse this helper class in any of my Android tests because they do not share the same class path - /androidTest vs /test.

I've thought of creating a new module that only contains test resources. However, this idea won't fly because the Android Gradle plugin refuses to depend on an app module.

project testCommon resolves to an APK archive which is not supported as a compilation dependency.

Is there any other way to create test classes that could be reused in both Android tests and unit tests?

like image 674
Some Noob Student Avatar asked Jul 26 '15 09:07

Some Noob Student


People also ask

Which tool is used for unit testing in Android?

First, let's start with the basics: JUnit is the default test framework for Android. Mockito, Robolectric, and Espresso all use it as the backbone and build on top of it. This is one decision you don't have to make. More specifically, AndroidJUnit4 is the current runner that provides utilities to run Android tests.

How do I create a test in Android?

Create new tests You can add a new test for a specific class or method directly from its source code by following these steps: Open the source file that contains the code you want to test. Put your cursor in the name of the class or method you want to test, and press Control+Shift+T ( Command+Shift+T on macOS).

How unit testing is implemented in Android?

Create a local unit test class To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.


3 Answers

NOTE: This is a theorized solution, one that I have not tried.

Step #1: Create a testSrc/ directory in your module for which you are trying to set up the shared testing code.

Step #2: Put the shared code in that directory (with appropriate subdirectories based on Java package).

Step #3: Add the following closure inside your android closure in your module's build.gradle file:

sourceSets {
    androidTest {
        java.srcDirs <<= 'testSrc'
    }

    test {
        java.srcDirs <<= 'testSrc'
    }
}

What this should do is tell Gradle for Android that testSrc is another source directory for Java code in the androidTest and test sourcesets.

like image 150
CommonsWare Avatar answered Oct 12 '22 14:10

CommonsWare


Based on CommonsWare's solution and https://code.google.com/p/android/issues/detail?id=181391, the correct way to add an additional class path is to use the += or <<= operators.

sourceSets {
    androidTest {
        java.srcDirs <<= 'testSrc'
    }

    test {
        java.srcDirs += 'testSrc'
    }
}
like image 44
Some Noob Student Avatar answered Oct 12 '22 13:10

Some Noob Student


The solution described in the blog below helped me.

http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/

I put my shared test code into src/sharedTest/java . Then I added the following code to build.gradle:

<pre>
android {  
  sourceSets {
    String sharedTestDir = 'src/sharedTest/java'
    test {
      java.srcDir sharedTestDir
    }
    androidTest {
      java.srcDir sharedTestDir
    }
  }
}
</pre>
like image 23
Jafar Karuthedath Avatar answered Oct 12 '22 12:10

Jafar Karuthedath