Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle test command not running any tests

I have a simple test implementation that I can run on Android Studio. This class is inside /src/androidTest directory of my project. ApplicationTest.java:

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }

    @Override
    protected void setUp() throws Exception {
        createApplication();
    }

    @SmallTest
    public void testSample() {
        assertEquals("Pass: ", 50, 50);
        assertEquals("Fail: ", 50, 49);
    }
}

But when I try to run via gradle with the following command, nothing runs. Gradle command that I'm trying:

./gradlew test

Output:

c-08e-abaek-0:HelloTest abaek$ ./gradlew test
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:preCompileDebugUnitTestJava
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:processDebugUnitTestJavaRes UP-TO-DATE
:app:compileDebugUnitTestJava UP-TO-DATE
:app:compileDebugUnitTestSources UP-TO-DATE
:app:mockableAndroidJar UP-TO-DATE
:app:assembleDebugUnitTest UP-TO-DATE
:app:testDebug UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:app:checkReleaseManifest
:app:prepareReleaseDependencies
:app:compileReleaseAidl UP-TO-DATE
:app:compileReleaseRenderscript UP-TO-DATE
:app:generateReleaseBuildConfig UP-TO-DATE
:app:generateReleaseAssets UP-TO-DATE
:app:mergeReleaseAssets UP-TO-DATE
:app:generateReleaseResValues UP-TO-DATE
:app:generateReleaseResources UP-TO-DATE
:app:mergeReleaseResources UP-TO-DATE
:app:processReleaseManifest UP-TO-DATE
:app:processReleaseResources UP-TO-DATE
:app:generateReleaseSources UP-TO-DATE
:app:processReleaseJavaRes UP-TO-DATE
:app:compileReleaseJava UP-TO-DATE
:app:preCompileReleaseUnitTestJava
:app:preReleaseUnitTestBuild UP-TO-DATE
:app:prepareReleaseUnitTestDependencies
:app:processReleaseUnitTestJavaRes UP-TO-DATE
:app:compileReleaseUnitTestJava UP-TO-DATE
:app:compileReleaseUnitTestSources UP-TO-DATE
:app:assembleReleaseUnitTest UP-TO-DATE
:app:testRelease UP-TO-DATE
:app:test UP-TO-DATE

BUILD SUCCESSFUL

Total time: 3.303 secs

This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.4/userguide/gradle_daemon.html

What am I doing wrong?

like image 919
baekacaek Avatar asked Dec 06 '22 20:12

baekacaek


1 Answers

I'm not familiar with every kind of Android tests, but I see you are trying to run basic unit tests.

Command:

./gradlew test

will execute pure java unit tests located in: src/test/java/ directory.

Pure Java tests unit tests on Android

If you want to execute pure java unit tests without android dependencies, you can locate them in src/test/java directory and define them as follows:

@RunWith(JUnit4.class)
public class SampleTest {

  @Test
  public void testKiraiShouldSetAnInputAndNotBeNull() {
    // given
    int givenValue = 50;

    // when
    int expectedValue = 50; // you can generate this value by your app

    // then
    assertThat(givenValue).isEqualTo(expectedValue);
  }
}

Moreover, you should have the following dependencies in your build.gradle file:

dependencies {
  testCompile 'junit:junit:4.12'
  testCompile('com.google.truth:truth:0.25') {
    exclude group: 'junit' // Android has JUnit built in
  }
}

I'm using Google Truth for assertions. It's very convenient library. After that, you can execute command:

./gradlew test

and your tests should be executed. Moreover, you can execute them through Android Studio as well.

If you want to see complete working example with pure java tests, you can check out one of my projects here: https://github.com/pwittchen/kirai.

Unit tests with Android dependencies

If you want to create unit tests with Android dependencies (e.g. using a few classes from Android SDK), you should locate them in the following directory:

src/androidTest/

Exemplary test can look as follows:

@RunWith(AndroidJUnit4.class)
public final class SampleTest {
  @Test
  public void testKiraiShouldSetAnInputAndNotBeNull() {
  // given
  int givenValue = 50;

  // when
  int expectedValue = 50; // you can generate this value by your app

  // then
  assertThat(givenValue).isEqualTo(expectedValue);
  }
}

Basically, the only difference from the previous sample is value of @RunWith annotation defining test runner and the fact that you can use classes from Android SDK here.

You should define the following dependencies in your build.gradle file:

dependencies {
  androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
  androidTestCompile('com.google.truth:truth:0.26') {
    exclude group: 'junit' // Android has JUnit built in.
  }  
}

Moreover, you should define testInstrumentationRunner in build.gradle file as well like that:

android {
  defaultConfig {
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
}

After that, you need to run Android device emulator or connect real Android device to your computer and enable debugging on it.

Then, you can execute the following command:

./gradlew connectedCheck

It will execute instrumentation unit tests on device or emulator. You can execute these tests from Android Studio as well.

If you want to see complete working example with unit tests with Android dependencies, you can check my another project, where I'm using this approach: https://github.com/pwittchen/prefser.

Automated testing on Android may be confusing in the beginning and I know your pain. I hope my post will help. :)

like image 73
Piotr Wittchen Avatar answered Dec 11 '22 11:12

Piotr Wittchen