How Can You Test Unit Conversions?
From the earlier reference collection. Read the current software blog.
Use recognizable temperatures, inverse conversions, and a stated rounding tolerance to test both directions.
Choose values with clear expectations
A conversion test should use values whose expected results are familiar and whose precision policy is explicit. For Celsius and Fahrenheit, 0 °C corresponds to 32 °F, and 100 °C corresponds to 212 °F. These anchor cases catch swapped formulas and incorrect offsets. Add a negative or fractional value to exercise arithmetic beyond the easiest round numbers.
Keep units attached to inputs and outputs in names or test structures. A bare number such as 32 does not say whether it is Fahrenheit or Celsius, and unit confusion can survive a numerically plausible assertion.
Check both directions
Test Celsius to Fahrenheit and Fahrenheit to Celsius independently. Then use round-trip checks: convert a Celsius value to Fahrenheit and back, and do the reverse with a Fahrenheit input. A round trip should recover the original within the precision promised by the application, but round trips alone are not enough. Two inverse functions can share a matching error, so retain known-value assertions as well.
For a simple example, convert 20 °C to 68 °F, then back to 20 °C. If the interface rounds displayed values to one decimal place, test the underlying numeric result separately from the formatted display.
State the tolerance and rounding rule
Floating-point calculations may produce a value extremely close to the expected result rather than an identical binary representation. Use an absolute tolerance suitable for the application's stated precision, such as a small fraction of a degree for an internal calculation. Do not choose a tolerance so wide that a meaningful formula error can pass.
If the output is rounded to whole degrees, test the rounding boundary explicitly, including values just below and above a half-degree according to the chosen rounding convention. Formatting policy is part of the observable behavior and may differ from the precision of internal values.
Cover invalid and unchanged cases
Decide what happens for unsupported units, nonnumeric input, and missing values. Tests can verify a clear error or a structured unavailable result; they should not permit an accidental conversion using a default unit. Include a value such as zero in both scales so truthiness-based shortcuts cannot discard it.
A compact suite with anchors, inverse checks, precision edges, and invalid inputs gives confidence that the conversion stays mathematically sound and understandable to users.