SetUICulture
SetUICultureAttribute is used to set the current CultureInfo.CurrentUICulture for the duration of a test. The UI culture remains set until the test or fixture completes and is then reset to its original value.
This attribute affects resource loading operations. When your application uses ResourceManager to load localized strings, images, or other resources, it uses CurrentUICulture to determine which localized resources to load.
Constructor
SetUICultureAttribute(string culture)
| Parameter | Type | Description |
|---|---|---|
culture |
string |
The name of the UI culture to use (e.g., "fr-FR", "de-DE", "en-US"). Must be a valid culture name recognized by CultureInfo. |
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ✅ | ✅ |
Note
If SetUICultureAttribute is applied at fixture or assembly level, the UI culture setting applies to all contained tests.
When applied at multiple levels, the most specific level takes precedence. The attribute is inherited by derived test fixtures.
Examples
Setting UI Culture on a Fixture
[TestFixture]
[SetUICulture("fr-FR")]
public class FrenchUITests
{
private CultureInfo _originalCulture = null!;
[OneTimeSetUp]
public void SaveOriginalCulture()
{
_originalCulture = CultureInfo.CurrentCulture;
}
[Test]
public void TestResourceLoading()
{
// UI Culture affects resource manager lookups
// e.g., ResourceManager will look for French resources
Assert.That(CultureInfo.CurrentUICulture.Name, Is.EqualTo("fr-FR"));
}
[Test]
public void CurrentCultureUnchanged()
{
// SetUICulture does NOT affect CurrentCulture
// Formatting operations use CurrentCulture, not CurrentUICulture
Assert.That(CultureInfo.CurrentUICulture.Name, Is.EqualTo("fr-FR"));
Assert.That(CultureInfo.CurrentCulture, Is.EqualTo(_originalCulture));
}
}
Setting UI Culture on Individual Methods
[TestFixture]
public class MixedUITests
{
[Test]
[SetUICulture("de-DE")]
public void TestWithGermanUI()
{
Assert.That(CultureInfo.CurrentUICulture.Name, Is.EqualTo("de-DE"));
}
[Test]
[SetUICulture("es-ES")]
public void TestWithSpanishUI()
{
Assert.That(CultureInfo.CurrentUICulture.Name, Is.EqualTo("es-ES"));
}
}
Using Both SetCulture and SetUICulture
Use both attributes when you need to test with different formatting and UI cultures:
[TestFixture]
public class LocalizationTests
{
[Test]
[SetCulture("en-US")]
[SetUICulture("fr-FR")]
public void TestWithDifferentCultures()
{
// CurrentCulture: affects formatting (numbers, dates, currency)
Assert.That(CultureInfo.CurrentCulture.Name, Is.EqualTo("en-US"));
// CurrentUICulture: affects resource loading (UI strings, messages)
Assert.That(CultureInfo.CurrentUICulture.Name, Is.EqualTo("fr-FR"));
// Example: Format a number using CurrentCulture (en-US)
double price = 1234.56;
string formatted = price.ToString("C"); // Uses en-US currency format
Assert.That(formatted, Does.Contain("$"));
}
}
Notes
- Only one UI culture may be specified per attribute. To test with multiple cultures, create separate test methods or use parameterized tests.
SetUICultureaffectsCultureInfo.CurrentUICulture, which controls resource loading. It does not affectCultureInfo.CurrentCulture(formatting). UseSetCulturefor formatting operations.- If you need to conditionally run tests based on the current culture (rather than setting it), use the
Cultureattribute instead.