Running all NUnit3 tests under multiple cultures

253 views
Skip to first unread message

HammerBob

unread,
Feb 29, 2016, 4:37:20 PM2/29/16
to NUnit-Discuss
I have been using NUnit 2.6.4 to run around 2000 tests. I have the nightly need to run the 2000 tests in each of these cultures (en-US, zh-CN, de-DE, pt-BR, es-MX). Somewhere along the line I read that NUnit3 would help me do that. But apparently the ability to globally specify the culture for all tests is something that is still waiting to be implemented.

I recently got the 3.0.1 source and added the ability for the console runner to specify the default culture for all the tests it runs. I added these two new command line switches:

--culture=de-DE        Default thread culture for all tests. Examples: en-US, es-MX, de-DE
--ui-culture=de-DE     Default UI culture for all tests. Examples: en-US, es-MX, de-DE

One change that might be good is to let --culture set both the UI and the thread cultures, and use --thread-culture to set the thread culture alone - but I didn't implement that.

These two new switches simply specify the default culture for all tests. You can still override the default culture using "SetCulture("pt-BR"), SetUICulture("pt-BR")" in a test's attribute.

With these new switches I should be able in my nightly TeamCity build to run all of my 2000 tests under each of the 5 of the cultures I'm interested in.

It would be convenient if the NUnit3 GUI runner allowed you to specify a list of cultures for all the tests to run under, like this:

Run tests under these cultures: [en-US, zh-CN, de-DE, pt-BR, es-MX]

I have not looked into making the GUI runner do that.

It only took 18 lines of code (not counting the comments) to add this to the NUnit3 console runner.

Does this sound like a worthwhile change? It would be a big help for me to have this built into NUnit3.

Here is the code.

File: NUnitTestAssemblyRunner.cs
Method: CreateTestExecutionContext
Added the following code after "Context = new TestExecutionContext();"
         if ( Settings.Contains( PackageSettings.DefaultCulture ) )
            Context.CurrentCulture = new CultureInfo( (string) Settings[ PackageSettings.DefaultCulture ] );
         if ( Settings.Contains( PackageSettings.DefaultUiCulture ) )
            Context.CurrentUICulture = new CultureInfo( ( string ) Settings[ PackageSettings.DefaultUiCulture ] );

File: PackageSettings.cs
Class: PackageSettings
Added the following code in the "Engine Settings - Used by the Engine itself" region:
      /// <summary>
      /// The name of the default culture. If not specified, the current culture is used.
      /// </summary>
      public const string DefaultCulture = "DefaultCulture";

      /// <summary>
      /// The name of the default UI culture. If not specified, the current UI culture is used.
      /// </summary>
      public const string DefaultUiCulture = "DefaultUiCulture";

File: ConsoleOptions.cs
Class: ConsoleOptions
Added the following code in the "Properties" region under the "How to Run Tests" comment:
      public string DefaultCulture { get; private set; }
      public bool DefaultCultureSpecified { get { return DefaultCulture != null; } }

      public string DefaultUiCulture { get; private set; }
      public bool DefaultUiCultureSpecified { get { return DefaultUiCulture != null; } }
Method: ConfigureOptions
Added the following code below the "How to Run Tests" comment:
         this.Add( "culture=", "Default thread culture for all tests.\nExamples: en-US, es-MX, de-DE",
             v => DefaultCulture = RequiredValue( v, "--culture" ) );
         this.Add( "ui-culture=", "Default UI culture for all tests.\nExamples: en-US, es-MX, de-DE",
             v => DefaultUiCulture = RequiredValue( v, "--ui-culture" ) );

File: ConsoleRunner.cs
Class: ConsoleRunner
Method: MakeTestPackage
Added the following code below line "TestPackage package = new TestPackage( options.InputFiles );":
         if ( options.DefaultCultureSpecified )
            package.AddSetting( PackageSettings.DefaultCulture, options.DefaultCulture );
         if ( options.DefaultUiCultureSpecified )
            package.AddSetting( PackageSettings.DefaultUiCulture, options.DefaultUiCulture );


Charlie Poole

unread,
Feb 29, 2016, 4:43:43 PM2/29/16
to NUnit-Discuss
I think what you may have read about regarding NUnit 3, is the plan to
allow specifying multiple cultures via an attribute, rather than only
a single culture as is now done with SetCulture. However, this is not
implemented anyway.

I don't think the request to implement it as a command-line switch has
come up before. What do other people think about that? Should you be
able to specify culture (and uiculture) at the command-line? If yes,
should there be two separate settings or a single one?

Regarding the GUI, I would think that such an option would be easier
to use through a menu selection rather than by the command-line.

Charlie
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nunit-discus...@googlegroups.com.
> To post to this group, send email to nunit-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/nunit-discuss.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages