Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to pass Culture when comparing strings using Assert.AreEqual() in C#

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?

like image 452
davy Avatar asked Nov 25 '14 09:11

davy


People also ask

How does assert Areequal work?

Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.

How do you compare two objects using assert?

In order to change the way two objects are compared in an assert we only need change the behavior of one of them – the expect value (might change depending on the unit testing framework).

How do you compare assert?

To compare two lists specifically, TestNG's Assert class has a method known as assertEquals(Object actual, Object expected) and there is an extended version of this method with customized message as assertEquals(Object actual, Object expected, String message). if the elements of the lists are in the same order.

Is equal NUnit?

NUnit is able to compare single-dimensioned arrays, multi-dimensioned arrays, nested arrays (arrays of arrays) and collections. Two arrays or collections are considered equal if they have the same dimensions and if each pair of corresponding elements is equal.


2 Answers

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.

like image 113
Matthew Watson Avatar answered Oct 25 '22 01:10

Matthew Watson


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.

like image 43
Jono Job Avatar answered Oct 25 '22 01:10

Jono Job