Gradle test team does not run any tests

I have a simple test implementation that I can run on Android Studio. This class is inside the / 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 start through gradle with the following command, nothing starts. gradle I'm trying to execute:

 ./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?

+2
source share
1 answer

I am not familiar with any of the Android tests, but I see that you are trying to run basic unit tests.

Team:

./gradlew test

will run pure Java unit tests located in the src/test/java/ directory.

Pure Java test tests on Android

If you want to run unit tests of pure java without android dependencies, you can find them in the 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); } } 

In addition, your build.gradle file should have the following dependencies:

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

I use Google Truth for claims. This is a very convenient library. After that you can execute the command:

./gradlew test

and your tests should be performed. Alternatively, you can execute them through Android Studio.

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

Units Android dependency tests

If you want to create unit tests with Android dependencies (for example, using several classes from the Android SDK), you should find them in the following directory:

src/androidTest/

A sample test might look like this:

 @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 the @RunWith annotation value that defines the test runner, and the fact that you can use classes from the Android SDK here.

In the build.gradle file build.gradle you must define the following dependencies:

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

In addition, you must define testInstrumentationRunner in the build.gradle file just like this:

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

After that, you need to run the emulator of the Android device or connect a real-time Android device to it and enable debugging on it.

Then you can run the following command:

./gradlew connectedCheck

He will perform instrument cluster tests on a device or emulator. You can also run these tests from Android Studio.

If you want to see a full working example with unit tests with Android dependencies, you can check out my other project where I use this approach: https://github.com/pwittchen/prefser .

Automatic testing on Android can be confusing at the beginning, and I know your pain. Hope my post helps. :)

+12
source

All Articles