December 2004 - Posts

Happy Holidays everyone

Christmas time again, and looking forward to a week of vacation... And so I have a couple of Christmas gifts for you :-)  Our friends who are now celebrating Hannukah or another cultural holiday, please consider these gifts Hannukah/... gifts...

I got 5 more Google Mail accounts to give away to those who can come up with the most original reason why they need one :-) Just send me a request, my e-mail address is roy.dictus@gmail.com.

Happy Holidays everyone!

with 0 Comments

Unit-testing with multiple cultures in MbUnit and NUnit

It's a good idea, when unit-testing your classes, to test under multiple circumstances -- such as multiple cultures. Sure, a routine may work great when run under a given culture, say US English, but what if it is run on a Spanish or Japanese machine?

MbUnit makes it easy to run tests under different cultures. If NUnit is your tool of choice, you need to do a little bit more work.

First, let's start with MbUnit. This great unit-testing framework has a special attribute called MultipleCulture that automagically runs your tests under all the cultures you specify as a comma-separated list. Here's an example in C#:

[Test][MultipleCulture("en-US,es-ES,jp-JP")]
public void ConversionTest()
{
    single price = MyConverter.FromCurrencyText("$ 200.00")
    Assert.AreEqual(200, price)
}

NUnit, unfortunately, doesn't feature this attribute. But you can do almost the same if you create multiple unit-testing classes that inherit from a base class that contains all the tests. Every of these derived classes then sets a given culture on the thread, as in this example in Visual Basic.Net:


<TestFixture()> _
Public Class MyConverterJapaneseUnitTests
    Inherits MyConverterUnitTests

    <TestFixtureSetUp()> _
    Public Sub Setup()
        System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("jp-JP")
        System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("jp-JP")
    End Sub
End Class

Open your assembly with NUnit and voilà -- instant multicultural tests!

with 0 Comments