1

I have an ASP.NET Core 6 Web API application. I am writing Unit tests, using xUnit.

I need to change the culture for some tests.

According to this answer here it is recommended to make a custom attribute [UseCulture("en-US")] as shown in the xUnit samples here. Meanwhile, however, I see this discussion here which states that the xUnit team does not have the intention to incorporate this attribute in the library.

Do you have any ideas as to why?

Do you see any kind of threats in the way the attribute is implemented? Or any other issues with the implementation?

TylerH
  • 20,799
  • 66
  • 75
  • 101
MiBuena
  • 451
  • 3
  • 22
  • Why is the culture not a parameter to the test? What is the point of a separate attribute? – Neil Aug 17 '22 at 16:28

1 Answers1

1

Why not pass the required culture to your test, and then you control how that culture is configured:

[InlineData("en-GB")]
[InlineData("en-US")]
[Theory]
public void MyTest(string culture)
{
   // Arrange
   Thread.CurrentThread.CurrentCulture = new CultureInfo(culture, false));

   etc
}
Neil
  • 11,059
  • 3
  • 31
  • 56
  • This answer is correct, however keep in mind that also have to revert the CurrentCulture. To make this easier, I created a [NuGet package](https://www.nuget.org/packages/CultureAwareTesting.xUnit) which is based on [test.utility/CultureAwareTesting](https://github.com/xunit/xunit/tree/master/test/test.utility/CultureAwareTesting) and supports 2 attributes which can be used to define the Culture for a specific test: `CulturedFact` and `CulturedTheory`. – Stef Heyenrath Dec 09 '22 at 17:38