Skip to content

Commit 1fe8c4a

Browse files
committed
Add explanation of mocking and stubbing
1 parent b008ff4 commit 1fe8c4a

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

testing/README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,39 @@ Compilers tested: *A list of compilers we have tried with these tests*
3838
| Feature | Implemented natively | Implemented manually |
3939
|---------|----------------------|----------------------|
4040
| Can run individual tests | Yes or No (explanation) | Yes or No (explanation) |
41-
| Mocking | Yes or No (explanation) | Yes or No (explanation) |
42-
| Stubbing | Yes or No (explanation) | Yes or No (explanation) |
41+
| Mocking/Stubbing | Yes or No (explanation) | Yes or No (explanation) |
4342
| Data driven tests | Yes or No (explanation) | Yes or No (explanation) |
4443
| Coverage report | Yes or No (explanation) | Yes or No (explanation) |
4544
| Skip tests | Yes or No (explanation) | Yes or No (explanation) |
45+
46+
### Explanations
47+
48+
**Mocking**
49+
50+
There are many good explanations of what mocking is, [here is one](https://www.hypertest.co/unit-testing/unit-test-mocking) (this is summarised below)
51+
52+
**What is Mocking?**
53+
54+
Mocking is a technique used in unit testing to replace real objects with mock objects. These mock objects simulate the behaviour of real objects, allowing the test to focus on the functionality of the unit being tested. Mocking is particularly useful when the real objects are complex, slow, or have undesirable side effects (e.g., making network requests, accessing a database, or depending on external services).
55+
56+
**Why Use Mocking?**
57+
58+
- *Isolation:* By mocking dependencies, you can test units in isolation without interference from other parts of the system.
59+
60+
- *Speed:* Mocking eliminates the need for slow operations such as database access or network calls, making tests faster.
61+
62+
- *Control:* Mock objects can be configured to return specific values or throw exceptions, allowing you to test different scenarios and edge cases.
63+
64+
- *Reliability:* Tests become more predictable as they don't depend on external systems that might be unreliable or unavailable.
65+
66+
**Stubbing**
67+
68+
Stubbing is very similar to mocking. The concept of stubbing is to have well-defined responses to procedure calls within a test which do not require calls to actual src code. For example, if we are want to test `functionAB` which relies on some data which is usually returned from `functionA` which is compute intensive, we can essentially hard-code the response from `functionA` to speed up the test.
69+
```f90
70+
call stubbedFunctionA(data) ! retrieve the hard coded data values from a stub
71+
call functionB(data, output) ! Use these hard coded values in out procedure being tested
72+
! Verify out output
73+
```
74+
This may seem overkill in the above example, but it becomes more important if we pass `functionA` into `functionB` and wanted to ensure this was stubbed.
75+
76+
The difference compared to mocking is that stubbing will not track calls to the mocked procedure/function/object whereas mocking will.

testing/pFUnit/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Compilers tested: GNU Fortran (Homebrew GCC 14.2.0_1)
1818
| Feature | Implemented natively | Implemented manually |
1919
|---------|----------------------|----------------------|
2020
| Can run individual tests | Yes (by directly calling the test executable, not ctest, we can pass `-f` to filter tests by name) | N/A |
21-
| Mocking | No | No |
22-
| Stubbing | No | No |
21+
| Mocking/Stubbing | No | No |
2322
| Data driven tests | Yes (see [test_calculate_mesh_parameters.pf](./test_calculate_mesh_parameters.pf)) | N/A |
2423
| Coverage report | Yes or No (explanation) | Yes or No (explanation) |
2524
| Skip tests | Partially, but there is no logging of skipped tests (add required pre-compile flag e.g. `@Test(#ifdef NO_SKIP)`) | Partially (comment `@Test` annotation) |

testing/test-drive/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ Compilers tested: GNU Fortran (Homebrew GCC 14.2.0_1)
3030
| Feature | Implemented natively | Implemented manually |
3131
|---------|----------------------|----------------------|
3232
| Can run individual tests | No | Yes, see [main.f90](./main.f90). However, this requires running the test executable directly without ctest. |
33-
| Mocking | No | Not implemented |
34-
| Stubbing | No | Not implemented |
33+
| Mocking/Stubbing | No | Not implemented |
3534
| Data driven tests | No | Yes, but this is very cumbersome. See `verify_calculate_mesh_parameters` and `verify_calculate_mesh` in [test_mesh_generator.f90](./test_mesh_generator.f90)
3635
| Coverage report | Yes, with fpm | N/A |
3736
| Skip tests | Yes, see `test_skip_example` in [test_mesh_generator.f90](./test_mesh_generator.f90) | N/A |

testing/veggies/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ Compilers tested: GNU Fortran (Homebrew GCC 14.2.0_1)
1616
| Feature | Implemented natively | Implemented manually |
1717
|---------|----------------------|----------------------|
1818
| Can run individual tests | Yes ( fpm test *"fpm test name"* [-- -f *"regex for test description"*] ) | N/A |
19-
| Mocking | No | No |
20-
| Stubbing | No | No |
19+
| Mocking/Stubbing | No | No |
2120
| Data driven tests | Yes (via defining custom types which extend `input_t` type) | N/A |
2221
| Coverage report | No | No |
2322
| Skip tests | No | Sort of (return a generic pass with a description indicating test was skipped) |

0 commit comments

Comments
 (0)