2

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!!

2
  • 1
    As beginner do the 3rd option. Never 2nd. Commented Oct 17, 2018 at 11:27
  • I think Theory is best used when you need to check your code works for every value of an enumeration, which isn't applicable here. Commented Oct 17, 2018 at 11:35

2 Answers 2

3

Actually, It depends what exactly you're testing. If it extension method as you described, using one test for all Asserts I think should be pretty enough. Unit tests should be fast and readable especially for common service things like extensions. If you need tests business logic, authorization stuff, and check services with wrong inputs or some exceptions - negative tests should be like a separate one per each scenario. IMHO :)

[Fact] public void StringExtension_IsNullOrEmpty_CommonBehaviour() { Assert.True(((string)null).IsNullOrEmpty()); Assert.True(string.Empty.IsNullOrEmpty()); Assert.True(" ".IsNullOrEmpty()); Assert.False("MyTestString".IsNullOrEmpty()); } 

btw, I'd not rather write tests for this kind of extensions in solution project. Create a separate project with all your services functions there (with test coverage), make a common closed package and include this project to your business solution. Then you can rely on this as on third-party library.

Sign up to request clarification or add additional context in comments.

Comments

2

I think single test are more useful, because with an increasing complexity, the troubleshooting becomes easier, if each test case has its own test. Also, when you are developing in a team, it is easier to understand the test if they are split into single scenarios.

2 Comments

Thanks for that Sarah! That makes sense!
I'm not convinced this response should be accepted as the answer. Xunit was purpose built to be different to MSUnit and NUnit by avoiding duplication of test code. Therefore the correct use of this framework is to reuse tests. If you want tests to be isolated, then use MSUnit or NUnit. I appreciate changing after the fact is not acceptable in most real world scenarios.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.