11.. include :: include.rst
22
3- *****************************************************************************
4- Session Six: Testing, Advanced Argument Passing, lambda, functions as objects
5- *****************************************************************************
3+ ***********************************************
4+ Session Six: Testing, Advanced Argument Passing
5+ ***********************************************
66
77======================
88Lightning Talks Today:
@@ -86,18 +86,20 @@ if you need to do along string literal, sometimes a triple quoted string is perf
8686 so having the line endings automatic is great.
8787 """
8888
89- But yo don't always want the line endings quite like that. And you may not want all that whitespace when fitting it into indented code.
89+ But you don't always want the line endings quite like that. And you may not want all that whitespace when fitting it into indented code.
9090
91- It turns out that when you put a multiple strings together with no commas or anythign in between -- pyhton will join them: :
91+ It turns out that when you put a multiple strings together with no commas or anythign in between -- python will join them:
9292
9393.. code-block :: ipython
9494
9595 In [81]: "this is " "a string " "built up of parts"
9696 Out[81]: 'this is a string built up of parts'
9797
98- If it's parentheses, you can put the next chunk on the next line :
98+ .. nextslide : :
9999
100- .. code-block :: ipython
100+ If it's in parentheses, you can put the next chunk on the next line:
101+
102+ .. code-block :: python
101103
102104 print (" {} is from {} , and he likes "
103105 " {} cake, {} fruit, {} salad, "
@@ -111,7 +113,7 @@ If it's parentheses, you can put the next chunk on the next line:
111113 pretty print
112114------------
113115
114- if you need to print our nested (or large) data structure in a more readable fashion, the "pretty print" module is handy:
116+ If you need to print our nested (or large) data structure in a more readable fashion, the "pretty print" module is handy:
115117
116118.. code-block :: ipython
117119
@@ -128,6 +130,22 @@ if you need to print our nested (or large) data structure in a more readable fas
128130 'pasta': 'lasagna',
129131 'salad': 'greek'}
130132
133+ Exceptions
134+ ----------
135+
136+ Adding stuff to an Exception:
137+
138+ Example from slack
139+
140+
141+ Anything else?
142+ --------------
143+
144+ .. rst-class :: center medium
145+
146+ Anything else you want me to go over?
147+
148+
131149Lightning Talks
132150----------------
133151
@@ -146,14 +164,14 @@ Testing
146164.. rst-class :: build left
147165.. container ::
148166
149- You've already seen some a very basic testing strategy.
167+ You've already seen a very basic testing strategy.
150168
151169 You've written some tests using that strategy.
152170
153171 These tests were pretty basic, and a bit awkward in places (testing error
154172 conditions in particular).
155173
156- .. rst-class :: centered
174+ .. rst-class :: centered large
157175
158176 **It gets better **
159177
@@ -283,7 +301,9 @@ There are several other options for running tests in Python.
283301
284302* ... (many frameworks supply their own test runners: e.g. django)
285303
286- Both are very capable and widely used. I have a personal preference for pytest -- so we'll use it for this class
304+ Both are very capable and widely used. I have a personal preference for pytest
305+
306+ -- so we'll use it for this class
287307
288308Installing ``pytest ``
289309---------------------
@@ -313,13 +333,17 @@ Pre-existing Tests
313333
314334Let's take a look at some examples.
315335
316- ``IntroPython2016\Examples\Session06 ``
336+ in ``IntroPython2016\Examples\Session06 ``
337+
338+ .. code-block :: bash
317339
318- `` $ py.test``
340+ $ py.test
319341
320342 You can also run py.test on a particular test file:
321343
322- ``py.test test_cigar_party.py ``
344+ .. code-block :: bash
345+
346+ $ py.test test_random_unitest.py
323347
324348 The results you should have seen when you ran ``py.test `` above come
325349partly from these files.
@@ -328,68 +352,90 @@ Let's take a few minutes to look these files over.
328352
329353[demo]
330354
331- .. nextslide :: What's Happening Here.
355+ What's Happening Here.
356+ ----------------------
332357
333358When you run the ``py.test `` command, ``pytest `` starts in your current
334359working directory and searches the filesystem for things that might be tests.
335360
336361It follows some simple rules:
337362
338- .. rst-class :: build
339-
340363* Any python file that starts with ``test_ `` or ``_test `` is imported.
364+
341365* Any functions in them that start with ``test_ `` are run as tests.
366+
342367* Any classes that start with ``Test `` are treated similarly, with methods that begin with ``test_ `` treated as tests.
343368
344- ( don't worry about "classes" just yet ;-) )
369+ ( don't worry about "classes" part just yet ;-) )
345370
346- .. nextslide :: pytest
371+ pytest
372+ ------
347373
348374This test running framework is simple, flexible and configurable.
349375
350376Read the documentation for more information:
351377
352378http://pytest.org/latest/getting-started.html#getstarted
353379
380+ It will run ``unittest `` tests for you.
381+
382+ But in addition to finding and running tests, it makes writting tests simple, and provides a bunch of nifty utilities to support more complex testing.
383+
384+
354385Test Driven Development
355386-----------------------
356387in the Examples dir, try::
357388
358389 $ py.test test_cigar_party
359390
360- What we've just done here is the first step in what is called **Test Driven
361- Development **.
391+ What we've just done here is the first step in what is called:
392+
393+ .. rst-class :: centered
394+
395+ **Test Driven Development **.
362396
363397A bunch of tests exist, but the code to make them pass does not yet exist.
364398
365- The red you see in the terminal when we run our tests is a goad to us to write
366- the code that fixes these tests.
399+ The red you see in the terminal when we run our tests is a goad to us to write the code that fixes these tests.
367400
368401Let's do that next!
369402
370- ============================
371403Test Driven development demo
372- ============================
404+ -----------------------------
405+
406+ Open up:
407+
408+ ``Examples/Session06/test_cigar_party.py ``
409+
410+ and:
373411
412+ ``Examples/Session06/cigar_party.py ``
374413
375- In `` Examples/Session06/test_cigar_party.py ``
414+ and run::
376415
416+ $ py.teset test_cigar_party.py
417+
418+ Now go in to ``cigar_party.py `` and let's fix the tests.
419+
420+ Let's play with codingbat.py also...
377421
378422===
379423LAB
380424===
381425
382- Pick an example from codingbat:
426+ .. rst-class :: left
427+
428+ Pick an example from codingbat:
383429
384- ``http://codingbat.com ``
430+ ``http://codingbat.com ``
385431
386- Do a bit of test-driven development on it:
432+ Do a bit of test-driven development on it:
387433
388- * run something on the web site.
389- * write a few tests using the examples from the site.
390- * then write the function, and fix it 'till it passes the tests.
434+ * run something on the web site.
435+ * write a few tests using the examples from the site.
436+ * then write the function, and fix it 'till it passes the tests.
391437
392- Do at least two of these...
438+ Do at least two of these...
393439
394440Lightning Talk
395441--------------
@@ -473,7 +519,7 @@ This is a **very** important point -- I will repeat it!
473519Function arguments in variables
474520-------------------------------
475521
476- function arguments are really just
522+ When a function is called, its arguments are really just:
477523
478524* a tuple (positional arguments)
479525* a dict (keyword arguments)
@@ -758,21 +804,24 @@ You get a new list every time the function is called
758804Homework
759805========
760806
761- Finish up the Labs
807+ .. rst-class :: left
762808
763- Material to review for next week
764- --------------------------------
809+ Finish up the Labs
765810
766- Lambda:
811+ Write a complete set of unit tests for your mailroom program.
767812
768- http://www.blog.pythonlibrary.org/2015/10/28/python-101-lambda-basics/
813+ * You will likely find that it is really hard to test without refactoring.
769814
770- https://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/
815+ * This is Good!
771816
772- Functions as Objects .
817+ * If code is hard to test -- it probably should be refactored .
773818
774819
775- Object Oriented Programming:
820+
821+ Material to review for next week
822+ --------------------------------
823+
824+ Next week, we'll get started on Object Oriented Methods. It's a good idea to read up on it first -- so we can dive right in:
776825
777826 * Dive into Python3: 7.2 -- 7.3
778827 http://www.diveintopython3.net/iterators.html#defining-classes
@@ -785,9 +834,9 @@ Object Oriented Programming:
785834
786835[note that in py3 you don't need to inherit from object]
787836
788- Talks by Raymond Hettinger:
837+ Talk by Raymond Hettinger:
789838
790- https://youtu.be/HTLu2DFOdTg
839+ Video of talk: https://youtu.be/HTLu2DFOdTg
791840
792- https://speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger
841+ Slides: https://speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger
793842
0 commit comments