A SW-Component "SWC-A" is integrated with a SW-Component "SWC-B". The SWC-A has an output interface which provides "Distance in meters". The SWC-B receives this value and does some calculation. If in SWC-B interprets the received value is interpreted as "Distance in Feet" and not in meters. How can this scenario be tested (static/dynamic), where Units of measure between two components are different.

  • Can you give some more information about how these two components interact? Is one component passed as a constructor argument to another? A function argument maybe? Is the result of A being passed to B via an object that coordinates A and B? Mar 5, 2019 at 12:19
  • There is no way, in general, to test this error. Mar 5, 2019 at 14:38

2 Answers 2


If the calculation is deterministic, the values of correct (in meters) and incorrect (in feet) results are known to you in advance for any specific input. Choose an input to A and write tests for the output of B based upon the possible output values. One test to tell if the result is correct, and another to test for the specific case where the result is wrong because B assumed feet instead of meters.

  • Can you provide an example? I can't think of a way to tell whether the value 2 is in feet or in meters. Mar 5, 2019 at 14:43
  • 1
    @BobDalgleish You know what your method is supposed to do, so you know what the answer should be. E.g., you want to calculate how long it takes to fall a distance x. If x is 5 meters, the answer should be ~1 second, whereas if it was 5 feet, the answer should be ~0.5 second. If you input 5 to SWC-A, the answer should be 1. If you input 5 to SWC-B, the answer should be 0.5.
    – mmathis
    Mar 5, 2019 at 15:36
  • You have two independent parameters for your proposed test, which you can test against. On the other hand if SWC-B calculates the area, then no test is possible. Mar 5, 2019 at 18:28
  • @BobDalgleish In the case of area, units don't matter. The question implicitly suggests units matter.
    – Joe
    Mar 5, 2019 at 20:14
  • @Joe well, units do matter very much with area - 5 m^2 is quite a larger area than 5 ft^2. The problem here is that it is impossible to differentiate the two cases, without some additional work.
    – mmathis
    Mar 6, 2019 at 0:24

If you have the ability to change the codebase(s), you can add the notion of a measurement unit to your methods, either as a separate class or as an additional (string) parameter. I've worked with products / APIs that had a fully-fleshed out unit and coordinate system, defining unit, unit measurement, symbols, as well as a complete conversion utility. That may be overkill for your application, but even a smaller subset of those classes would very clearly define the units being used in your methods / classes. This would then lend itself to testing using feet and meters in both methods / components.

A Measurement class may be as simple as a value and a unit symbol (m), or include the unit string (meters), or an entire class for the unit. Depends on how flexible you need it to be.

A word of wisdom from experience in dealing with various unit systems: your API should always use a single unit system - whether it be SI, Imperial, or some hodge-podge of units common to your domain (in oil & gas, for instance, it is common to work with times in milliseconds, so using the SI standard of seconds would be a bit unwieldy). The expected units should also be clearly defined in your documentation, so there is never any confusion as to what units need to be passed in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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