Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
319d791
Vocabulary from motivation to quick reference
mmesiti Sep 11, 2026
03bf5d9
Fix "what can test help you do?" section
mmesiti Sep 11, 2026
bdbe948
moved recommendations to the conclusions
mmesiti Sep 11, 2026
b13270d
Add comment on self-hosting
mmesiti Sep 17, 2026
afad9c8
Small rewording fixes
mmesiti Sep 19, 2026
e68959f
Completely remove "where to start" from motivation
mmesiti Sep 19, 2026
4fd8d0d
Elaborate on runner self-hosting
mmesiti Sep 19, 2026
17a80d2
Motivations: small reformatting for readability
mmesiti Sep 19, 2026
52aae85
fix typo
mmesiti Sep 19, 2026
5cfed98
Use epigraph directive for quote in motivations
mmesiti Sep 19, 2026
598f314
Motivations: refactor + frameworks
mmesiti Sep 19, 2026
f5d4062
Fixes from Anja's review
mmesiti Sep 22, 2026
6a2db30
move "why use a testing framework" to conclusions
mmesiti Sep 22, 2026
5a068cf
mention e2e and unit test in examples in motivations
mmesiti Sep 22, 2026
3189516
Some addition to conclusions and quick reference
mmesiti Sep 22, 2026
04abe9a
modularity <-> testability in conclusions
mmesiti Sep 22, 2026
d481962
Lots of fixes by Anja
mmesiti Sep 23, 2026
c70cdf9
further sphinx warning fixes
mmesiti Sep 23, 2026
906dea2
(minor) terser / better wording
mmesiti Sep 23, 2026
77f7b50
conclusions: Improve wording on knowledge needed
mmesiti Sep 23, 2026
084c7a1
conclusions: test framework feat table col fixes
mmesiti Sep 23, 2026
a187734
Mention the test pyramid
mmesiti Sep 24, 2026
ce9039a
Split 'Exercise' in local testing in 2 sections
mmesiti Sep 24, 2026
dfb8fb7
local testing: false negative -> false positive
mmesiti Sep 24, 2026
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
183 changes: 159 additions & 24 deletions content/conclusions.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,174 @@
# Conclusions and recommendations

## The basics
```{objectives}
- What is a realistic approach to automated test?
- How can I start?
```

- Learn one test framework well enough for basics
- Explore and use the good tools that exist out there
- An incomplete list of testing frameworks can be found in the [Quick Reference](quick-reference)
- Start with some basics
- Some simple thing that test all parts
- Automate tests
- Faster feedback and reduce the number of surprises
---

## Discussion: What's easy and hard to test?

## Going more in-depth
```{discussion} Discussion: Testing in practice

Use the collaborative notes to answer these questions:

1. Give examples of things (from your work) that are easy to test.
2. Give examples of things (from your work) that are hard to test.
```

---

Considering automated tests when writing code
is a major mental shift, that you will hopefully embrace
after attending this lesson.

## The basics: what knowledge do you need?

Learn one {term}`testing framework` well enough for basics:
- Explore and use the good tools that exist out there.
- An incomplete list of testing frameworks
can be found [here](#unit-test-frameworks).

### Why use a testing framework?

Automated testing typically involves a number of repetitive tasks
and tricky problem solving.

Fortunately for us,
someone has already found a solution for most of these
and created {term}`testing framework`s that we can use.

Note: not all frameworks solve all problems
(also because sometimes the underlying language does not have the necessary features).
Comment on lines +41 to +43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this note below the table (now we are giving a note about problems before we present the problems in the table)

@mmesiti mmesiti Sep 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue against this just because this table is not meant to be discussed in detail, it's more there as a collection of reasons why one should use a testing framework. The message "... but not all frameworks tackle all the problems" is perhaps more important than the details contained in the table.

Perhaps, we should move the whole table to an appendix (or just the "quick-reference" episode, which isn't that quick anymore), put a link here, and go through it if permits? Not sure about that though.


```{list-table} Why use a testing framework?
:widths: 40 40 20
* - **Typical problem**
- **Solution**
- **Examples**
* - Report failures/successes
consistently
(to humans or other machines)
- Automated collection and output,
(e.g., Junit XML format or [TAP](https://en.wikipedia.org/wiki/Test_Anything_Protocol))
- Fundamental feature that
all testing frameworks have
* - Remember to run
all the tests you write
in the main test script/program
- Automatic discovery,
automatic {term}`registration<test registration>`
when declaring test functions
- [pytest](https://docs.pytest.org/en/stable/)
* - Run only some tests
(to save time)
- Test filtering
via patterns
- [pytest](https://docs.pytest.org/en/stable/how-to/usage.html#specifying-which-tests-to-run)
* - Provide useful information
on why a test has failed
- "Smart" assertions/macros
- [pytest](https://docs.pytest.org/en/stable/how-to/assert.html#assert),
[GoogleTest](https://google.github.io/googletest/primer.html#assertions)
* - Run same test for many
known input/output combinations
- Parametric tests
- [pytest](https://docs.pytest.org/en/stable/how-to/parametrize.html#pytest-mark-parametrize-parametrizing-test-functions),
[Julia](https://docs.julialang.org/en/v1/stdlib/Test/#Working-with-Test-Sets) (see `testset for`)
* - Check that a property holds
for a class of inputs and outputs
- Automatically generate test cases
based on a strategy
(property testing)
- [hypothesis](https://hypothesis.readthedocs.io/en/latest/tutorial/introduction.html)
(Python)
* - Debugging on failure
- Start debugger on test failure
- `pytest --pdb`
* - Floating point equalities
with tolerance
- Macros/classes
- `≈` (Julia), `pytest.approx`
* - Set up and tear down
of complex test cases
- {term}`Fixture`s
- [pytest](https://docs.pytest.org/en/stable/explanation/fixtures.html),
[GoogleTest](https://google.github.io/googletest/primer.html#same-data-multiple-tests)
* - Estimate how much of your code
is *run* in the test suite
- Automatic {term}`coverage<code coverage>`
measurement
- [Pytest-cov](https://pytest-cov.readthedocs.io/en/latest/),
[gcov/lcov](https://wiki.cs.jmu.edu/reference/gcov/)
(for C/C++/Fortran)
* - Do code examples
in documentation
work as expected?
- Documentation tests
(doctests)
- [Python](https://docs.python.org/3/library/doctest.html),
[Julia](https://documenter.juliadocs.org/stable/man/doctests/),
[R](https://cran.r-project.org/web/packages/doctest/vignettes/doctest.html)
* - Will my code work
with different versions
of the dependencies?
- Test in different environments
- [tox](https://tox.wiki/en), [Nox](https://nox.thea.codes/en/stable/index.html)
(Python)
```

- Strike a healthy balance between unit tests and integration tests
- As the code gets larger and the chance of undetected bugs
increases, tests should increase
- When adding new functionality, also add tests
- When you discover and fix a bug, also commit a test against this bug
- Use code coverage analysis to identify untested or unused code
- If you make your code easier to test, it becomes more modular

## Don't over-test

## Ways to get started
- Not every code needs perfect {term}`test coverage<code coverage>`.
- A simple script or notebook probably does not need an automated test.

## Pick the low-hanging fruits first

You probably won't do everything perfectly when you start off... But
what are some of the easy starting points?

- Do you have some single functions that are easy to test, but hard to
verify just by looking at them? Add unit tests.
**If you have got nothing yet**:
1. Start with an end-to-end test.
Typically easy to add, from a "manual" use case.
This should match (or serve as) an **example in the code documentation** anyway.
- Describe in words how *you* check whether the code still works.
- Translate the words into a script.
- Run the script as often as reasonable.

- Do you have data analysis or simulation of some sort? Make an
end-to-end test with sample data, or sample parameters. This is
useful as an example anyway.
2. Do you have some single functions that are easy to test, but hard to
verify just by looking at them? Add unit tests.

- A local testing framework + GitHub actions is very easy! And works
well in the background - you do whatever you want and get an email
3. A local testing framework + GitHub actions/Gitlab CI-CD is very easy!
And works well in the background - you do whatever you want and get an email
if you break things. It's actually pretty freeing.

**If you need to start modifying some existing code:**
1. Add a {term}`characterization test` for the part of the code you need to change.
2. Add tests for any functionality you intend to add.
3. Consider adding some end-to-end tests for the use case you have in mind.

## Going more in-depth

**With time**:
- The code gets larger,
the chance of undetected bugs increases,
tests should increase.
- Bugs will be found. When you find them, add tests against those.


**How to improve your code:**
- Use {term}`code coverage` analysis to identify untested or unused code.
Remember [Goodhart's Law](https://en.wikipedia.org/wiki/Goodhart%27s_law).
- Strike a healthy balance between different kinds of tests:
- Fast tests give you information quicker but can be shallow;
- Thorough tests can catch more bugs but take longer to run
and can be brittle.

The {term}`test pyramid`
is a recommended strategy to balance between test types.

- If you make your code easier to test, it becomes more modular (and vice versa - see the [modular code development lesson](https://coderefinery.github.io/modular-type-along/)).
- **Learning how to test well make the rest of your code better, too.**

12 changes: 10 additions & 2 deletions content/locally.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```


## Exercise
## Setting up your first automated test


In this exercise we will make a simple function and use
Expand Down Expand Up @@ -267,9 +267,17 @@ whether our test detects the change:
```````
`````````

## Numerical Tolerances

Some times the testing logic needs to be slightly more complicated.
In scientific computing
many functions return floating point numbers:
how do we test them?


`````````{challenge} (optional) Local-2: Create a test that considers numerical tolerance (10 min)
Let's see an example where the test has to be more clever in order to
avoid false negative.
avoid false positive.

In the above exercise we have compared integers. In this optional exercise we
want to learn how to compare floating point numbers since they are more tricky
Expand Down
Loading
Loading