Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions episodes/1-intro-to-unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ exercises:

Unit testing is a way of verifying the validity of a code base by testing its smallest individual components, or **units**.

>*"If the parts don't work by themselves, they probably won't work well together"* (Thomas and Hunt, 2019, Topic 51).
>*"If the parts don't work by themselves, they probably won't work well together"*
> -- (Thomas and Hunt, 2019, [The pragmatic programmer](https://search.worldcat.org/search?q=bn:9780135957059), Topic 51).

Several key aspects define a unit test. They should be...

Expand All @@ -37,7 +38,8 @@ Several key aspects define a unit test. They should be...

#### Other forms of testing

There are other forms of testing, such as integration testing in which two or more units of a code base are tested to verify that they work together, or that they are correctly **integrated**. However, today we are focusing on unit tests as it is often the case that many of these larger tests are written using the same test tools and frameworks, hence we will make progress with both by starting with unit testing.
There are other forms of testing, such as integration testing in which two or more units of a code base are tested to verify that they work together, or that they are correctly **integrated**.
However, today we are focusing on unit tests as it is often the case that many of these larger tests are written using the same test tools and frameworks, hence we will make progress with both by starting with unit testing.

::::::::::::::::::::::::::::::::::::::::::::::::

Expand All @@ -59,37 +61,38 @@ All unit tests tend to follow the same pattern of Given-When-Then.

#### Challenge 1: Write a unit test in sudo code.

Assuming you have a function `reverse_array` which reverses the order of an allocated array. Write a unit test in sudo code for `reverse_array`.
Assuming you have a function `reverse_array` which reverses the order of an allocated array. Write a unit test in pseudo code for `reverse_array` using the pattern above.

:::::::::::::::::::::::: solution

```
! Given
Allocate the input array input_array
Fill input_array, for example with (1,2,3,4)
Allocate the expected output array expected_output_array
Fill expected_output_array with the correct expected output, i.e. (4,3,2,1)
Allocate the input array `input_array`
Fill `input_array`, for example with `(1,2,3,4)`
Allocate the expected output array `expected_output_array`
Fill `expected_output_array` with the correct expected output, i.e., `(4,3,2,1)`

! When
Call reverse_array with input_array
Call `reverse_array` with `input_array`

! Then
for each element in input_array:
Assert that the corrosponding element of expected_output_array matches that of input_array
for each element in `input_array`:
Assert that the corresponding element of `expected_output_array` matches that of `input_array`
```

:::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::

### When should unit tests be run?

A major benefit of unit tests is the ability to identify bugs at the earliest possible stage. Therefore, unit tests should be run frequently throughout the development process. Passing unit tests give you and your collaborators confidence that changes to your code are correct and have not broken any existing features, so run your unit tests...
A major benefit of unit tests is the ability to identify bugs at the earliest possible stage. Therefore, unit tests should be run frequently throughout the development process. Passing unit tests give you and your collaborators confidence that changes to your code aren't modifying the previously expected behaviour, so run your unit tests...

- if you make a change locally
- if you raise a merge request
- if you plan to do a release
- if you are reviewing someone else's changes
- if you have recently installed your code into a new environment
- if your dependencies have been updated

Basically, all the time.

Expand Down Expand Up @@ -117,15 +120,15 @@ This is much clearer. We immediately have an idea of what could be going wrong a

### Challenge 2: Unit test bad practices

Take a look at [1-into-to-unit-tests/challenge-2](https://github.com/UCL-ARC/fortran-unit-testing-exercises/episodes/1-into-to-unit-tests/challenge-2) in the exercises repository.
Take a look at [1-into-to-unit-tests/challenge-2](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/1-into-to-unit-tests/challenge-2) in the exercises repository.

:::::::::::::::::::::::::::::::: solution

A solution is provided in [episodes/1-into-to-unit-tests/challenge-2/test/solution](https://github.com/UCL-ARC/fortran-unit-testing-exercises/episodes/1-into-to-unit-tests/challenge-2/test/solution).
A solution is provided in [episodes/1-into-to-unit-tests/challenge-2/test/solution](https://github.com/UCL-ARC/fortran-unit-testing-exercises/tree/main/episodes/1-into-to-unit-tests/challenge-2/test/solution).

:::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::

## References

- David Thomas and Andrew Hunt (2019). The Pragmatic Programmer: your journey to mastery, 20th Anniversary Edition, 2nd Edition. Addison-Wesley Professional.
- David Thomas and Andrew Hunt (2019). [The Pragmatic Programmer: your journey to mastery](https://search.worldcat.org/search?q=bn:9780135957059), 20th Anniversary Edition, 2nd Edition. Addison-Wesley Professional.