1

Say I have three methods, all very similar but with different input types:

void printLargestNumber(int a, int b) { ... }
void printLargestNumber(double a, double b) { ... }
void printLargestNumber(String numberAsString, String numberAsString) { ... }

All three use the same underlying logic. For example: maybe the double version is the only one that compares numbers, and the other two just convert their inputs to double.

We could imagine a few different unit tests: first input is larger, second is larger, both inputs are negative, etc.

My Question

Should all three methods have the full set of tests (black box since we don't assume the core implementation is the same)

or

Should only the double version be tested heavily and the other two tested lightly to verify parameter conversion (white box testing since we know they share the same implementation and it's already been tested in the double tests)?

1

3 Answers 3

4

If all of those methods are public, i.e. callable by the outside world, I'd definitely test all of them with a full set of tests. One good reason is that white-box tests are more brittle than black-box tests; if the implementation changes the public contract might change for some of those methods.

1
  • I agree white box unit tests are more brittle, I have battled with tests tightly coupled to the implementation, small changes often break many tests and the time to complete a change can be less than the time to fix tests. Whats your view on methods that do not return anything, say you have an AWS lambda function and it takes an event and does a few things, fetches data from a database table and puts messages onto a queue but the only output to a caller is an exception. How do you verify your function is correct without a white box approach? A black box approach can only test error scenarios.
    – berimbolo
    Feb 1, 2023 at 15:32
3

There are a set of tests that explicitly exercise the public interfaces. I would treat those as black-box tests.

There are a second set of tests that could be seen as looking at the corner cases of the implementation. This is white box testing and surely has a place in a Unit test. You can't know the interesting paths without some white-box implementation knowledge. I would pay particular attention to the String case, because the interface allows for strings that may not convert cleanly to doubles, that push the boundaries of precision etc.

Would I cut a few corners in the integer case? I know I pushed the paths in the double case, probably shouldn't but might well under time pressure.

0
2

It depends.

Do you think the implementation is likely to change? If so then go with black box testing.

If you can guarantee that the implementation won't change go with white box. However, the chances of you being able to guarantee this aren't 100%.

You could compromise and do some of the black box tests, particularly around the boundary conditions. However, writing the tests should be easy - so there's no excuse from that point of view for not doing full black box testing. The only limiting factor is the time it takes to run the tests.

Perhaps you should investigate the possibility of running the tests in parallel.

2
  • +1 for "writing the tests should be easy". It's true, they would all be very similar and easy to write, and it would help if the implementation does change (which is what tests are for!). Thanks for the answer!
    – Brad Cupit
    Dec 1, 2010 at 23:01
  • 1
    You cannot guarantee the implementation won't ever change. Not 50%, not 20%, not 1%. You just can't. Everything may change at some point. And if you test only a part of your code "because I know this is actually an alias to that", then, when it changes (not if, when) you suddenly (without realizing it right away) stop testing a part of your code. Not good. Always assume everything can, and most of it will change at some point. Test accordingly. Feb 24, 2013 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.