Marty Wallace
Marty Wallace

Reputation: 35796

Unit testing - how many test cases here

I have a method that takes an array as an argument, and returns true or false depending on the presence of a particular value.

In this scenario how many test cases should be written?

I think 3:

  1. If the value is present
  2. If the value is not present
  3. If the array is empty (could be covered by 2 though?? )

Upvotes: 1

Views: 110

Answers (2)

martinstoeckli
martinstoeckli

Reputation: 24151

It is the code of the function you want to test, so you cannot tell how many test cases are useful. Think again what your code does, how will the value be found?

An example: If your code tries to find a value with a certain name, and you make a string comparison, then think of the problems that can arise with string comparisons -> should the key be found case (in)sensitive, is null equal to an empty string, how does it handle duplicates and are other types converted correctly to strings (type juggling)?

Upvotes: 0

Nagarjun
Nagarjun

Reputation: 2476

I can think of 3 test cases:

  1. If the array is not empty (or not null)
  2. If the value is valid or not (I can pass an object where it expects a string :) )
  3. If the value is present in array

Upvotes: 2

Related Questions