-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.

  • Possible duplicate of [How should I test the functionality of a function that uses other functions in it?](https://softwareengineering.stackexchange.com/questions/225323/how-should-i-test-the-functionality-of-a-function-that-uses-other-functions-in-i) – gnat Nov 26 '18 at 21:54
  • @gnat that question doesn't actually shed light on the question of "crashing vs performance". So I guess the answer is *don't write tests if you lose performance", i.e. the function should be tested only for what's intended to do and for what data is always meant to receive? – Paul Razvan Berg Nov 26 '18 at 21:58

1 Answers1

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.

Nick Alexeev
  • 2,484
  • 2
  • 17
  • 24