Should I check that the methods do not throw exceptions?

I take my first baby steps with unit testing and wrote (among other things) these two methods:

[TestCase] public void InsertionSortedSet_AddValues_NoException() { var test = new InsertionSortedSet<int>(); test.Add(5); test.Add(2); test.Add(7); test.Add(4); test.Add(9); } [TestCase] public void InsertionSortedSet_AddValues_CorrectCount() { var test = new InsertionSortedSet<int>(); test.Add(5); test.Add(2); test.Add(7); test.Add(4); test.Add(9); Assert.IsTrue(test.Count == 5); } 

Do I really need a NoException method? If an exception is thrown, it will also be fired in the CorrectCount method.

I tend to save it as 2 test cases (maybe reorganize the repeating code as another method), because the test should only check for one thing, but maybe my interpretation is wrong.

+17
c # unit-testing nunit
Jan 09 '12 at 12:37
source share
6 answers

To put it in the simplest terms, IMO testing of which method does not can be very slippery, as you can come up with more and more scenarios when you think about it. Going the other way, claiming that your code does what you intended to do is pretty much the goal of unit testing.




There are two simple questions that usually help me identify a suspicious test and find out if it makes any sense:

  • What part of the desired functionality is testing?
  • what simple change can i make in test class to break test?

Note that it is extremely simple to solve these issues having a second test ( _CorrectCount ). We really did not see the code of the Add method, but we can most likely get a decent assumption of what could have been changed to interrupt this test. Proven functionality is even more obvious. The answers are intuitive and seem quick (which is good!).

Now try to answer these questions for the first test ( _NoException ). It immediately raises new questions (Is working code real functionality? Isn't that obvious? Isn't that implied? Isn't that what we always strive for? Why is there no statement at the end ??). On the second question, this is even worse - breaking this test is likely to require a clear exception exception ... that we all agree not to go.

Conclusion

Simple The second test is a great example of a well-written unit test . It is short, it checks one thing, it can be easily understood. The first test is not . Even in that it is just as short and (it seems) simple, it introduces new questions (while it really should answer already stated - does Add add?). - and as a result brings unnecessary complexity.

+17
Jan 9 2018-12-12T00:
source share

It makes sense to make a method that checks that the test method throws exceptions when you expect it. The first test, which you do not state, does not mean that the second test is not yet distributed.

+6
Jan 09 2018-12-12T00:
source share

I always test both working and non-working. Thus, you confirm that the code works, returns the correct results, and also processes errors in the expected way.

+2
Jan 09 '12 at 12:40
source share

Imo, if you are sure that the InsertionSortedSet works in the worklist (I'm not sure where it comes from), I would skip testing the InsertionSortedSet_AddValues_NoException , as you intend to do, if necessary.

Of course, it's best to check as much as possible.

+1
Jan 09 2018-12-12T00:
source share

There is no 100% correct answer.

On the one hand, you are right, one test should check for one thing.
This is especially true in cases where one of the tests may change in the future, and then you will not be able to know for sure whether you are testing another test.

On the other hand, you are creating redundancy, since both tests actually check the same thing.
This redundancy is bad only in cases where your tests take too long to run, but since (as it seems) you have only a few tests, this should not be a problem.

+1
Jan 9 2018-12-12T00:
source share

There is no statement and expected exception in the first test, I think it should be reorganized.

IMO, one test should check for incorrect behavior (expecting an error to be selected), and the other for the correct behavior (exception is not returned, a good value is returned).

+1
Jan 09 2018-12-12T00:
source share



All Articles