Testing for exceptions with [TestCase] attribute in NUnit 3?

ExpectedException would have been the correct method for NUnit 2.X, but it was removed from NUnit 3. There’s a various snippets of discussion in the NUnit Google Group and the equivalent Dev group – but it looks like the decision was made that it’s generally a better design pattern to test expected outcomes, and exceptions … Read more

Persist variable changes between tests in unittest?

As some comments have echoed, structuring your tests in this manner is probably a design flaw in the tests themselves and you should consider restructuring them. However, if you want to do this and rely on the fact that the test runner you are using executes them in an alphabetical (seemingly) order then I suggest … Read more

How do I put new List {1} in an NUNIT TestCase?

There is one option to use TestCaseSource attribute. Here I provide a non-assert test with two cases just to see how it works: [TestFixture] public class TestClass { private static readonly object[] _sourceLists = { new object[] {new List<int> {1}}, //case 1 new object[] {new List<int> {1, 2}} //case 2 }; [TestCaseSource(“_sourceLists”)] public void Test(List<int> … Read more

NUnit TestCase with Generics

NUnit test methods actually can be generic as long as the generic type arguments can be inferred from parameters: [TestCase(42)] [TestCase(“string”)] [TestCase(double.Epsilon)] public void GenericTest<T>(T instance) { Console.WriteLine(instance); } If the generic arguments cannot be inferred, the test runner will not have a clue how to resolve type arguments: [TestCase(42)] [TestCase(“string”)] [TestCase(double.Epsilon)] public void GenericTest<T>(object … Read more

Test cases in inner classes with JUnit

You should annontate your class with @RunWith(Enclosed.class), and like others said, declare the inner classes as static: @RunWith(Enclosed.class) public class DogTests { public static class BarkTests { @Test public void quietBark_IsAtLeastAudible() { } @Test public void loudBark_ScaresAveragePerson() { } } public static class EatTests { @Test public void normalFood_IsEaten() { } @Test public void badFood_ThrowsFit() … Read more