Reputation: 4552
I have come across some unit tests written by another developer that regularly use an overloaded version of Assert.AreEqual
like so:
Assert.AreEqual(stringparamX, stringParamY, true, CultureInfo.InvariantCulture);
stringParamX
is set within the unit test and stringparamY
will be the result from the system under test.
It is possible hat this code may be ported to other countries and these tests may be executed. However, on having a look at the MSDN docs for Culture, I can't help thinking that passing in CultureInfo.InvariantCulture
is adding some unnecessary complexity here. I'm not sure what kind of impact removing it would have on the tests if executed in other cultures.
In the context of unit testing in my situation, why should I (or not) write asserts like this?
Upvotes: 5
Views: 819
Reputation: 109567
There is no reason to use that overload if you are specifying the culture as InvariantCulture
, because that is the default.
From the documentation here:
The invariant culture is used for the comparison.
Note that there ARE some cases where it would make a difference if you weren't using the InvariantCulture
, the most notorious being "The Turkish I problem".
The following code illustrates the issue:
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
Assert.AreEqual("i", "I", true, CultureInfo.CurrentCulture); // This throws.
Assert.AreEqual("i", "I", true, CultureInfo.InvariantCulture); // This doesn't throw.
Assert.AreEqual("i", "I", true); // This doesn't throw.
However, note that you will not be affected by that because you are using the invariant culture.
Upvotes: 3
Reputation: 3028
The MSDN documentation for Assert.AreEqual that doesn't take the CultureInfo states in the "Remarks" section:
The invariant culture is used for the comparison
If true, then specifying it explicitly is technically unnecessary. It's then a moot point about whether being explicit is better.
That being said, it depends on what's being tested whether culture should be considered for a case-insensitive string comparison.
Upvotes: 2