I am trying to structure my unit tests nicely and I'm not sure of the best way to split up my tests. I'm using C# and xUnit.
For the purpose of this question, I'm testing this simple extension method code:
public static bool IsNullOrEmpty(this string str) { return string.IsNullOrEmpty(str); } I've a few options around testing and I don't know what's correct. I could write a Fact or Theory.
Theory Should I use Theory? One test for positive and one for negative?
[Theory, IsUnit] [InlineData(null)] [InlineData("")] public void Test_IsNullOrEmpty_Positive(string value) { Assert.True(value.IsNullOrEmpty()); } [Theory, IsUnit] [InlineData("TEST")] [InlineData(" ")] public void Test_IsNullOrEmpty_Negative(string value) { Assert.False(value.IsNullOrEmpty()); } Fact Should I use Fact? Positive and negative in one:
[Fact, IsUnit] public void Test_IsNullOrEmpty() { // Positive testing. string myString = null; Assert.True(myString.IsNullOrEmpty()); myString = ""; Assert.True(myString.IsNullOrEmpty()); // Negative testing. myString = " "; Assert.False(myString.IsNullOrEmpty()); myString = "TEST"; Assert.False(myString.IsNullOrEmpty()); } OR should I have single tests:
[Fact, IsUnit] public void Test_IsNullOrEmpty_Null() { string myString = null; Assert.True(myString.IsNullOrEmpty()); } [Fact, IsUnit] public void Test_IsNullOrEmpty_Empty() { var myString = ""; Assert.True(myString.IsNullOrEmpty()); } [Fact, IsUnit] public void Test_IsNullOrEmpty_Space() { var myString = " "; Assert.False(myString.IsNullOrEmpty()); } [Fact, IsUnit] public void Test_IsNullOrEmpty_Word() { var myString = "TEST"; Assert.False(myString.IsNullOrEmpty()); } I'm not that experienced writing tests and want to follow best practices, so I only want to know which of the above is best practice in the context of C# and xUnit (hopefully there are rules to this and this question is not opinion based :-)).
Thanks for any pointers in advance!!