|
| 1 | +--- |
| 2 | +title: "Writing your first unit test" |
| 3 | +teaching: |
| 4 | +exercises: |
| 5 | +--- |
| 6 | + |
| 7 | +:::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- What does a unit test look like? |
| 10 | + |
| 11 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 12 | + |
| 13 | +::::::::::::::::::::::::::::::::::::: objectives |
| 14 | + |
| 15 | +- Understand the benefits of parameterized tests. |
| 16 | +- Able to write a unit test which is isolated, minimal and fast. |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | + |
| 21 | +The key aspects of a unit test are the same no matter the language being testing |
| 22 | +(python, Fortran, etc) or the framework we are using (pFUnit, etc). Therefore, |
| 23 | +when we are first learning unit testing, it can be useful to think about what the |
| 24 | +content of a unit test might look like before we try to learn the specific syntax |
| 25 | +of any one tool. |
| 26 | + |
| 27 | +## Testing the temperature |
| 28 | + |
| 29 | +We'll now use an example Fortran library which converts between units of temperature. |
| 30 | +This code can be found in the exercises repo under |
| 31 | +[3-writing-your-first-unit-test/challenge/src/temp_conversions.f90][temp-lib]. This |
| 32 | +library contains two functions, one to convert from Fahrenheit to Celsius |
| 33 | +(`fahrenheit_to_celsius`) and another to convert from Celsius to Kelvin |
| 34 | +(`celsius_to_kelvin`). |
| 35 | + |
| 36 | +Imagine we want to use this library to do some temperature conversions from Fahrenheit |
| 37 | +to Kelvin. To ensure the library contains the functionality we need, we decide to write |
| 38 | +some unit tests. |
| 39 | + |
| 40 | +:::::::::::::::::::::::::::::::::::::::: challenge |
| 41 | + |
| 42 | +### Challenge: Pseudo test |
| 43 | + |
| 44 | +Write a unit test in pseudocode for the temperature library to check that it can |
| 45 | +convert from Fahrenheit to Kelvin. |
| 46 | + |
| 47 | +::::::::::::::::::::::::::::::::::: solution |
| 48 | + |
| 49 | +Your test could look something like this... |
| 50 | + |
| 51 | +``` |
| 52 | +Set some input value of Fahrenheit, for example 32.0 |
| 53 | +Call fahrenheit_to_celsius with this input |
| 54 | +Check that the output is equal to the expected value of 0.0 |
| 55 | +
|
| 56 | +Set some input value of Celsius, for example 0.0 |
| 57 | +Call celsius_to_kelvin with this input |
| 58 | +Check that the output is equal to the expected value of 273.15 |
| 59 | +``` |
| 60 | +:::::::::::::::::::::::::::::::::::::::::::: |
| 61 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 62 | + |
| 63 | + |
| 64 | +## Writing a test |
| 65 | + |
| 66 | +All unit tests tend to follow a similar pattern. |
| 67 | + |
| 68 | +1. Define the inputs to your unit of code to be tested as well as the outputs you |
| 69 | + expect from execution with these inputs. |
| 70 | + |
| 71 | +2. Setup and verify any state required for successful execution (verify a file exists, |
| 72 | + allocate memory, etc) |
| 73 | + |
| 74 | +3. Call the unit of code to be tested using the inputs defined in the first step. |
| 75 | + |
| 76 | +4. Verify the actual outputs of your unit of code with the expected outputs defined in the |
| 77 | + first step. |
| 78 | + |
| 79 | +:::::::::::::::::::::::::::::::::::::::: challenge |
| 80 | + |
| 81 | +### Challenge: Standard Fortran test |
| 82 | + |
| 83 | +Write a unit test in standard Fortran for the temperature library to check that it can |
| 84 | +convert from Fahrenheit to Kelvin. You can use your pseudocode as a starting point. |
| 85 | + |
| 86 | +As we are not yet using a testing framework, some boilerplate code has been provided to |
| 87 | +help you create a test-suite. Take a look at part one of the exercise |
| 88 | +[3-writing-your-first-unit-test/challenge][exercises-challenge]. |
| 89 | + |
| 90 | +::::::::::::::::::::::::::::::::::: solution |
| 91 | + |
| 92 | +A solution is provided in [3-writing-your-first-unit-test/solution][exercises-solution]. |
| 93 | + |
| 94 | +:::::::::::::::::::::::::::::::::::::::::::: |
| 95 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 96 | + |
| 97 | +[temp-lib]: https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/3-writing-your-first-unit-test/challenge/src/temp_conversions.f90 |
| 98 | +[exercises-challenge]: https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/3-writing-your-first-unit-test/challenge |
| 99 | +[exercises-solution]: https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/3-writing-your-first-unit-test/solution |
0 commit comments