我是否必须对参数可能采用的每个值进行单元测试?

For example, if I have a method that execute simple logic and take an int parameter in input, do I have to unit test cases when input is :

  • a good int (like 1)
  • a bad int (like 822873873824676372732367232637233)
  • a descent float
  • a bad float
  • a bool
  • a string
  • no parameter

Well I think I already know the answer, but where is the limit?

Also, when the method have A and B parameters, do I have to test every cases that could happens by doing these methods? :

  • testGoodAWithoutB
  • testGoodBWithoutA
  • testGoodAWithGoodB
  • testBadAWithoutB
  • testGoodAWithBadB
  • ...

The answer to your question is: no, of course not.

It makes no sense to test every possible value. If you are working with unit tests I guess that you are trying to ceap your code as clean as possible. For this your function should follow the single responsibility principle.

If your function just hase one functionality it makes it easy just to test that.

It depends also what your function can expect. When you have a function add(a, b) it depends on the context inside your programms. When the error handling is done before the function call and you are sure that you will always get valid values there is no need to test this, because your function is not responsible for that. If the same function is for example a public method the function has to check the input values. In that case you should also test that.

You need to keep in mind that your tests has to be readeable for humans. If you are testing everything which could happen the next 100 years the tests will be not so easy to understand.

Your tests should also be something like a documentation for other programmers. So just test your function like your function should be called.