r/softwaretesting • u/ClassKlutzy7633 • 4d 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?
2
u/mystique0712 3d ago
Asserting on the raw Double with a tight epsilon and also doing a separate string check on the formatted output is a common approach.
This way you catch both formula logic errors and display/rounding bugs.
For boundary cases, it's usually best to either avoid them or include them with a specific tolerance that accounts for the expected rounding behavior.
4
u/Roboman20000 4d 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.
1
u/zaphodikus 3d ago
Read up on rounding, its more usefull once you understand where rounding errors come from and to then write code to test to allow for that as a standard fixture or assert in your arsenal.
1
u/Brainy-Cos 3d ago
doing both is pretty normal imo, they're testing two different things. raw value with a tight relative tolerance catches the formula bugs (relative not absolute, since your formulas probably live on pretty different scales). then a smaller set of exact string asserts on the formatted output catches rounding/formatting bugs. with only the display check and one unit of slack you can't really tell a small formula drift from a rounding mode difference
for boundary cases i'd keep them on purpose but label them, like a few golden rows that land exactly on x.xx5 where you assert the exact string you expect for whatever rounding mode you picked. those are the ones that break when someone changes the formatter. just make sure the raw input actually lands on the boundary and floating point doesn't turn it into x.xx4999, or that test gets flaky
also worth one test in a comma-decimal locale if you have users in e.g. germany. "2,63" with replace(",", "") turns into 263 lol
1
u/Glad_Butterfly_4424 2d ago
Your current tolerance seems a bit generous. If you're displaying two decimal places, allowing a difference of 0.01 could let a genuine calculation error through. I'd keep the numerical accuracy check separate from whatever rounding you use for display.