I switched to using Xunit for unit tests from NUnit. With NUnit, I would create one method with several test cases that have the same result. For example, the following NUnit unit test checks the validation of the class constructor, in particular the variable "name". The name cannot be empty, empty or whitespace. The test checks the correctness of the output of ArgumentNullException:
[Test] [TestCase(null)] [TestCase("")] [TestCase(" ")] [ExpectedException(typeof(ArgumentNullException))] public void Constructor_InvalidName_ExceptionThrown(string name) {
Here is how I implemented this with 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 a few statements about what should be a βsingleβ test, and with test cases embedded in a method (which can be a lot more complicated in some other unit tests).
What is the preferred way to handle multiple test cases in Xunit?
Ivan
source share