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?
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
ParseExactand explicitly provide the format string to make it fail.