Run Instrumentation Tests for Your App With Different Locales

Surprise! We need to do a lot of tests with different locales here at Localazy :slightly_smiling_face:.

Introduction

This is a bit of a problem with Android’s instrumentation tests. It seems that there is no easy way how to change locale prior to running the test.

Also, we need to run our tests in isolated environment and not in a single process as it wouldn’t allow us to test data updates, uninitialized states, etc. correctly.

Solution

So, we run some of our tests using a simple bash script like this:

Code

#!/bin/bash

# Param 1 - Locale to switch emulator before the test
# Param 2 - Name of the module from which the test is
# Param 3 - Class with tests
# Param 4 - Test method
runTest() {
    adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE "$1" com.android.customlocale2
    sleep 1
    if ./gradlew -Pandroid.testInstrumentationRunnerArguments.class="$3"#"$4" "$2"; then
        echo -e "\e[1m\e[32mTEST "$3"#"$4" SUCCESSFULL!\e[0m\n"
    else
        echo -e "\n\n\e[1m\e[31mTEST "$3"#"$4" FAILED!\e[0m\n\n"
        exit 1
    fi
}

runTest "de_DE" "testApp01::connectedAndroidTest" "com.localazy.test.BasicTestSet" "testUncontainedLanguage"

runTest "cs_CZ" "testApp01::connectedAndroidTest" "com.localazy.test.BasicTestSet" "testBasicUpdatingData1"

runTest "de_DE" "testApp01::connectedFlavor1DebugAndroidTest" "com.localazy.test.BasicTestSet" "testBasicUpdatingData2"

# … more tests ...

echo -e "\n\n\e[1m\e[32mALL TEST FINISHED SUCCESSFULLY!\e[0m\n\n"
exit 0

Puzzled with weird echos? Those crazy strings like \e[1m\e[32m are just for coloring the output and adding some empty lines, so it’s better readable for us. In CD/CI you can just check for return value.

Notice

Beware: This solution works only inside Android emulator since it relies on preinstalled ‘Custom Locale’ app which is only available in emulators.

translate app

Join Localazy today

Translating apps has never been easier. Try Localazy for free.

Sign up