Setting a custom assets directory for unit testing in Android Studio

后端 未结 4 1450
野的像风
野的像风 2021-02-02 08:24

We have the not uncommon requirement of executing unit tests of our Android application which make use of assets which are only required for unit testing.

With the Eclip

相关标签:
4条回答
  • 2021-02-02 08:32

    I am using Android Studio 1.3 + Gradle 1.2.3 and src/androidTest/assets didn't work for me. I placed everything in src/debug without any changes to build.gradle and it worked. Android seemingly picks up up items from the debug folder automatically while building the apk.

    0 讨论(0)
  • 2021-02-02 08:34

    I've stumbled upon an answer for my own question, for which I scored the coveted "Tumbleweed" award reserved for questions that inspire a particularly notable lack of interest. Never-the-less it may prove useful to someone.

    In the end I'm going to describe this as a bug in Android's gradle build but in the absence of any conclusive documentation that's going to have to remain an opinion.

    It would appear that "androidTest/assets" just doesn't work. However "debug/assets" does. So does "debug/java" incidentally so the solution is to put any and all testing resources and code into the debug root, with the exception of the unit tests themselves.

    I also added the following modifications to my build.gradle:

        androidTest {
            assets.srcDirs = ['src/main/assets', 'src/androidTest/assets/', 'src/debug/assets/']
            java.srcDirs = ['src/main/java', 'src/androidTest/java', 'src/debug/java']
        }
    

    And now I have functioning unit tests employing assets and mocks that are unknown to the release build.

    0 讨论(0)
  • 2021-02-02 08:55

    I found this right for me

    android{
        sourceSets {
            androidTest {
                assets.srcDirs = ['src/main/assets', 'src/androidTest/assets/', 'src/debug/assets/']
                java.srcDirs = ['src/main/java', 'src/androidTest/java', 'src/debug/java']
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-02 08:57

    I don't know how it was before, but I was able to create Android instrumented unit tests where I can read test assets or production assets. See this answer for more info.

    0 讨论(0)
提交回复
热议问题