views:

33

answers:

3

Good evening everyone

Suppose your code has the following statement:

if (string ends with char 'y') {
  if ((char before last is not 'e') || (char before last is not 'a')) {
    return (something awesome);
  }
}

So that's simple enough i thought ...

Test1: input = "xy" (last char is y, char before last is not e or a)

Result - partial coverage ...

What other tests i am missing? If this was and && instead of ||, i suppose test would be far easier, but || confused me a bit.

Can you suggest Test2,3,n?

Thank you

+2  A: 

You can also write tests to make sure you don't return something awesome

Test2: input = "ez" does not return something awesome
Test3: input = "af" does not return something awesome

The tests should prove your intended behaviour. What about various lengths of strings?

(empty, '1', '2 ', '3  ')

You may want to turn this into a method or function and name it something appropriate. Then write the tests for this simple method (in TDD you would write them first).

kevpie
+2  A: 

You want to test for the expected behavior with inputs "ey" and "ay".

You may find that your method is not doing quite what you think it is. I think || has indeed confused you a bit.

Don Roby
A: 

There's different types of coverage:

  • Method level coverage (what C++ compilers used to do)
  • Line level coverage
  • Symbol level coverage (most C# coverage tools provide this)
  • Branch level coverage (many Java coverage tools provide this)

I think your confusion is coming in to play because you are thinking in terms of symbol coverage while your tool is giving you branch coverage. Here's the distinction:

Symbol coverage will measure whether you have reached each symbol (i.e. bunch of code up to a ';' character). The below line contains two symbols:

int i = 0; int j = 3;

Branch coverage measures each condition in both the true and false values. In your example you had 4 different conditions listed, each of which have a true branch and a false branch. In order to fully test each branch, you need a test for each of the following conditions:

  • string ends with char y, char before last is e
  • string ends with char y, char before last is a
  • string ends with char y, char before last is neither a nor e
  • string does not end with char y

With the code you wrote, you will probably experience something unexpected. You will get something_awesome no matter what if the string ends in y. If the string ends with 'ey', then it is not ending with 'ay'. If either of those conditions are true you get something awesome. Write the tests and see for yourself.

Berin Loritsch