Search code examples
c#asp.net-corexunit

xUnit custom UseCulture attribute - does the sample attribute contain any issues?


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?


Solution

  • 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
    }