0

One of the projects I'm working on will sometimes have deployment issues due to failing unit tests. The most common issue that causes this to happen is date formatting not matching.

Usually the problem is that some places will call the ToString method on a date, which then that string value is validated somewhere. But with different regional settings we can end up with different date strings. Some of which cause the unit tests to fail.

What I would like to try do is mark some tests as regional settings sensitive. And if a test is marked as regional settings sensitive I would like to try running all the same tests that would be run, but with different regional settings to make sure that no changes to those regional settings would cause issues.

I know I can probably add this to the tests myself manually, but this happens often enough that I want to investigate a broader solution.

Any advice on how to implement such a change would be greatly appreciated.

TheEvilMetal
  • 323
  • 2
  • 14
  • Sounds like you should fix the tests and not work around it. But that is hard to say without you first posting some code. So please do that in order to get a good answer and not just a guess! – Jocke Aug 29 '22 at 18:39
  • In the real world we're likely to encounter different system settings and different locales. I want my unit tests to be able to automatically check that different locales are handled fine. Why would I need to rewrite the tests to create what is essentially middleware – TheEvilMetal Aug 30 '22 at 06:32
  • This might help https://stackoverflow.com/questions/5001225/c-sharp-how-can-i-force-localization-culture-to-en-us-for-tests-project (Jon Senchyna's answer) – Christopher Hamkins Aug 30 '22 at 14:18

1 Answers1

0

What I ended up doing is creating a new RegionalFactAttribute and RegionalTheoryAttribute. These inherit from the corresponding FactAttribute and TheoryAttribute in Xunit. These attributes have params string[] parameter to accept culture strings.

These attributes need to be marked with the XunitTestCaseDiscoverer attribute. This takes 2 parameters: The namespace and class name of a discoverer class, and the assembly name that Xunit will need to assembly scan to use the discoverer classes.

Next I made the 2 discoverer classes: RegionalFactAttributeDiscoverer implements IXunitTestCaseDiscoverer and it's Discover method. This discover method creates new test cases. This detects which culture strings were used in the constructor for the RegionalFact and selects a new test case for every culture used in the constructor for the RegionalFact.

The discoverer for the RegionalTheory needed to inherit from TheoryDiscoverer. This ten overrides the CreateTestCasesForDataRow and CreateTestCasesForTheory methods to create test cases. Similar to how the RegionalFact works this will create a new theory test case for each set of inline data provided.

To change the culture I just set CultureInfo.CurrentCulture and CurrentUICulture.

For unit testing that this actually works I used a fixture to get a view of the culture before the test runs and after.

TheEvilMetal
  • 323
  • 2
  • 14