Designing a reliable unit test - testing the same logic in several ways?

In unit test design, it is very easy to fall into the trap by actually calling your implementation logic.

For example, when testing an array from int, which should be higher than two (2, 4, 6, 8, etc.), is it really enough to get the return value from the method and claim that this template is the case?

Am I missing something? It seems that one unit test method should be more reliable by testing the same expectation in several ways. So the above expectation can be stated by checking for an increase of two, but also the next number is divisible by 2. Or is it just redundant logic?

So, does unit test need to test for one expectation in several ways? For example, if I wanted to check that my pants fit me, I could / could measure the length, put it next to my leg and see the comparison, etc. Is this the logic that is needed for unit testing?

thank

+5
source share
5 answers

Your unit tests should indicate all your assumptions. If you do this in 1 test or several tests, this is a personal preference.

In the above example, you had two different assumptions: (1) Each value should increase by 2. (2) All values ​​must be even.

Should (-8, -6, -4, -2) pass / fail?

, , , , , , .

+3

, 2,4,6,8, , , , , , 6,8, 10,12. . .

, , , , TDD ...

+1

, , - . , , ?

+1

, ints, (2, 4, 6, 8 ..), , , ?

, , . ? , . ?

- ? unit test , . 2. ? ?

... 1,3,5,9 assertEachValueIncrementsByTwo, assertValuesDivisibleByTwo. , 2? , . , .

, . assertValuesDivisibleByTwo, , , .

, , 100 . , , .

+1

. - , , . . [1,3,4,5], , , , .

. , , .

testEntriesStepByTwo

testEntriesAllEven

. , , " 2 , " " - " , ?

+1

All Articles