在Android上进行单元测试时的语言环境 [英] Locale during unit test on Android

查看:91
本文介绍了在Android上进行单元测试时的语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要测试的代码.我想检查一个字符串是否由我在资源中拥有的​​各种字符串正确组成.这里的挑战是在我的资源中处理多种翻译.我知道在测试桌面应用程序时语言环境可能是一个问题,建议您创建独立于语言环境的测试.

I have some code I want to test. I want to check if a String is properly composed out of various strings that I have in resources. The challenge here is to deal with multiple translations in my resources. I know that locale can be an issue when testing a desktop application and that it is recommended that you create locale-independent tests.

我发现您可以通过编程方式设置语言环境,但不建议这样做(请参见在Android中以编程方式更改语言).尽管此问题旨在在正常运行应用程序时在运行时更改语言环境,但我想知道是否有更好的解决方案来解决我的问题.

I've found that you can set the locale programatically, but it was not recommended (see Change language programmatically in Android). While this question is aimed at changing locale at runtime when running an app normally, I was wondering if there was a better solution to my problem.

推荐答案

如果仅用于测试,则可以以编程方式更改区域设置而不会出现任何问题.它将更改您的应用程序的配置,并且您将能够使用新的语言环境测试您的代码.它具有与用户更改它相同的效果.如果您想自动化测试,则可以编写一个脚本,使用adb shell更改语言环境,如

If it's just for testing, then you can change the locale programmatically without any issues. It will change the configuration of your app and you will be able to test your code with the new locale. It has the same effect as if a user has changed it. If you want to automate your tests, you can write a script that changes locale using adb shell as described here, and launch your tests afterwards.

这里是测试英语,德语和西班牙语语言环境中取消"一词翻译的示例:

Here is an example of testing translations of word "Cancel" for English, German and Spanish locales:

public class ResourcesTestCase extends AndroidTestCase {

    private void setLocale(String language, String country) {
        Locale locale = new Locale(language, country);
        // here we update locale for date formatters
        Locale.setDefault(locale);
        // here we update locale for app resources
        Resources res = getContext().getResources();
        Configuration config = res.getConfiguration();
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }

    public void testEnglishLocale() {
        setLocale("en", "EN");
        String cancelString = getContext().getString(R.string.cancel);
        assertEquals("Cancel", cancelString);
    }

    public void testGermanLocale() {
        setLocale("de", "DE");
        String cancelString = getContext().getString(R.string.cancel);
        assertEquals("Abbrechen", cancelString);
    }

    public void testSpanishLocale() {
        setLocale("es", "ES");
        String cancelString = getContext().getString(R.string.cancel);
        assertEquals("Cancelar", cancelString);
    }

}

这是Eclipse中的执行结果:

Here are the execution results in Eclipse:

Android O更新.

在Android O中运行时,应使用方法Locale.setDefault(Category.DISPLAY, locale)(请参阅

When running in Android O method Locale.setDefault(Category.DISPLAY, locale) shall be used (see behaviour changes for more detail).

这篇关于在Android上进行单元测试时的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆