[TIP] Testing locale-dependent behaviour

Grig Gheorghiu gheorghe_gheorghiu at yahoo.com
Tue Nov 25 11:12:52 PST 2008

--- On Mon, 11/24/08, Thomas Lotze <thomas at thomas-lotze.de> wrote:

> From: Thomas Lotze <thomas at thomas-lotze.de>
> Subject: [TIP] Testing locale-dependent behaviour
> To: testing-in-python at lists.idyll.org
> Date: Monday, November 24, 2008, 10:42 PM
> Is there a best practice how to test locale-dependent code,
> given that one
> cannot be sure when writing the tests which locales are
> available on the
> machines that may execute them later? Is there any way to
> define a locale
> from inside a test environment in order to achieve some
> specific behavior
> to test for?
> As an example, I want to be able to check that a function
> that calls
> str.upper at some point behaves differently under a locale
> that knows
> about umlauts than under the C locale, but the tests may
> get executed on a
> machine that doesn't have any umlaut-aware locales.

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.


More information about the testing-in-python mailing list