Skip to content

Commit 7c87f35

Browse files
authored
refactor: ♻️ minor changes to Tests section (#51)
# Description I looked over #39 and came across a few things I would like to add/change. This PR needs a quick review. ## Checklist - [X] Formatted Markdown - [X] Ran `just run-all`
1 parent fb9e4e4 commit 7c87f35

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

python/tests.qmd

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ or private functionality.
2222
The generally
2323
[accepted](https://softwareengineering.stackexchange.com/questions/380287/why-is-unit-testing-private-methods-considered-as-bad-practice)
2424
"best practice" is to commit tests for public functionality only. We
25-
also agree with this principle, as it helps keep the codebase smaller,
26-
more maintainable, and focused on what the package is designed to do. In
25+
agree with this principle, as it helps keep the codebase smaller, more
26+
maintainable, and focused on what the package is designed to do. In
2727
particular, it allows you as the developer to relatively easily refactor
2828
internal code without having to also update tests for the internal
2929
functionality that you are modifying.
@@ -90,8 +90,9 @@ math/
9090
## How to write tests
9191

9292
Now that you know when and where to write tests, let's look at how to
93-
write them. The tests should be written in Python as functions. When
94-
writing the name for the test, use this general pattern:
93+
write them. The tests should be written in Python as functions.
94+
95+
When writing the name for the test, use this general pattern:
9596

9697
- `test_<function-name>()`, for example `test_adding()`. Use this if
9798
you only need one test for the function.
@@ -129,15 +130,16 @@ def add(a, b):
129130
return a + b
130131
```
131132

132-
And the test for this function might look like:
133+
And a test for this function might look like:
133134

134135
``` {.python filename="tests/test_adding.py"}
135-
import pytest from math.adding
136-
import add
136+
import pytest
137+
from math.adding import add
137138
138139
def test_add_two_numbers():
139-
"""Test adding two numbers together."""
140-
# Given expected = 5
140+
"""Test adding two numbers."""
141+
# Given
142+
expected = 7
141143
# When
142144
actual = add(2, 5)
143145
# Then
@@ -146,19 +148,20 @@ def test_add_two_numbers():
146148
:::
147149

148150
If the tests are short and very simple, you don't have to use these
149-
sections explicitly, meaning you don't necessarily have to use comments.
150-
But as tests get longer and more complex, it can be helpful---for
151-
reviewers and your future self---to include them. The reasoning for
152-
using docstrings in test functions is the same. It can help explain what
153-
the test is doing and why it is important.
151+
sections explicitly, meaning you don't necessarily have to include the
152+
comments `# Given`, `# When`, and `# Then`. But as tests get longer and
153+
more complex, it can be helpful---for reviewers and your future
154+
self---to include them. The reasoning for using docstrings in test
155+
functions is the same. It can help explain what the test is doing and
156+
why it is important.
154157

155158
In general, it's best to keep the tests as simple as possible and only
156159
test one thing at a time. If you need to test multiple things, write
157160
multiple tests.
158161

159162
::: callout-tip
160-
It won't be covered in this guide, but you can avoid repeating yourself
161-
in tests by using [parameterized
163+
We won't cover it in this guide, but you can avoid repeating yourself in
164+
tests by using [parameterized
162165
tests](https://docs.pytest.org/en/7.1.x/how-to/parametrize.html) to test
163166
multiple inputs and outputs with the same test function.
164167
:::
@@ -187,7 +190,7 @@ uv run pytest
187190
uvx pytest
188191
```
189192

190-
::: callout-tip
193+
::: callout-note
191194
Because the package is structured with the `src/` folder, you would
192195
normally need to tell pytest that the package is a "src" package by
193196
using the `--import-mode=importlib` option when running pytest or by

0 commit comments

Comments
 (0)