简体   繁体   中英

Questions about Android Unit Test with gradle or android studio

I am writing some test cases in my project and I have bunch questions about android unit test with android studio & gradle I met in recent days and cannot get the good answers after searching.

Here are situations I have met and they are really bothering me.

Situation 1:

When I put the test cases in androidTest folder and run graldew cAT and also use the annotation @AndroidJUnit4 for the test class. I can run the unit test and get the right results. But after I just changed the @AndroidJUnit4 to @MockitoJUnitRunner (I need mock the context). The android studio or gradle cannot find any test, only tell "empty suit".

Situation 2:

When I put the test cases in test folder and run graldew test and also use the annotation @MockitoJUnitRunner . It can find the tests successfully. But I guess it only run locally. If the test cases dependent some native lib, it will give the failure message like: java.lang.UnsatisfiedLinkError: no libxx in java.library.path

So here are questions:

Question 1:

It seems that we can put the test code in src/test or src/androidTest. So what are differences between these two folders?

Question 2:

What are differences between gradlew cAT and gradle test ? Are these two commands related to folder ( test/androidTest folder I mentioned) in projects?

Question 3:

In my situation, I need write some test cases, which dependent the Context and native so. What should I do for that?

As this is actually 3 questions, it might have been best to create 3 separate SO questions. In any event, here are the answers:

Answer 1:

The src/test folder is meant for "regular" JUnit unit tests. These are tests that run in a regular JVM.

The src/androidTest folder is meant for any tests that require an Android device or emulator to be running. This is where your Espresso tests would live.

Answer 2:

The command gradlew cAT or gradlew connectedAndroidTest runs any tests which require a connected device ( cAT standing for Connected Android Test) that are in the src/androidTest directory, while the command gradle test runs just unit tests, in src/test directory.

Answer 3:

If your unit tests depend on Context, consider using Robolectric for your unit tests. This will give you access to Context. Good examples for how to use Robolectric can be found in Corey Latislaw's "Android Katas" repo .

EDIT

Situation 1: I'm not sure if this is what you are experiencing, but when I have run into this "Empty test suite" error (when I clearly have tests in the directory), it was because I forgot to include the Android JUnit Test Instrumentation Runner in my module's build.gradle file. Include it in the defaultConfig section of your android section, like this:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.kiodev.example"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM