Say I have an object called File
which exposes three methods: WriteRecord
, Iterate
, and GetSize
.
The contract for WriteRecord
is that it returns false if the input would cause the File
to exceed 16kb. Otherwise, it returns true and stores the record in the file. However, if the File
is empty, but the Record is larger than 16kb, we accept the record anyway (otherwise it would never get written).
Say I have a test for WriteRecord
where the record is 16.1kb. In this test, we call WriteRecord
and validate that it returned true. Should we also validate that GetSize
returns 16.1kb, to make sure WriteRecord
actually stored this record? And should we also validate that Iterate
returns this record? Or should we have three separate tests for the same 16.1kb case?
Unit testing, being code, is like any other code. There are no absolute rules. Rather, you should design and implement according to the given context and the forces that are at play.
The first question you should probably ask is: Why are we even testing in the first place?
It's impossible to answer the questions in the OP until you know the answer to that particular question. There might be plenty of reasons not to test, just as there are many good reasons to test.
Should you also verify that
GetSize
returns the expected size? Yes, if you care about that (which I suppose that you do).Should you also check that
Iterate
returns the file? Again, yes, if that's part of the object's contract, that sounds like a reasonable assertion.Consider if this would lead you to write duplicated test code. If you need to copy and paste most of the test code in order to have three separate tests, then that's probably not a good idea. On the other hand, if these three test cases are sufficiently different so that you don't have test-code duplication, then three unit tests sounds like a good idea.
Unit test code is code too. This is also code that you have to maintain. Code duplication in test code costs time and effort, just like code duplication in 'production code' does.
Factor test code accordingly.