Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put Android Instumentation test data

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"
like image 902
PhilLab Avatar asked Mar 09 '18 09:03

PhilLab


People also ask

How do I create a test folder in Android?

To add a testing source set for your build variant in Android Studio, follow these steps: In the Project window on the left, click the drop-down menu and select the Project view. Within the appropriate module folder, right-click the src folder and click New > Directory.

What is test folder in Android Studio?

By default, Unit tests are written in src/test/java/ folder and Instrumentation tests are written in src/androidTest/java/ folder. Android studio provides Run context menu for the test classes to run the test written in the selected test classes.


Video Answer


2 Answers

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
    }
}
like image 180
ahasbini Avatar answered Oct 17 '22 00:10

ahasbini


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
}
like image 1
Nick Cardoso Avatar answered Oct 17 '22 02:10

Nick Cardoso