-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
427 lines (393 loc) · 17 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>pytest in der Praxis</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css">
<link rel="stylesheet" href="css/custom.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Font awesome -->
<script defer src="fontawesome/js/fa-brands.min.js"></script>
<script defer src="fontawesome/js/fa-regular.min.js"></script>
<script defer src="fontawesome/js/fa-solid.min.js"></script>
<script src="fontawesome/js/fontawesome.min.js"></script>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- ##### start slides ##### -->
<section id="start">
<h1>pytest in der Praxis</h1>
</section>
<section data-timing="10" id="author">
<div class="left">
<h2>Author</h2>
<strong>David Zerrenner</strong>
<ul class="fa-ul" style="margin-top: 1em;">
<li><span class="fa-li"><i class="fas fa-envelope"></i></span><a href="mailto:[email protected]">[email protected]</a>
</li>
<li><span class="fa-li"><i class="fab fa-twitter"></i></span><a
href="https://twitter.com/DavidZerrenner">@DavidZerrenner</a></li>
<li><span class="fa-li"><i class="fab fa-github"></i></span><a href="https://github.com/dzerrenner">github.com/dzerrenner</a>
</li>
</ul>
<ul class="fa-ul" style="margin-top: 1em;">
<li><span class="fa-li"><i class="fab fa-github"></i></span><a href="https://dzerrenner.github.io/pytest-talk/slides/">Slides on github</a></li>
</ul>
</div>
<div class="right">
<img src="images/avatar.jpg">
</div>
<aside class="notes">
Notizen
</aside>
</section>
<section id="toc">
<h1>Inhalt</h1>
<ul>
<li><a href="#/pytest">pytest</a></li>
<li><a href="#/assertions">assertions</a> • <a href="#/parameter">Parameter</a></li>
<li><a href="#/parameter">Fixtures</a> • <a href="#/dynamic">dynamische Parameter</a></li>
<li><a href="#/hooks">Hooks</a> • <a href="#/plugins">Plugins</a></li>
</ul>
</section>
<section id="pytest">
<section>
<h1>pytest</h1>
<p>
<span class="fragment fade-up" data-fragment-index="1">python</span>
<span class="fragment fade-up" data-fragment-index="2">• Test</span>
</p>
<p>
<span class="fragment fade-up" data-fragment-index="3"><a href="https://docs.pytest.org/en/latest/">https://docs.pytest.org/en/latest/</a></span>
</p>
<p>
<span class="fragment fade-up" data-fragment-index="3">MIT-Lizenz</span>
</p>
<aside class="notes">
<p>MIT-Lizenz: OK: Commercial use, Modification, Distribution, Private use; Keine Haftung, keine Garantie
</p>
</aside>
</section>
<section>
<h2>Was ist pytest?</h2>
<p class="fragment fade-up" data-fragment-index="1">Test-Framework <span class="fragment"
data-fragment-index="6">zum Erstellen von Test-Frameworks</span>
</p>
<p>
<span class="fragment fade-up" data-fragment-index="2">Assertion introspection</span>
<span class="fragment fade-up" data-fragment-index="3">• Fixtures</span>
<span class="fragment fade-up" data-fragment-index="4">• Plugins</span>
</p>
<p class="fragment fade-up" data-fragment-index="5">
<i class="fab fa-dropbox fa-2x"></i>
<i class="fab fa-firefox fa-2x"></i>
</p>
<aside class="notes">
<p>Testframework:
Nosetests, Unittests, pytest-Testfälle; Framework zum Erstellen von Testframeworks</p>
<p>Features:
<br/> assertion introspection:
Instrumentierung von assert
kein self.assertEqual(), self.assertSonstwas(), nur assert
<br/> Fixtures:
Mächtige Werkzeuge mit dependency injection
<br/> Plugins:
viele 3rd party PLugins
relativ einfach, eigene Plugins zu bauen ⇛ Verweis auf Vorstellung und github</p>
<p>Zielgruppe:
Entwicklerteams, QA Teams, Unabhängige Testeinheiten, Test driven development, ...</p>
<p>Referenzen: Mozilla, Dropbox</p>
</aside>
</section>
</section> <!-- /pytest -->
<section id="assertions">
<section>
<h3>assertions</h3>
<table width="100%">
<thead>
<tr>
<th>pytest</th>
<th>unittest</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<pre><code class="python" data-trim data-noescape>assert True</code></pre>
</td>
<td>
<pre><code class="python" data-trim data-noescape>assertTrue(True)</code></pre>
</td>
</tr>
<tr>
<td>
<pre><code class="python" data-trim data-noescape>assert a == b</code></pre>
</td>
<td>
<pre><code class="python" data-trim data-noescape>assertEqual(a, b)</code></pre>
</td>
</tr>
<tr>
<td>
<pre><code class="python" data-trim data-noescape>assert a <= b</code></pre>
</td>
<td>
<pre><code class="python" data-trim data-noescape>assertLessEqual(a,b)</code></pre>
</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3>assertion instrospection (1)</h3>
<pre><code class="python" data-trim data-noescape>
def test_assertion_set():
> assert {1, 2, 3} == {2, 3, 4}
E assert {1, 2, 3} == {2, 3, 4}
E Extra items in the left set:
E 1
E Extra items in the right set:
E 4
E Full diff:
E - {1, 2, 3}
E + {2, 3, 4}
</code></pre>
</section>
<section>
<h3>assertion instrospection (2)</h3>
<pre><code class="python" data-trim data-noescape>
def test_assertion_dict():
> assert {"a":1, "b": 2} == {"a": 2, "c": 3}
E AssertionError: assert {'a': 1, 'b': 2} == {'a': 2, 'c': 3}
E Differing items:
E {'a': 1} != {'a': 2}
E Left contains more items:
E {'b': 2}
E Right contains more items:
E {'c': 3}
E Full diff:
E - {'a': 1, 'b': 2}
E ? ^ ^ ^
E + {'a': 2, 'c': 3}
E ? ^ ^ ^
</code></pre>
</section>
<section>
<h3>assertion instrospection (3)</h3>
<pre><code class="python" data-trim data-noescape>
def test_assertion_namedtuple():
foo1 = Foo("a", "b", False)
foo2 = Foo("a", 1, False)
> assert foo1 == foo2
E AssertionError: assert Foo(a='a', b='b', c=False)
== Foo(a='a', b=1, c=False)
E At index 1 diff: 'b' != 1
E Full diff:
E - Foo(a='a', b='b', c=False)
E ? ^^^
E + Foo(a='a', b=1, c=False)
E ? ^
</code></pre>
</section>
</section><!-- /assertions -->
<section id="parameter">
<section>
<h3>Parameter • Fixtures</h3>
<pre><code class="python" data-trim data-noescape>
@pytest.mark.parametrize("data", (
(1, "foo", "bar"),
(2, "bla", "blubb")
), ids=["foobar", "blablubb"])
def test_db(testtable, log, data):
log.info("starting test")
cur = testtable.cursor()
insert_stmt = "INSERT INTO testtable VALUES (?, ?, ?)"
select_stmt = "SELECT i, col1, col2 from testtable"
cur.execute(insert_stmt, data)
cur.execute(select_stmt)
row = cur.fetchone()
assert row == data
log.info("test done")
</code></pre>
</section>
<section>
<h4>Fixture: testtable</h4>
<p>Hat Abhängigkeit zu weiterer Fixture</p>
<pre><code class="python" data-trim data-noescape>
@pytest.fixture()
def testtable(db):
create_stmt = "CREATE TABLE testtable " \
"(i INTEGER PRIMARY KEY , col1, col2)"
db.execute(create_stmt)
db.commit()
return db
</code></pre>
</section>
<section>
<h4>Fixture: db</h4>
<p>Setup / Teardown</p>
<pre><code class="python" data-trim data-noescape>
@pytest.fixture()
def db(log):
# setup
logger = log.getChild("db")
conn = sqlite3.connect(":memory:")
conn.set_trace_callback(logger.debug)
# /setup
yield conn
# teardown
conn.close()
# /teardown
</code></pre>
<aside class="notes">
Scopes: "session", "module", "class", "function" (default)
</aside>
</section>
<section>
<h4>Zusammenfassung</h4>
<p><span class="fragment fade-up" data-fragment-index="1"><code>mark.parametrize</code> erzeugt pro Parameterwert eine Testdurchführung</span></p>
<p><span class="fragment fade-up" data-fragment-index="2">Fixtures werden automatisch über mehrere Ebenen aufgelöst (dependency injection)</span></p>
<p><span class="fragment fade-up" data-fragment-index="3">Setup / Teardown in einer Funktion mit <code>yield</code></span></p>
</section>
</section> <!-- /parameter -->
<section id="dynamic">
<section>
<h3>Beispiel: dynamische Parameter</h3>
<p class="fragment fade-up" data-fragment-index="1">
Parameter stehen direkt beim Code der Testfunktion.
</p>
<p class="fragment fade-up" data-fragment-index="2">
Dynamische Parametrisierung kann über den Hook <code>pytest_generate_tests</code> realisiert werden.
</p>
<p class="fragment fade-up" data-fragment-index="3">
Freie Wahl bei der Bereistellung der Parameter (externe Datei, DB, python-Modul, ...).
</p>
<p class="fragment fade-up" data-fragment-index="4">
Freie Wahl bei der Zusammenstellung der Testfälle aus den Parametern (Kombination, Liste, ...).
</p>
</section>
<section>
<p>Testfunktion und Parameter</p>
<pre><code class="python" data-trim data-noescape>
PARAMS = {
"test_cocktail_volume": {
"p1": [3, 2, 1, 4, -1000, ], # 5
"p2": [4, 7, 2, ], # 3 -> 15 test instances
},
}
def pytest_configure(config): # in conftest.py
config.my_external_params = PARAMS
def test_cocktail_volume(p1, p2):
assert calc_volume(p1, p2) > 0
</code></pre>
</section>
<section>
<p>Hook <code>pytest_generate_tests</code></p>
<pre style="width: 95%;"><code style="max-height: 450px;" class="python" data-trim data-noescape>
def pytest_generate_tests(metafunc):
def mark_xfails(values):
# mark test as xfail if one parmeter is less than zero
for v in values:
if v < 0:
yield pytest.param(v, marks=[pytest.mark.xfail(...)])
else:
yield v
test_function_name = metafunc.function.__name__
if test_function_name in metafunc.config.my_external_params:
params =
metafunc.config.my_external_params.get(test_function_name)
for fixture_name, values in params.items():
if fixture_name in metafunc.fixturenames:
metafunc.parametrize(fixture_name, mark_xfails(values))
</code></pre>
</section>
</section>
<section id="hooks">
<h3>Hooks</h3>
<p>siehe</p>
<ul>
<li><code>pytest_collection_modifyitems</code><br/>⇛ Liste der Test-Nodes vor der Durchführung bearbeiten</li>
<li><code>pytest_generate_tests</code><br/>⇛ einzelne Test-Node vor der Durchführung</li>
<li><code>pytest_addoption</code><br/>⇛ eigene Kommandozeilen-Parameter</li>
</ul>
</section> <!-- /hooks -->
<section id="plugins">
<section>
<h3>Plugins</h3>
<p class="fragment fade-up" data-fragment-index="1">Sammlung aus Hooks, Fixtures etc.</p>
<p class="fragment fade-up" data-fragment-index="2">Einbinden über <code>entry_points</code> in setup.py</p>
<p class="fragment fade-up" data-fragment-index="3"><a href="https://docs.pytest.org/en/latest/writing_plugins.html">https://docs.pytest.org/en/latest/writing_plugins.html</a></p>
</section>
<section>
<p>Beispiele:</p>
<ul>
<li><a href="https://github.com/dzerrenner/pytest-report-jinja">pytest-report-jinja</a></li>
<li><a href="https://github.com/dzerrenner/pytest-jsonverdict">pytest-json-verdict</a></li>
</ul>
</section>
</section> <!-- /plugins -->
<section id="demok">
<h3><span style="text-decoration: line-through">Live-Demo</span></h3>
</section> <!-- /hooks -->
<section id="references">
<section>
<h3>References</h3>
<img src="images/bopytest.jpg">
</section>
<section>
<h3>References</h3>
<ul>
<li><a href="https://docs.pytest.org/en/latest/reference.html">pytest Reference</a></li>
<li><a href="http://devork.be/talks/advanced-fixtures/advfix.html">Advanced py.test Fixtures</a></li>
<li><a href="http://pythontesting.net/framework/pytest/pytest-fixtures/">Pythontesting - Pytest fixtures</a></li>
<li><a href="http://www.patricksoftwareblog.com/testing-a-flask-application-using-pytest/">Testing a Flask application with pytest</a></li>
</ul>
</section>
</section> <!-- /references -->
<section id="thanks">
<h1>Danke!</h1>
<i class="fab fa-meetup fa-2x"></i><br/>
<img src="images/qr-code.png">
<p><small><a href="https://dzerrenner.github.io/pytest-talk/slides/">https://dzerrenner.github.io/pytest-talk/slides/</a></small></p>
</section> <!-- /thanks -->
<!-- ##### end slides##### -->
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{src: 'plugin/markdown/marked.js'},
{src: 'plugin/markdown/markdown.js'},
{src: 'plugin/notes/notes.js', async: true},
{
src: 'plugin/highlight/highlight.js', async: true, callback: function () {
hljs.initHighlightingOnLoad();
}
}
]
});
Reveal.configure({
slideNumber: 'h.v',
defaultTiming: 120,
mouseWheel: true
});
</script>
</body>
</html>