What’s wrong with this test case code? – Identifying False Positives Part 2 – The Answer


The problem is with this line:

‘ retval is NULL if API failed to set focus on requested window

If retval.Equals(0) Then

Remember, the retval for this API is an IntPtr, not an integer. Even if the IntPtr is null, the exception will not get thrown.

You can do

You can do

If retval.ToInt32.Equals(0) Then

Or

If retval.ToInt32 = 0 Then

Or

If retval.Equals(IntPtr.Zero) Then

But not

If retval.Equals(0) Then

When you’re dealing with lots and lots of APIs in an automation framework, this sort of gotcha might not be as blatantly obvious as it was tonight.

Comments (1)

  1. I think it is great that you are putting together these kinds of exercises. I find these kinds of mistakes in test suites i review all the time.

    I always get confused whether these kinds of problems should be called false positives or false negatives. If you wrongly test positive for HIV that is called a false positive, even though it really isn’t so positive. Since tests are supposed to find bugs, wouldn’t that mean a false positive was when it failed even though there wasn’t really a bug?

    Anyhow, this is confusing enough, that i decided to call what you call a false positive here, a ‘silent horror’. And then when a test fails but there isn’t a bug, i just call that a false alarm. I call them silent horror’s because if you ever find them, you suddenly worry how many other problems like this are burried in your tests.

Skip to main content