Search Results for

    Show / Hide Table of Contents

    Culture

    CultureAttribute is used to specify cultures for which a test or fixture should be run. It does not change the culture setting, but merely uses it to determine whether to run the test. If the specified culture requirements are not met, the test is skipped.

    This attribute is useful for providing alternative tests under different cultures, or for skipping tests that are not applicable to certain cultures.

    Constructors

    CultureAttribute()
    CultureAttribute(string cultures)
    
    Parameter Type Description
    cultures string A comma-delimited list of culture names to include. You may specify either specific cultures like "en-GB" or neutral cultures like "de".

    Properties

    Property Type Description Default
    Include string Comma-delimited list of cultures on which the test should run. null
    Exclude string Comma-delimited list of cultures on which the test should not run. null
    Reason string The reason for including or excluding the test. Displayed when the test is skipped. null

    Applies To

    Test Methods Test Fixtures (Classes) Assembly
    ✅ ✅ ✅

    Examples

    Including Specific Cultures

    [TestFixture]
    public class FrenchOnlyTests
    {
        [Test]
        [Culture("fr-FR")]
        public void TestForFrenchCulture()
        {
            // This test only runs when current culture is fr-FR
            Assert.That(CultureInfo.CurrentCulture.Name, Is.EqualTo("fr-FR"));
        }
    
        [Test]
        [Culture("fr-FR,fr-CA")]
        public void TestForFrenchCultures()
        {
            // This test runs when current culture is fr-FR or fr-CA
            Assert.That(CultureInfo.CurrentCulture.TwoLetterISOLanguageName, Is.EqualTo("fr"));
        }
    
        [Test]
        [Culture("fr")]
        public void TestForAnyFrenchCulture()
        {
            // Using neutral culture matches any French variant (fr-FR, fr-CA, etc.)
            Assert.That(CultureInfo.CurrentCulture.TwoLetterISOLanguageName, Is.EqualTo("fr"));
        }
    }
    

    Excluding Cultures

    [TestFixture]
    public class ExcludeCultureTests
    {
        [Test]
        [Culture(Exclude = "en")]
        public void TestExcludingEnglish()
        {
            // This test runs on any culture EXCEPT English variants
            Assert.That(CultureInfo.CurrentCulture.TwoLetterISOLanguageName, Is.Not.EqualTo("en"));
        }
    
        [Test]
        [Culture(Exclude = "en,de")]
        public void TestExcludingEnglishAndGerman()
        {
            // This test is skipped when culture is English or German
            var lang = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
            Assert.That(lang, Is.Not.EqualTo("en").And.Not.EqualTo("de"));
        }
    }
    

    Applying to a Fixture

    [TestFixture]
    [Culture("en-US,en-GB")]
    public class EnglishCultureTests
    {
        [Test]
        public void Test1()
        {
            // All tests in this fixture only run on en-US or en-GB
            Assert.That(CultureInfo.CurrentCulture.Name,
                Is.EqualTo("en-US").Or.EqualTo("en-GB"));
        }
    
        [Test]
        public void Test2()
        {
            Assert.That(CultureInfo.CurrentCulture.TwoLetterISOLanguageName, Is.EqualTo("en"));
        }
    }
    

    Providing a Reason

    [TestFixture]
    public class CultureWithReasonTests
    {
        [Test]
        [Culture(Include = "ja-JP", Reason = "This test uses Japanese-specific date formatting")]
        public void TestJapaneseDateFormat()
        {
            // The reason is shown when the test is skipped
            Assert.That(CultureInfo.CurrentCulture.Name, Is.EqualTo("ja-JP"));
        }
    }
    

    Notes

    1. When specifying a neutral culture (e.g., "fr"), the test will run on any specific culture variant (e.g., fr-FR, fr-CA, fr-BE).
    2. If both Include and Exclude are specified, Include is evaluated first.
    3. This attribute does not change the culture. To change the culture for a test, use SetCulture or SetUICulture instead.
    4. Tests that are skipped due to culture requirements appear as "Skipped" in test results with the reason displayed.

    See Also

    • SetCulture Attribute
    • SetUICulture Attribute
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0