[TIP] Testing locale-dependent behaviour

Olemis Lang olemis at gmail.com
Wed Nov 26 06:47:09 PST 2008


2008/11/25 Kathryn Van Stone <kvs at acm.org>:
>
> On Nov 25, 2008, at 2:12 PM, Grig Gheorghiu wrote:
>
>>
>> It seems to me that your scenario is a good candidate for mock
>> testing. You could try mocking the functions that interface with
>> the locale, both by having them return canned values corresponding
>> to a given locale (and seeing how your code reacts to those
>> values), and by having them raise exceptions or other errors, to
>> simulate environments where a given locale is not present.
>>
>> Grig
>>
>
> I agree. In particular you don't  need to test that the locale code
> works, just that you are calling it correctly.  Mocks are good for
> that, and Python is an easy language to do mocks in.
>

Once again (just to complement my previous comment ;) ...

Perhaps this is not your case but, if you were using Babel the class
babel.core.Locale [1]_ supports some features you may want to try (of
course, if you can ... I don't really know if you can rely on Babel's
classes in your specific case, but well, this is just a suggestion ;)
...

- You can negotiate [2]_ [3]_ the locale to be used ... perhaps the
  specific locale you need is not available, but you find another
  equivalent (or similar ...) locale ;)

- You could try mocking Locale objects ... however I am not really
  sure about whether you will need to mock locale's functions or not
  :-§ ... but anyway ... just another idea ;)

Finally, yesterday I tried loading 'en_US' in Windows after installing
Babel, and it worked just fine. However, right now I was trying to
load the same locale object in GNU/Linux (Ubuntu Hardy ;) and it is
not available ... so :(

Besides (GNU/Linux ;) ...

{{{
#!python

>>> Locale.default('LC_MESSAGES')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/Babel-0.9.4-py2.5.egg/babel/core.py",
line 155, in default
  File "/usr/lib/python2.5/site-packages/Babel-0.9.4-py2.5.egg/babel/core.py",
line 137, in __init__
babel.core.UnknownLocaleError: unknown locale 'en_US'

>>> Locale.default('LC_ALL')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/Babel-0.9.4-py2.5.egg/babel/core.py",
line 155, in default
  File "/usr/lib/python2.5/site-packages/Babel-0.9.4-py2.5.egg/babel/core.py",
line 137, in __init__
babel.core.UnknownLocaleError: unknown locale 'en_US'

}}}

... so it seems GNU/Linux is a little bit more problematic ... :(. But
you could definitely do something like :

{{{
#!python

>>> from babel.core import Locale, UnknownLocaleError

>>> class TestLocale(Locale):
...     def __new__(cls, *args):
...             try:
...                     return Locale(*args)
...             except UnknownLocaleError:
...                     return MyDummyLocale(*args)
...
>>> l = TestLocale('en', 'US')

}}}

... perhaps a more elaborate (still quick :) solution might be
implemented to do so ;)

.. [1] Core locale representation and locale data access.
        (http://babel.edgewall.org/wiki/ApiDocs/babel.core#babel.core:Locale)

.. [2] negotiate(cls, preferred, available, sep='_', aliases=LOCALE_ALIASES)
         (http://babel.edgewall.org/wiki/ApiDocs/babel.core#babel.core:Locale:negotiate)

.. [3] negotiate_locale(preferred, available, sep='_', aliases=LOCALE_ALIASES)
         (http://babel.edgewall.org/wiki/ApiDocs/babel.core#babel.core:negotiate_locale)

-- 

Regards,

Olemis.



More information about the testing-in-python mailing list