views:

290

answers:

4

Hi, I have an issue with parsing a date during a unit test run, but I cannot reproduce it. To make it more interesting it fails when the test is run by a continuous integration process but succeeds when run within Visual Studio, and they both run on the same machine, although with a different user.

Here's the test:

[Test]
public void Test()
{
    DateTime.Parse("21/12/2009", CultureInfo.CreateSpecificCulture("it-IT"));
}

(In Italian the short date format is dd/MM/yyyy)

The reason I'd expect it to fail is that I have the international settings on the machine modified so that the short date pattern for the Italian culture is dd/MM/yy, but it looks like it is either not picking it up correctly or smart enough to be able to parse it anyway, at least when I run it manually.

Any ideas how to make the test fail?

A: 

do you have a globalization setting in your config?

runrunraygun
Nope, no configuration file at all.
Simone
Give it ago, it might help. But it might not.
runrunraygun
http://msdn.microsoft.com/en-us/library/aa719558(VS.71).aspx
runrunraygun
+1  A: 

So... Correct me if I am wrong, but aren't the locale settings per user? So if you modify the settings for both users the tests should be consistent...

OK, try to print out what the framework thinks is the date pattern on both users, here is the doco:

http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo_members.aspx

Try CultureInfo.DateTimeFormatInfo.ShortDatePattern, i think that's what dd/mm/yy(yy) is.

Igor Zevaka
That's probably true, and then that's why, if this is the case, I would expect it to succeed during the CI run, since that user would have an untouched Italian culture and the parsing using an explicit culture should succeed.
Simone
That's what I did. Will report back what I find out. I still don't see why it is failing though.
Simone
+1  A: 

Even if you're using a CultureInfo object, DateTime.Parse will try your string against several patterns to do it's best to avoid throwing an exception. The devil is in the details - you should probably look at the documentation for DateTime.Parse in depth.

"Smart enough to parse it anyway" is probably what is happening. You should use ParseExact and explicitly provide the format string to make it fail.

womp
A: 

Don't let your tests depend on user-profile-based server settings. Instead, try this approach:

DateTime.ParseExact("21/12/2009", "d'/'M'/'yy", CultureInfo.InvariantCulture);
brianary