You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 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.
94
96
```
95
-
plugins: regtest-2.1.1
97
+
plugins: snaptol-0.0.2
96
98
```
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.
98
100
99
101
```
100
102
collected 3 items
101
103
```
102
104
- This simply tells us that 3 tests have been found and are ready to be run.
103
105
104
106
```
105
-
advanced/test_advanced_calculator.py .
107
+
advanced/test_advanced_calculator.py .
106
108
test_calculator.py .. [100%]
107
109
```
108
110
- 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 `.`.
- This tells us that the 3 tests have passed in 0.01 seconds.
116
118
117
119
### 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.
119
121
120
122
The start is much the same as before:
121
123
122
124
```
123
125
=== test session starts ===
124
-
platform darwin -- Python 3.11.0, pytest-8.1.1, pluggy-1.4.0
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
142
144
___ test_add ___
143
145
def test_add():
144
146
"""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)
148
150
149
-
test_calculator.py:21: AssertionError
151
+
test_calculator.py:7: AssertionError
150
152
```
151
153
152
154
This is where we get detailled information about what exactly broke in the test.
153
155
154
156
- 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`.
158
160
159
161
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.
160
162
161
163
Finally, pytest prints out a short summary of all the failed tests:
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
191
190
E def test_multiply()
192
191
E ^
193
192
E SyntaxError: expected ':'
@@ -221,6 +220,10 @@ Alternatively you can call a specific test using this notation: `pytest test_cal
221
220
222
221
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.
223
222
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
+
224
227
::::::::::::::::::::::::::::::::::::: challenge
225
228
226
229
## Challenge - Experiment with pytest options
@@ -243,12 +246,12 @@ Try running pytest with the above options, editing the code to make the tests fa
243
246
244
247
:::::::::::::::::::::::::::::::::::::::::
245
248
246
-
::::::::::::::::::::::::::::::::::::: keypoints
249
+
::::::::::::::::::::::::::::::::::::: keypoints
247
250
248
251
- You can run multiple tests at once by running `pytest` in the terminal.
249
252
- Pytest searches for tests in files that start or end with 'test' in the current directory and subdirectories.
250
253
- 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.
0 commit comments