In TDD, how many tests should I write for a method?

I want to implement a method that tells me that the coordinates (x and y) are out of bounds. How many tests should I write? It seems to me 5:

  • Negative x test for related
  • Test for positive x on related
  • Testing negative y for related
  • Testing positive y for related
  • Limit Test

I am creating redundant tests and should I have only 1 test for each method that I want to implement?

+3
source share
5 answers

This is usually not the way we think about it in TDD. It is more: "What test do I need next?" Therefore, as a rule, I started with (pseudocode)

given: bounds (5, 10, 15, 20)
assert: outOfBounds(0, 0)

and complete this pass with

outOfBounds(x, y): return true

, , , .

assert: !outOfBounds(5, 10)

, . , ? ,

outOfBounds(x, y): return x == 0

, , , . , . , 5 , " ", - , , , .

: ?

+8

, , - , .

, TDD ( ), , , - , , . ( , " ", , , ?)

, , .

+4

TDD . - , - " , ?" : " , ?" , : " , ?" " , , ". , , , , ( ) , .

, , , : ", ". , .

+1
source

In my opinion, I will return to the rule of thumb for testing with good data, bad data, no data. Therefore, for a method with one input and a return value, I would think that I would need at least three tests. I would like to hear what others think about it.

0
source

I would personally say that you need one test case.

In this case, you should check all the boundaries that you need.

So, 1 test method that checks 5 boundaries.

-3
source

All Articles