How do I structure project test directory in Android Studio?

前端 未结 4 2035
迷失自我
迷失自我 2021-01-30 22:15

There is no convention for this yet, but how do I structure the test directory for Android Studio, now that what\'s stated on the Android testing fundamentals page differs?

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 22:45

    UPDATE

    Starting from Build Tools 19.1.0 and build plugin 0.11.0 build.gradle files needs to have testPackageName renamed to testApplicationId ( also packageName should be renamed to androidId)

    As of build plugin 0.9.0 instrumentTest folder is renamed to androidTest. That's all we need for testing.

    Here is example of 0.11.+ DSL

    android {
        compileSdkVersion 19
        buildToolsVersion "19.1.0"
    
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
            androidId "org.homelab.lab"
            testApplicationId "org.homelab.lab.test"
            testInstrumentationRunner "org.homelab.lab.test.Runner"
        }
    
        ...
    }
    

    GOTCHAS : if your build file consists definitions of testPackageName and testInstrumentationRunner, remove them

    For version 0.5.0 - 0.8.+

    Android Studio uses Gradle plugin version 0.5.+ which follows Gradle SourceDir principles.

    Android Studio project structure

    How to make it work:
    1.update SDK
    2.install or update Gradle to 1.6 (reported problems with 1.7) or stick with gradle wrapper
    3.don't use Android Studio for running instrumentation task, use gradle command

    gradle connectedCheck
    

    4.don't use same package for test and main apk
    5.check results using browser

    /build/reports/instrumentTests/index.html
    

    Gotchas:
    If test package and main package are the same it may create empty TestSuite. Result is misleading as Gradle reports no problems but reports show that no Class has been tested.

    EDIT:

    Below is the part of build.gradle which configures instrument tests required before 0.9.0:

    android {
        compileSdkVersion 14
        buildToolsVersion "17.0.0"
    
        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 17
            testPackageName "org.homelab.lab.test"
            testInstrumentationRunner "org.homelab.lab.test.Runner"
        }
    
        ...
    }
    

    example project https://github.com/swavkulinski/android-studio-instrumentation-test

提交回复
热议问题