@@ -117,7 +117,7 @@ Most modern programming languages have good options to streamline testing
117
117
118
118
## Testing metrics
119
119
120
- #### Targets are arbitrary and indicative of but do not guarantee strong testing.
120
+ #### Targets are arbitrary and indicative
121
121
122
122
<div style =" display : flex ; justify-content : center ; align-items : center " >
123
123
<div >
@@ -141,178 +141,115 @@ Most modern programming languages have good options to streamline testing
141
141
<img src =" ./media/testing/metrics.jpg " width =" 300 " style =" margin-left : 60px " >
142
142
</div >
143
143
144
-
145
-
146
- ===
147
-
148
- <!-- .slide: data-state="standard" -->
149
-
150
- ## Misleading metrics
151
-
152
- Consider this function...
153
- ``` python
154
- def fahrenheit_to_celsius (temp_list , converted_temps = []):
155
- for temp in temp_list:
156
- temp_c = (temp - 32.0 ) * (5.0 / 9.0 )
157
- converted_temps.append(temp_c)
158
- return converted_temps
159
- ```
160
-
161
- ...and these tests with 100% coverage:
162
- <!-- .element: class="fragment" data-fragment-index="2" -->
163
- ``` python
164
- assert fahrenheit_to_celsius([32.0 , 77.0 ]) == [0.0 , 25.0 ]
165
- assert (fahrenheit_to_celsius([100 ], converted_temps = [0.0 , 25.0 ])
166
- == [0.0 , 25.0 , 37.77777777777778 ])
167
- ```
168
- <!-- .element: class="fragment" data-fragment-index="2" -->
169
-
170
- Q: Can you think of a problem that will not be caught?
171
- <!-- .element: class="fragment" data-fragment-index="3" -->
172
-
173
144
===
174
145
175
146
<!-- .slide: data-state="standard" -->
176
147
148
+ ## Write Code
177
149
178
- ## Writing a test file
179
-
180
- Create a file called test_example.py ...
181
150
<pre ><code class =" bash " style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers >
182
151
$ mkdir pytest-example
183
152
$ cd pytest-example
184
- $ nano test_example.py
185
153
</code ></pre >
186
154
187
155
<div class =" fragment " >
188
- ... and add the function and tests from before:
156
+ Creating a file < code >example.py</ code > containing
189
157
<pre ><code class =" python " style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers >
190
- def fahrenheit_to_celsius(temp_list, converted_temps=[]):
191
- for temp in temp_list:
192
- temp_c = (temp - 32.0) * (5.0/9.0)
193
- converted_temps.append(temp_c)
194
- return converted_temps
158
+ def add(a, b):
159
+ return a + b
195
160
  ;
196
161
  ;
197
- def test_convert(): # Special name!
198
- assert fahrenheit_to_celsius([32.0, 77.0]) == [0.0, 25.0]
199
- assert (fahrenheit_to_celsius([100], converted_temps = [0.0, 25.0])
200
- == [0.0, 25.0, 37.77777777777778])
162
+ def test_add(): # Special name!
163
+ assert add(2, 3) == 5 # What's `assert`? 🤔
164
+ assert add('space', 'ship') == 'spaceship'
201
165
</code ></pre >
202
166
</div >
203
167
204
- ===
168
+ <div class =" fragment " >
169
+ Chat with the python shell about <code >assert</code > ...
170
+ </div >
171
+ <div class =" fragment " >
172
+ <pre ><code class =" python " style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers >
173
+ >>> assert 1==1 # passes
174
+ >>> assert 1==2 # throws error
175
+ Traceback (most recent call last):
176
+ File "< ; stdin> ; ", line 1, in < ; module> ;
177
+ AssertionError
178
+ </code ></pre >
205
179
206
- <!-- .slide: data-state="standard" -- >
180
+ </ div >
207
181
208
- ## Running Pytest
182
+ ===
209
183
210
- The ` pytest ` command will run all functions starting with "test _ " from all files starting with "test _ ".
184
+ <!-- .slide: data-state="standard" -->
211
185
186
+ ## Test!
212
187
213
- <pre class = " fragment " ><code style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers =" 1|1-9 " >
214
- $ pytest
188
+ <pre ><code style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers =" 1|1-9 " >
189
+ $ pytest example.py
215
190
======================== test session starts ========================
216
191
platform linux -- Python 3.6.9, pytest-7.0.1, pluggy-1.0.0
217
192
rootdir: /home/ole/Desktop/pytest-texample
218
193
collected 1 item
219
194
220
- test_example .py . [100%]
195
+ example .py . [100%]
221
196
222
197
========================= 1 passed in 0.00s =========================
223
198
</code ></pre >
224
199
225
-
226
200
===
227
201
228
202
<!-- .slide: data-state="standard" -->
229
203
230
- ## Pytest Exercise
231
-
232
- 1 . Add a test to catch "problematic" behavior of the function.
233
- - don't fix the function (yet)
234
- 2 . Run pytest to see what the response looks like.
235
- - what information can you gather from the response?
236
- 3 . Fix the function and rerun the test
204
+ ## Breaking Things
237
205
238
- <div >
239
206
<pre ><code class =" python " style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers >
240
- def fahrenheit_to_celsius(temp_list, converted_temps=[]):
241
- for temp in temp_list:
242
- temp_c = (temp - 32.0) * (5.0/9.0)
243
- converted_temps.append(temp_c)
244
- return converted_temps
245
-   ;
246
-   ;
247
- def test_convert():
248
- assert fahrenheit_to_celsius([32.0, 77.0]) == [0.0, 25.0]
249
- assert (fahrenheit_to_celsius([100], converted_temps = [0.0, 25.0])
250
- == [0.0, 25.0, 37.77777777777778])
251
- </code ></pre >
252
- </div >
253
-
254
- ===
255
-
256
- <!-- .slide: data-state="standard" -->
207
+ def add(a, b):
208
+ return a - b # Uh oh, mistake! 😱
257
209
258
- ## Breaking the test
259
210
260
- 1 . Add a test to catch problematic behavior of the function.
261
-
262
- <div class =" fragment " >
263
- <pre ><code class =" python " style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers =" 1-11|12 " >
264
- def fahrenheit_to_celsius(temp_list, converted_temps=[]):
265
- for temp in temp_list:
266
- temp_c = (temp - 32.0) * (5.0/9.0)
267
- converted_temps.append(temp_c)
268
- return converted_temps
269
-   ;
270
-   ;
271
- def test_convert():
272
- assert fahrenheit_to_celsius([32.0, 77.0]) == [0.0, 25.0]
273
- assert (fahrenheit_to_celsius([100], converted_temps = [0.0, 25.0])
274
- == [0.0, 25.0, 37.77777777777778])
275
- assert fahrenheit_to_celsius([32.0, 77.0]) == [0.0, 25.0]
211
+ def test_add():
212
+ assert add(2, 3) == 5
213
+ assert add('space', 'ship') == 'spaceship'
276
214
</code ></pre >
277
- </div >
278
215
279
216
===
280
217
281
218
<!-- .slide: data-state="standard" -->
282
219
283
- ## Failing tests
220
+ ## Testing Again
284
221
285
- <pre style = " width : 100 % ; max-width : 1200 px ; overflow : auto ; " ><code style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers =" 1|2-8|9-22|23-26 " >
222
+ <pre ><code style =" overflow : hidden ;" data-trim class =" bash " data-line-numbers =" 1|2-8|9-17|18-20 " >
286
223
$ pytest example.py
287
224
======================== test session starts =========================
288
225
platform linux -- Python 3.6.9, pytest-7.0.1, pluggy-1.0.0
289
226
rootdir: /home/ole/Desktop/pytest-texample
290
227
collected 1 item
291
228
292
- test_example .py F [100%]
229
+ example .py F [100%]
293
230
294
231
============================== FAILURES ==============================
295
- ______________________________ test_convert ______________________________
296
-
297
- def test_convert(): # Special name!
298
- assert fahrenheit_to_celsius([32.0, 77.0]) == [0.0, 25.0]
299
- assert (fahrenheit_to_celsius([100], converted_temps = [0.0, 25.0])
300
- == [0.0, 25.0, 37.77777777777778])
301
- > assert fahrenheit_to_celsius([32.0, 77.0]) == [0.0, 25.0]
302
- E assert [0.0, 25.0, 0.0, 25.0] == [0.0, 25.0]
303
- E
304
- E Left contains 2 more items, first extra item: 0.0
305
- E Use -v to get more diff
306
-
307
- test_example.py:11: AssertionError
308
- ============================= short test summary info =============================
309
- FAILED test_example.py::test_convert - assert [0.0, 25.0, 0.0, 25.0] == [0.0, 25.0]
310
- ================================ 1 failed in 0.67s ================================
311
-   ;
232
+ ______________________________ test_add ______________________________
233
+
234
+ def test_add():
235
+ > assert add(2, 3) == 5
236
+ E assert -1 == 5
237
+ E + where -1 = add(2, 3)
238
+
239
+ example.py:6: AssertionError
240
+ ====================== short test summary info =======================
241
+ FAILED example.py::test_add - assert -1 == 5
242
+ ========================= 1 failed in 0.05s ==========================
312
243
</code ></pre >
313
244
245
+ <ul >
246
+ <li class =" fragment " >🚀❓<span class =" fragment " >Functions fail on first error</span ></li >
247
+ <li class =" fragment " >But all test functions are executed</li >
248
+ </ul >
249
+
314
250
===
315
251
252
+
316
253
<!-- .slide: data-state="standard" -->
317
254
318
255
## Pytest wrap up
0 commit comments