Skip to content

Commit 6f01f01

Browse files
committed
differences for PR #36
1 parent bc55b34 commit 6f01f01

6 files changed

Lines changed: 115 additions & 14 deletions

3-writing-your-first-unit-test.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ end function my_test_params_toString
357357

358358
## Challenge: Write Fortran unit tests in multiple frameworks.
359359

360-
Take a look at [3-fortran-unit-test-syntax/challenge](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/3-fortran-unit-test-syntax/challenge).
360+
Take a look at [4-fortran-unit-test-syntax/challenge](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/4-fortran-unit-test-syntax/challenge).
361361

362362
:::::::::::::::::::::::::::::::: solution
363363

364-
A solution is provided in [3-fortran-unit-test-syntax/solution](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/3-fortran-unit-test-syntax/solution).
364+
A solution is provided in [4-fortran-unit-test-syntax/solution](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/4-fortran-unit-test-syntax/solution).
365365

366366
:::::::::::::::::::::::::::::::::::::::::
367367
:::::::::::::::::::::::::::::::::::::::::::::
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ This results in the output
196196
197197
```bash
198198
$ ctest
199-
Test project /Users/connoraird/work/fortran-unit-testing-exercises/episodes/4-debugging-a-broken-test/challenge-1/build-cmake
199+
Test project /Users/connoraird/work/fortran-unit-testing-exercises/episodes/5-debugging-a-broken-test/challenge-1/build-cmake
200200
Start 2: pfunit_transpose_tests
201201
1/1 Test #2: pfunit_transpose_tests ........... Passed 0.24 sec
202202

@@ -449,11 +449,11 @@ end do
449449
450450
### Challenge 2: Debug and fix a failing test.
451451
452-
Take a look at the [4-debugging-a-broken-test/challenge-1 README.md](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/4-debugging-a-broken-test/challenge-1/README.md) in the exercises repository.
452+
Take a look at the [5-debugging-a-broken-test/challenge-1 README.md](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/5-debugging-a-broken-test/challenge-1/README.md) in the exercises repository.
453453
454454
:::::::::::::::::::::::::::::::: solution
455455
456-
A solution is provided in [README-solution.md](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/4-debugging-a-broken-test/challenge-1/README-solution.md).
456+
A solution is provided in [README-solution.md](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/5-debugging-a-broken-test/challenge-1/README-solution.md).
457457
458458
:::::::::::::::::::::::::::::::::::::::::
459459
::::::::::::::::::::::::::::::::::::::::::::::::
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ end subroutine TestMySrcProcedure
134134

135135
### Challenge 1: Testing MPI parallel code
136136

137-
Take a look at [5-testing-parallel-code/challenge](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/5-testing-parallel-code/challenge) in the exercises repository.
137+
Take a look at [6-testing-parallel-code/challenge](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/6-testing-parallel-code/challenge) in the exercises repository.
138138

139139
:::::::::::::::::::::::::::::::: solution
140140

141-
A solution is provided in [5-testing-parallel-code/solution](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/5-testing-parallel-code/solution).
141+
A solution is provided in [6-testing-parallel-code/solution](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/6-testing-parallel-code/solution).
142142

143143
:::::::::::::::::::::::::::::::::::::::::
144144
::::::::::::::::::::::::::::::::::::::::::::::::

config.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ contact: 'c.aird@ucl.ac.uk'
6868
episodes:
6969
- 1-what-is-a-unit-tests.md
7070
- 2-refactor-fortran.md
71-
- 3-fortran-unit-test-syntax.md
72-
- 4-debugging-a-broken-test.md
73-
- 5-testing-parallel-code.md
71+
- 3-writing-your-first-unit-test.md
72+
- 4-fortran-unit-test-syntax.md
73+
- 5-debugging-a-broken-test.md
74+
- 6-testing-parallel-code.md
7475

7576
# Information for Learners
7677
learners:

md5sum.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
"file" "checksum" "built" "date"
22
"CODE_OF_CONDUCT.md" "c93c83c630db2fe2462240bf72552548" "site/built/CODE_OF_CONDUCT.md" "2022-08-05"
33
"LICENSE.md" "16e8eaad880865bc4a41811b3e8fa945" "site/built/LICENSE.md" "2025-02-03"
4-
"config.yaml" "96d2d16818f2fa55447be304eec49cfc" "site/built/config.yaml" "2025-11-04"
4+
"config.yaml" "fd0f4d4bf38c1fefef16b2739a95233a" "site/built/config.yaml" "2025-12-05"
55
"index.md" "9b341101d36480c9cf55c871cd1fdffd" "site/built/index.md" "2025-11-04"
66
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2022-04-22"
77
"episodes/1-what-is-a-unit-tests.md" "ba669da4e6eb98ad7357bedcecbd6b27" "site/built/1-what-is-a-unit-tests.md" "2025-11-04"
88
"episodes/2-refactor-fortran.md" "7b7e22ab2b944f524a1b115cdb06ba3f" "site/built/2-refactor-fortran.md" "2025-11-04"
9-
"episodes/3-fortran-unit-test-syntax.md" "985339fc735a2ae67b83a94e59039ae4" "site/built/3-fortran-unit-test-syntax.md" "2025-08-05"
10-
"episodes/4-debugging-a-broken-test.md" "a492b0a1b576f95baa4d1baa0aa67712" "site/built/4-debugging-a-broken-test.md" "2025-07-30"
11-
"episodes/5-testing-parallel-code.md" "7b7425010bc9a1413d1a8891bf34a7c5" "site/built/5-testing-parallel-code.md" "2025-08-05"
9+
"episodes/3-writing-your-first-unit-test.md" "9582bf8c63f9d3a0f2f0b231a3b87e23" "site/built/3-writing-your-first-unit-test.md" "2025-12-05"
10+
"episodes/4-fortran-unit-test-syntax.md" "49f61dc719bac6123af97232692c8dd5" "site/built/4-fortran-unit-test-syntax.md" "2025-12-05"
11+
"episodes/5-debugging-a-broken-test.md" "1248112200207cd778b0f707f8cb4acd" "site/built/5-debugging-a-broken-test.md" "2025-12-05"
12+
"episodes/6-testing-parallel-code.md" "7b97f99ccfbdf57bbef63e0dfd7bc988" "site/built/6-testing-parallel-code.md" "2025-12-05"
1213
"instructors/instructor-notes.md" "cae72b6712578d74a49fea7513099f8c" "site/built/instructor-notes.md" "2023-03-16"
1314
"learners/reference.md" "1c7cc4e229304d9806a13f69ca1b8ba4" "site/built/reference.md" "2023-03-16"
1415
"learners/setup.md" "c6c79b9031b5ef722ca9d5ff201c13dc" "site/built/setup.md" "2025-11-04"

0 commit comments

Comments
 (0)