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

Really easy one tonight, but something ever tester working with APIs should be aware of. Even if you know better, someone else might not.

Here’s a little example I threw together in the past 10 minutes to illustrate tonight’s point.

Public Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal HWnd As IntPtr) As IntPtr

Public Sub SetWindowFocus(ByVal hwnd As IntPtr)

‘ call the Windows SetFocus function

Dim retval As IntPtr = SetFocus(hwnd)

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

If retval.Equals(0) Then

Throw New Exception("Failed to set focus")

End If

End Sub

‘ the actual test case code

Public Sub Run()


InitScenario("Verify user can set focus on window")


‘ get the hwnd for the editor

Dim hwnd As IntPtr = GetSomeWindowsHwnd()

Dim ex As Exception

‘ try to set focus to window



Catch ex

End Try

‘ if we catch an exception, log a failure

If ex Is Nothing Then




End If

‘ continue with the rest of test case

End Sub

By popular demand, I will post the answer on a second entry. At least this way, if you find a bug in this code that I wasn’t expecting, I can play it off that I planned it that way. =)

Comments (4)

  1. dxoigmn says:

    Minor error in your example test case. Should be SetWindowFocus(hwnd) instead of SetWindowFocus() 🙂

  2. Well the obvious error is this line:

    > If ex Is Nothing Then

    ex should be out of scope at this location. Who hasn’t turned on option explicit?

  3. sara ford says:

    dxoigmn: thanks for catching that! I should have paid more attention to my syntax errors. =)

    Geoff: Ex isn’t out of scope at this point. I defined it outside the try catch. Works whether Explicit is on or off.

  4. Daniel C says:

    The only functionality that seems to be tested here is that SetWindowFocus(hwnd) does not throw an exception. Surely an empty method would pass this test case equally well.

Skip to main content