-1

Let's say we have function A and function B which perform consecutive operations on some data, with B never receiving the data before A processes it.

Function A makes exhaustive checks on potential errors on the input data. For A, it's a no-brainer to write proper unit tests. Should there also be tests written for B? If it's given invalid data in tests, it crashes, but if there are checks in place there's a performance loss.

4

If we look only at B, then it's a matter of contract for the function B. You can put a precondition into the contract for B that its input data should be valid (per function A). Then you don't need to unit-test B with invalid data.

If B is given invalid data, it crashes but if there are checks in place there's a performance loss.

If B will not check its input data, then design your systems such that B can't be called with invalid data. How to implement this would depend on the nature of A, B, the data. Perhaps, keep track of state and throw an exception [more graceful than crashing outright] if something tries to call B before A.

p.s.
This looks like a fledgling design question, rather than a unit test question. The desire for testable code is prompting a better design.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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