问题
Using NUnit I wish to run all the tests in a certain project against multiple cultures.
The project deals with parsing data that should be culture neutral, to ensure this I would like to run every test against multiple cultures.
The current solution I have is
public abstract class FooTests {
/* tests go here */
}
[TestFixture, SetCulture ("en-GB")] public class FooTestsEN : FooTests {}
[TestFixture, SetCulture ("pl-PL")] public class FooTestsPL : FooTests {}
Ideally, I shouldn't have to create these classes and instead use something like:
[assembly: SetCulture ("en-GB")]
[assembly: SetCulture ("pl-PL")]
回答1:
Unfortunatelly this isn't possible now but is planned for future.
You can also do this.
public class AllCultureTests
{
private TestSomething() {...}
[Test]
[SetCulture("pl-PL")]
public void ShouldDoSomethingInPoland()
{
TestSomething();
}
}
Maybe that's something you would prefer?
回答2:
NUnit's SetCultureAttribute
applies one culture to a test, multiple cultures are not (yet) supported.
You can work around this by using the TestCaseAttribute
with language codes and setting the culture manually:
[Test]
[TestCase("de-DE")]
[TestCase("en-US")]
[TestCase("da-DK")]
public void YourTest(string cultureName)
{
var culture = CultureInfo.GetCultureInfo(cultureName);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
var date = new DateTime(2012, 10, 14);
string sut = date.ToString("dd/MM/yyyy");
Assert.That(sut, Is.EqualTo("14/10/2012"));
}
Note that this unit test will fail for de
and da
- testing for different cultures is really important :)
回答3:
If you don't mind switching, MbUnit has had this feature for nearly five years now.
You can apply the MultipleCulture
attribute at the test, fixture and assembly levels.
来源:https://stackoverflow.com/questions/5991595/nunit-testing-against-multiple-cultures