Unit conversions
In the first step of the Measurement toolkit tutorial we downloaded the initial source code and installed the pillow package. In this step we will complete the functionality and testing for the unit conversion part of the tool. This will consist of conversions for metric units for length, area and volume.
Testing length conversions
We begin by considering conversions for metric units of length. The following diagram shows the process for converting between the four most widely used units (millimetres, centimetres, metres and kilometres).
To perform a conversion we follow the arrows from the initial unit to the target unit, applying the operation attached to each arc.
Conversion calculations are firstly written by hand – these will then form the basis for test cases. For example, calculations for converting kilometres to millimetres are shown below.
Once calculations have been done to cover all of the metric length unit conversions they should be coded in the convert_units_tester.py
file. The existing code for this is shown below. Test cases for converting from metres have already been included. The highlighted code (lines 6 and 7) contains a function to test whether the conversion from metres to millimetres is correct.
Each test case function begins with the keyword def
followed by the name of the function, in this case test_m_to_mm
, finally we include the input variable self in brackets followed by a colon. The first line of each test case function will the same except the function name will differ – ensure you give a meaningful name and make sure each function has a different name. The second line of the function contains the actual test case. The assertEqual
function takes two arguments. The first argument on line 7 calls the convert_length_unit
function and returns a result. The second argument contains the expected (correct) result. If the first argument equals the second argument then the test passes. Otherwise the test fails, indicating that we probably have an error in the code in the convert_length_unit
in the file convert_units.py
.
import unittest from convert_units import * class TestConvertUnits(unittest.TestCase): def test_m_to_mm(self): self.assertEqual(convert_length_unit(0.23,"m","mm"),230) def test_m_to_cm(self): self.assertEqual(convert_length_unit(0.25,"m","cm"),25) def test_m_to_m(self): self.assertEqual(convert_length_unit(178,"m","m"),178) def test_m_to_km(self): self.assertEqual(convert_length_unit(19000,"m","km"),19) if __name__ == '__main__': unittest.main()
The tester code imports the code in convert_units.py
on line 2, which contains the conversion functions. We will look at this file in more detail a little later. Before we do so we must complete the length unit conversion tests in convert_units_tester.py
file.
For example, to add a test based on the kilometre to millimetre calculation shown earlier we would add the following code.
def test_km_to_mm(self): self.assertEqual(convert_length_unit(1.2,"km","mm"),1200000)
This code would be included immediately after the previous test functions, making sure it is indented to the same level as the other test functions.
When the tester code is run it creates a report on how many tests passed or failed. The ultimate aim is obviously to pass all tests.
To ensure that all functionality is tested we should have at least one test case for each possible conversion, including conversions from a unit to itself (e.g. from metres to metres).
This means that you will need to write at least sixteen (16) length conversion tests.
When you run your length conversion tests you should get a number of failed tests – these represent errors in the convert_length_unit
code. As mentioned the code for this function is given in the convert_units.py
file and is listed below. Running the tests should help you identify the errors in the code. These should be corrected and tests should be rerun to ensure the function is free of errors.
def convert_length_unit(value,inunit, outunit): if (inunit==outunit): result=value elif (inunit=="m") & (outunit=="mm"): result=value*1000 elif (inunit=="m") & (outunit=="cm"): result=value*10 elif (inunit=="m") & (outunit=="km"): result=value*1000 elif (inunit=="km") & (outunit=="mm"): result=value*1000000 elif (inunit=="km") & (outunit=="cm"): result=value*10000 elif (inunit=="km") & (outunit=="m"): result=value*1000 elif (inunit=="cm") & (outunit=="mm"): result=value*100 elif (inunit=="cm") & (outunit=="m"): result=value/100 elif (inunit=="cm") & (outunit=="km"): result=value/100*1000 elif (inunit=="mm") & (outunit=="cm"): result=value/10 elif (inunit=="mm") & (outunit=="m"): result=value/1000 elif (inunit=="mm") & (outunit=="km"): result=value/1000*1000 return result
The function takes three input arguments, the first is the numerical value to be converted, the second is the original units of the measurement, and the third is the target units of the measurement. The function is implemented using a nested if-then-else statement. At the end of the function the converted measurement is returned as the result.
Area conversions
The next step is to develop the area unit conversions.
If you are doing this part of the development in pairs, then one person should design and develop the test cases in convert_units_tester.py
, while the second person writes the area conversion function in convert_units.py
The diagram below shows the conversion process for units of area.
For example, consider the following conversion calculation:
This process can be repeated on paper to design test cases for each of the 16 possible area conversions (remember we need to include tests that check that conversions for a unit to itself leave the value unchanged). Once the test cases have been calculated on paper they can be added to the convert_units_tester.py
file. The should be added immediately after the length unit test functions. For example, the test case calculated above would be written as:
def test_m2_to_mm2(self): self.assertEqual(convert_area_unit(5.8, "m2", "mm2"), 5800000)
Notice that the test function name indicates that units of area are being converted – don’t forget to include the correct units in the function name. The test is implemented by calling the assertEqual
function. This in turn calls convert_area_unit
which takes three arguments – the original area measurement, the original units of area and the target units of area. The units of area include a 2 at the end to distinguish them from units of length.
The last step in the process is to write the code for the convert_area_unit
function, contained in the
convert_units.py
file. In the supplied code this function is written as a code stub, meaning that the function is defined, but it is not implemented correctly. In this case the function stub returns 0 regardless of the inputs to the function as shown below.
def convert_area_unit(value,inunit,outunit): return 0
The second line of the function needs to be replaced by code similar to that given for the convert_length_unit
function, remembering though that the units of area will all need to include a 2 at the end. To implement this function, you should use the earlier area conversion diagram as a guide.
Once the code for the convert_area_unit
function has been written, then you should test it by running the test cases in convert_units_tester.py
.
Volume conversions
The last step in completing the unit conversions for the Measurement toolkit is to add volume conversions. As with the area conversions, if you are working in pairs one person should work on designing and implementing the test cases, while the other person develops the conversion code. For this task however you should swap roles.
The calculations for an example conversion is shown below:
Calculations should be completed for all 16 possible conversions. These test cases should then be implemented in the convert_units_tester.py
file.
The conversion functionality is added to the tool by implementing the following code stub in
the convert_units.py
file, replacing the second line with appropriate code.
def convert_volume_unit(value,inunit,outunit): return 0