Skip to content

Commit dc42872

Browse files
committed
differences for PR #14
1 parent c4a1c5e commit dc42872

File tree

7 files changed

+77
-30
lines changed

7 files changed

+77
-30
lines changed

.DS_Store

-6 KB
Binary file not shown.

03-interacting-with-tests.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ teaching: 10
44
exercises: 2
55
---
66

7-
:::::::::::::::::::::::::::::::::::::: questions
7+
:::::::::::::::::::::::::::::::::::::: questions
88

99
- How do I use pytest to run my tests?
1010
- What does the output of pytest look like and how do I interpret it?
@@ -84,25 +84,27 @@ Let's break down the successful output in more detail.
8484
```
8585
- The first line tells us that pytest has started running tests.
8686
```
87-
platform darwin -- Python 3.11.0, pytest-8.1.1, pluggy-1.4.0
87+
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
88+
8889
```
8990
- The next line just tells us the versions of several packages.
9091
```
91-
rootdir: /Users/sylvi/Documents/GitKraken/python-testing-for-research/episodes/files/03-interacting-with-tests
92+
rootdir: /home/<userid>/.../python-testing-for-research/learners/files/03-interacting-with-tests
93+
9294
```
9395
- The next line tells us where the tests are being searched for. In this case, it is your project directory. So any file that starts or ends with `test` anywhere in this directory will be opened and searched for test functions.
9496
```
95-
plugins: regtest-2.1.1
97+
plugins: snaptol-0.0.2
9698
```
97-
- This tells us what plugins are being used. In my case, I have a plugin called `regtest` that is being used, but you may not. This is fine and you can ignore it.
99+
- This tells us what plugins are being used. In my case, I have a plugin called `snaptol` that is being used, but you may not. This is fine and you can ignore it.
98100

99101
```
100102
collected 3 items
101103
```
102104
- This simply tells us that 3 tests have been found and are ready to be run.
103105

104106
```
105-
advanced/test_advanced_calculator.py .
107+
advanced/test_advanced_calculator.py .
106108
test_calculator.py .. [100%]
107109
```
108110
- These two lines tells us that the tests in `test_calculator.py` and `advanced/test_advanced_calculator.py` have passed. Each `.` means that a test has passed. There are two of them beside `test_calculator.py` because there are two tests in `test_calculator.py` If a test fails, it will show an `F` instead of a `.`.
@@ -115,23 +117,23 @@ test_calculator.py .. [100%]
115117
- This tells us that the 3 tests have passed in 0.01 seconds.
116118

117119
### Case 2: Some or all tests fail
118-
Now let's look at the output when the tests fail. Edit a test in `test_calculator.py` to make it fail (for example switching the `+` in `add` to a `-`), then run `pytest` again.
120+
Now let's look at the output when the tests fail. Edit a test in `test_calculator.py` to make it fail (for example switching a positive number to a negative number), then run `pytest` again.
119121

120122
The start is much the same as before:
121123

122124
```
123125
=== test session starts ===
124-
platform darwin -- Python 3.11.0, pytest-8.1.1, pluggy-1.4.0
125-
rootdir: /Users/sylvi/Documents/GitKraken/python-testing-for-research/episodes/files/03-interacting-with-tests
126-
plugins: regtest-2.1.1
126+
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
127+
rootdir: /home/<userid>/.../python-testing-for-research/learners/files/03-interacting-with-tests
128+
plugins: snaptol-0.0.2
127129
collected 3 items
128130
```
129131

130132
But now we see that the tests have failed:
131133

132134
```
133135
advanced/test_advanced_calculator.py . [ 33%]
134-
test_calculator.py F.
136+
test_calculator.py F.
135137
```
136138

137139
These `F` tells us that a test has failed. The output then tells us which test has failed:
@@ -142,26 +144,26 @@ These `F` tells us that a test has failed. The output then tells us which test h
142144
___ test_add ___
143145
def test_add():
144146
"""Test for the add function"""
145-
> assert add(1, 2) == 3
146-
E assert -1 == 3
147-
E + where -1 = add(1, 2)
147+
> assert add(1, 2) == -3
148+
E assert 3 == -3
149+
E + where 3 = add(1, 2)
148150
149-
test_calculator.py:21: AssertionError
151+
test_calculator.py:7: AssertionError
150152
```
151153

152154
This is where we get detailled information about what exactly broke in the test.
153155

154156
- The `>` chevron points to the line that failed in the test. In this case, the assertion `assert add(1, 2) == 3` failed.
155-
- The following line tells us what the assertion tried to do. In this case, it tried to assert that the number -1 was equal to 3. Which of course it isn't.
156-
- The next line goes into more detail about why it tried to equate -1 to 3. It tells us that -1 is the result of calling `add(1, 2)`.
157-
- The final line tells us where the test failed. In this case, it was on line 21 of `test_calculator.py`.
157+
- The following line tells us what the assertion tried to do. In this case, it tried to assert that the number 3 was equal to -3. Which of course it isn't.
158+
- The next line goes into more detail about why it tried to equate 3 to -3. It tells us that 3 is the result of calling `add(1, 2)`.
159+
- The final line tells us where the test failed. In this case, it was on line 7 of `test_calculator.py`.
158160

159161
Using this detailled output, we can quickly find the exact line that failed and know the inputs that caused the failure. From there, we can examine exactly what went wrong and fix it.
160162

161163
Finally, pytest prints out a short summary of all the failed tests:
162164
```
163165
=== short test summary info ===
164-
FAILED test_calculator.py::test_add - assert -1 == 3
166+
FAILED test_calculator.py::test_add - assert 3 == -3
165167
=== 1 failed, 2 passed in 0.01s ===
166168
```
167169

@@ -177,17 +179,14 @@ For example, if you remove the `:` from the end of the `def test_multiply():` fu
177179

178180
```
179181
=== test session starts ===
180-
platform darwin -- Python 3.11.0, pytest-8.1.1, pluggy-1.4.0
181-
Matplotlib: 3.9.0
182-
Freetype: 2.6.1
183-
rootdir: /Users/sylvi/Documents/GitKraken/python-testing-for-research/episodes/files/03-interacting-with-tests.Rmd
184-
plugins: mpl-0.17.0, regtest-2.1.1
185-
collected 1 item / 1 error
186-
182+
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
183+
rootdir: /home/<userid>/.../python-testing-for-research/learners/files/03-interacting-with-tests
184+
plugins: snaptol-0.0.2
185+
collected 1 item / 1 error
187186
=== ERRORS ===
188187
___ ERROR collecting test_calculator.py ___
189188
...
190-
E File "/Users/sylvi/Documents/GitKraken/python-testing-for-research/episodes/files/03-interacting-with-tests.Rmd/test_calculator.py", line 14
189+
E File "/home/<userid>/.../python-testing-for-research/learners/files/03-interacting-with-tests/test_calculator.py", line 14
191190
E def test_multiply()
192191
E ^
193192
E SyntaxError: expected ':'
@@ -221,6 +220,10 @@ Alternatively you can call a specific test using this notation: `pytest test_cal
221220

222221
If you want to stop running tests after the first failure, you can use the `-x` flag. This will cause pytest to stop running tests after the first failure. This is useful when you have lots of tests that take a while to run.
223222

223+
### Running tests that previously failed
224+
225+
If you don't want to rerun your entire test suite after a single test failure, the `--lf` flag will run only the 'last failed' tests. Alternatively, `--ff` will run the tests that failed first.
226+
224227
::::::::::::::::::::::::::::::::::::: challenge
225228

226229
## Challenge - Experiment with pytest options
@@ -243,12 +246,12 @@ Try running pytest with the above options, editing the code to make the tests fa
243246

244247
:::::::::::::::::::::::::::::::::::::::::
245248

246-
::::::::::::::::::::::::::::::::::::: keypoints
249+
::::::::::::::::::::::::::::::::::::: keypoints
247250

248251
- You can run multiple tests at once by running `pytest` in the terminal.
249252
- Pytest searches for tests in files that start or end with 'test' in the current directory and subdirectories.
250253
- The output of pytest tells you which tests have passed and which have failed and precisely why they failed.
251-
- Flags such as `-v`, `-q`, `-k`, and `-x` can be used to get more detailed output, less detailed output, run specific tests, and stop running tests after the first failure, respectively.
254+
- Pytest accepts many additional flags to change which tests are run, give more detailed output, etc.
252255

253256
::::::::::::::::::::::::::::::::::::::::::::::::
254257

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def power(value, exponent):
2+
"""Raise a value to an exponent"""
3+
result = value
4+
for _ in range(exponent - 1):
5+
result *= value
6+
return result
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from advanced_calculator import power
2+
3+
4+
def test_power():
5+
"""Test for the power function"""
6+
assert power(2, 3) == 8
7+
assert power(3, 3) == 27
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def add(a, b):
2+
"""Add two numbers"""
3+
return a + b
4+
5+
6+
def multiply(a, b):
7+
"""Multiply two numbers"""
8+
return a * b
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from calculator import add, multiply
2+
3+
4+
def test_add():
5+
"""Test for the add function"""
6+
# Check that positive numbers work
7+
assert add(1, 2) == 3
8+
# Check that negative numbers work
9+
assert add(-1, -2) == -3
10+
# Check that adding 0 works
11+
assert add(0, 4) == 4
12+
13+
14+
def test_multiply():
15+
"""Test for the multiply function"""
16+
# Check that positive numbers work
17+
assert multiply(5, 5) == 25
18+
# Check that multiplying by 1 works
19+
assert multiply(1, 5) == 5
20+
# Check that multiplying by 0 works
21+
assert multiply(0, 3) == 0
22+
# Check that negative numbers work
23+
assert multiply(-5, 2) == -10

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"episodes/00-introduction.Rmd" "9703fac71e1762903d67487cbe831bd6" "site/built/00-introduction.md" "2026-02-16"
88
"episodes/01-why-test-my-code.Rmd" "9402c4990ea2c28712f4efc01e6e4467" "site/built/01-why-test-my-code.md" "2026-02-16"
99
"episodes/02-simple-tests.Rmd" "b989833fdca9f1ac6176e4c978d6f7d9" "site/built/02-simple-tests.md" "2026-02-16"
10-
"episodes/03-interacting-with-tests.Rmd" "6270b5c5fef1e472e3661f062257d5f3" "site/built/03-interacting-with-tests.md" "2026-02-16"
10+
"episodes/03-interacting-with-tests.Rmd" "0d519e72cfa9e07cd8ef7aa14d47d363" "site/built/03-interacting-with-tests.md" "2026-02-16"
1111
"episodes/04-unit-tests-best-practices.Rmd" "2dc31f9de45c52ccea175d82778e770e" "site/built/04-unit-tests-best-practices.md" "2026-02-16"
1212
"episodes/05-testing-exceptions.Rmd" "763eaac6c3905bc4444d4ff179d3f4bb" "site/built/05-testing-exceptions.md" "2026-02-16"
1313
"episodes/06-floating-point-data.Rmd" "07a47665f7417581c5f08b02f0cc0003" "site/built/06-floating-point-data.md" "2026-02-16"

0 commit comments

Comments
 (0)