testingblack-box-testingwhite-box

How can a white box test fail, and a black box test succeed (and vice versa)?


I have an exam and trying to find an answer to this question but I am unsuccessful so far. Question is:

Give an example white box test says everything okay but black box test says there is an error. And an example black box test says everything okay but white box test says there is an error.


Solution

  • Assuming that with 'black box test' you mean some sort of integration test (ie. only use the public visible UI), and that with 'white box test', you mean some sort of unit test (ie. exposing the technical internals):

    Consider a scenario where you expect the result shown to the user to be 10, maybe a calculation for an invoice or some such.
    This works fine in your integration test, but when you do a unit test, the function responsible for getting 10 is actually returning 9!
    A reason this might happen is that the integration test is running a lot more code than just the unit test, for example, you might do:

    def _function_responsible_for_10
       return 9
    end
    
    def output_to_user
      value = _function_responsible_for_10()
      return value + 1
    end
    

    See the return value + 1? This gets you the correct output, but you get it in the wrong way. This only happens to work in this scenario, but when you add more code relying on function_responsible_for_10 later on (or change output_to_user), you suddenly get different (unexpected) results. Perhaps even worse, you could fix _function_responsible_for_10 at some point to correctly return 10, which will actually break this code!

    The example is simplified, and this is just one problem that might occur, but it should get you thinking in the right direction :-) I encourage you to think of other (possibly better!) examples your self.