A test rule for setting device locale

When you run your Android tests (like espresso tests), you may want to be able to force the locale of your device to some specific value at runtime (during test execution). This could be really helpful if you want to test some features of your app against multiple locales.

You can do this by using Junit4 rules. The rule implementation looks like this:

The test locale is passed in as a parameter to the constructor. Before test execution we set the test locale. When test is executed, the previous device’s locale will be automatically restored.

And here’s an example of a test where locale il set to Locale.UK before test execution:


Related Posts