5

I want to use large files (> 2 GB zip archives as well as video files) in my instrumentation tests to test file loading from SD card / internal storage.

How can I write these instrumentation tests and equip them with the files they need? For other tests, I only needed very small files so I put them in the app's raw resources

InputStream rStream = context.getResources().openRawResource(R.raw.smalltestvideo);

But now I need to specifically test large files for which this is not an option anymore. I am running with

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
PhilLab
  • 4,777
  • 1
  • 25
  • 77
  • Do you want to read every file? For test is there any requirement that this archived file needs to store on the sd card. – Jitesh Mohite Mar 28 '18 at 07:20
  • Just to understand the situation better, you want to have large sized files on the internal storage of the device, your app has the READ_STORAGE permission and you want to test the app ability to load these large file. Is that accurately correct? – ahasbini Mar 28 '18 at 08:19
  • My test devices are blank - consider them wiped before every test run to have a defined state. As I have several of them, I don't wan't to prepare them by hand, it has to be automated. But yes, the actual usage of the app implies reading large files from the internal or external storage and thus, the app has the ``READ_STORAGE`` permission – PhilLab Mar 28 '18 at 13:35
  • Beside of opening a big raw resources would throw some crazy exception, It's really inconvenience to put such a big file in a git repo. Do you want this for running on a ci server? – M. Reza Nasirloo Mar 28 '18 at 21:22

2 Answers2

9

As a start here's a script that could be added to the build.gradle file and do the job of copying files from a certain folder and placing them within the device.

I do want to point out that @m-reza-nasirloo has a point about storing these files within the repo and so it has to be taken into consideration for designing a proper test while having a maintainable and small sized repo that the CI server or teammate could easily download clone. With the script below, the test files can be placed outside the project root directory and achieve these points.

Also another point is this script assumes that the directory of the test files (in here it is testFiles) contains only files and not sub-directories. To handle sub-directories would need some more tweaks.

Furthermore, the script will push to all of the connected devices. So in case your running the tests from within Android Studio, devices which you didn't select to run will get the files as well.

Lastly, this script will be executed when building for android tests, specifically when gradle is executing assembleDebugAndroidTest or similar tasks. To change that just modify the if condition as you deem necessary.

import com.android.ddmlib.AndroidDebugBridge

task pushFilesToDevices {
    def location = "${project.rootDir}/../testFiles/"
    def files = new File(location).listFiles()
    AndroidDebugBridge.initIfNeeded(false)
    def bridge = AndroidDebugBridge.createBridge(android.adbExecutable.path, false)
    doLast {
        bridge.devices.each { device ->
            println "pushing files to ${device.name}"
            files.each { file ->
                device.pushFile(file.absolutePath, "/sdcard/${file.name}")
            }
            println "finished pushing"
        }
    }
}

tasks.whenTaskAdded { taskItem ->
    if (taskItem.name.contains("assemble") && taskItem.name.endsWith("AndroidTest")) {
        taskItem.dependsOn pushFilesToDevices
    }
}
ahasbini
  • 6,761
  • 2
  • 29
  • 45
1

You can include these large files as Test Assets - just create an Assets directory in your instrumentation test structure. When your app is compiled, these will be excluded (except for tests).

Just create the correct structure: src/androidTest/assets

You may also need to register it in your module gradle:

androidTest {
    assets.srcDirs = ['src/main/assets', 'src/androidTest/assets/']
    java.srcDirs = ['src/main/java', 'src/androidTest/java'] //you probably already have this
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124