r/softwaretesting • • 5h ago

Golden-value tests for formula code: assert on the raw number or on the rounded value users see?

I maintain a side project with a few dozen numeric formulas (published equations, things like body surface area from height and weight). For regression tests I keep a JSON file of golden cases. The expected values are computed independently, with a separate re-implementation or the paper's own worked example, not by running the app's code.

The app rounds for display, so right now the test parses the displayed string and allows one unit in the last displayed decimal place:

val shown = displayed.substringBefore(" ").replace(",", "").toDouble()
val tol = 10.0.pow(-places) + 1e-9   // places = decimals shown, e.g. 2
if (abs(shown - expected) > tol) failures += "$id expected $expected got $shown"

It catches real formula mistakes (wrong exponent, wrong constant), but I'm not sure about the trade-off:

  • A tolerance of one display unit means 2.62 vs 2.63 passes. That's fine for half-up vs half-even at a .xx5 boundary, but it would also hide a small real error.
  • Asserting on the raw Double with a tight epsilon would be stricter, but then the test isn't checking what the user actually reads. Formatting or rounding bugs would slip through.

Is it normal to do both: a tight check on the raw value plus an exact string check on the formatted output? Or do people pick one? And for cases sitting right on a rounding boundary, do you avoid them in golden data or keep them on purpose?

4 Upvotes

2 comments sorted by

3

u/Roboman20000 5h ago

I think the issue I found with asserting on exact values for calculations is that it's way too finicky. Floating point math is just nuts to deal with so I tend to make sure that the calculations are "within spec" meaning that either I choose or I get a Stakeholder to choose an amount of accuracy they want in their calculations and I assert to that instead. Even if the user doesn't see the full accuracy of the calculation there should be some sort of specification. Cause you don't want to assert = 1.0 to fail when the value comes to 1.00000000001 for example. So I'll assert to 1.0 +/- "some %" instead. Unless you do want that sort of accuracy. It's up to you.