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")]
Make more copies of the attribute if you want multiple cases. The data type of the values provided to the TestCase attribute should match with that of the arguments used in the actual test case.
NUnit is an open-source unit testing framework for the . NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.
We will install NUnit and Moq using the Nuget package manager. Make sure that in your references, NUnit and Moq are present after installation: For running NUnit tests and exploring the tests, we need to install a visual studio extension called “NUnit 3 Test Adapter”.
Unit Tests Should Only Test Public Methods The short answer is that you shouldn't test private methods directly, but only their effects on the public methods that call them. Unit tests are clients of the object under test, much like the other classes in the code that are dependent on the object.
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?
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With