I have migrated to using Xunit for unit tests from NUnit. With NUnit, I would create one method with multiple test cases that have the same outcome. For example, the following NUnit unit test tests the validation of a class constructor, specifically the "name" variable. The name can't be null, empty or whitespace. The test checks that an ArgumentNullException is correctly thrown:
[Test] [TestCase(null)] [TestCase("")] [TestCase(" ")] [ExpectedException(typeof(ArgumentNullException))] public void Constructor_InvalidName_ExceptionThrown(string name) { // action make_Foo(name); } private make_Foo(string name) { return new Foo(name); } This is how I have implemented this using Xunit:
[Fact] public void Constructor_InvalidName_ExceptionThrown() { Assert.Throws<ArgumentNullException>(() => new Foo(null)); Assert.Throws<ArgumentNullException>(() => new Foo("")); Assert.Throws<ArgumentNullException>(() => new Foo(" ")); } This seems bad for two reasons - I have multiple Asserts in what is supposed to be a "unit" test, and with the test cases buried within the method (which could get a lot more complicated in some of the other unit tests).
What is the preferred way to handle multiple test cases in Xunit?
Theorythat you can use.