Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit d07180e

Browse files
committed
2 parents 86f93a8 + 5e40cfb commit d07180e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+4511
-101
lines changed

examples/Session06/cigar_party.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
"""
3+
When squirrels get together for a party, they like to have cigars.
4+
A squirrel party is successful when the number of cigars is between
5+
40 and 60, inclusive. Unless it is the weekend, in which case there
6+
is no upper bound on the number of cigars.
7+
8+
Return True if the party with the given values is successful,
9+
or False otherwise.
10+
"""
11+
12+
13+
def cigar_party(num, weekend):
14+
pass
15+

examples/Session06/codingbat.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Examples from: http://codingbat.com
5+
6+
Put here so we can write unit tests for them ourselves
7+
"""
8+
9+
# Python > Warmup-1 > sleep_in
10+
11+
# The parameter weekday is True if it is a weekday, and the parameter
12+
# vacation is True if we are on vacation.
13+
#
14+
# We sleep in if it is not a weekday or we're on vacation.
15+
# Return True if we sleep in.
16+
17+
18+
def sleep_in(weekday, vacation):
19+
return not weekday or vacation
20+
21+
22+
# We have two monkeys, a and b, and the parameters a_smile and b_smile
23+
# indicate if each is smiling.
24+
25+
# We are in trouble if they are both smiling or if neither of them is
26+
# smiling.
27+
28+
# Return True if we are in trouble.
29+
30+
def monkey_trouble(a_smile, b_smile):
31+
return a_smile is b_smile

examples/Session06/my_mod.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
"""
3+
A trivial module to demonstrate testing
4+
"""
5+
6+
7+
def my_func(val1, val2):
8+
return val1 * val2
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
When squirrels get together for a party, they like to have cigars.
5+
A squirrel party is successful when the number of cigars is between
6+
40 and 60, inclusive. Unless it is the weekend, in which case there
7+
is no upper bound on the number of cigars.
8+
9+
Return True if the party with the given values is successful,
10+
or False otherwise.
11+
"""
12+
13+
14+
# you can change this import to test different versions
15+
from cigar_party import cigar_party
16+
# from cigar_party import cigar_party2 as cigar_party
17+
# from cigar_party import cigar_party3 as cigar_party
18+
19+
20+
def test_1():
21+
assert cigar_party(30, False) is False
22+
23+
24+
def test_2():
25+
26+
assert cigar_party(50, False) is True
27+
28+
29+
def test_3():
30+
31+
assert cigar_party(70, True) is True
32+
33+
34+
def test_4():
35+
assert cigar_party(30, True) is False
36+
37+
38+
def test_5():
39+
assert cigar_party(50, True) is True
40+
41+
42+
def test_6():
43+
assert cigar_party(60, False) is True
44+
45+
46+
def test_7():
47+
assert cigar_party(61, False) is False
48+
49+
50+
def test_8():
51+
assert cigar_party(40, False) is True
52+
53+
54+
def test_9():
55+
assert cigar_party(39, False) is False
56+
57+
58+
def test_10():
59+
assert cigar_party(40, True) is True
60+
61+
62+
def test_11():
63+
assert cigar_party(39, True) is False
64+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
test file for codingbat module
5+
6+
This version can be run with nose or py.test
7+
"""
8+
9+
from codingbat import sleep_in, monkey_trouble
10+
11+
12+
# tests for sleep_in
13+
def test_false_false():
14+
assert sleep_in(False, False)
15+
16+
17+
def test_true_false():
18+
assert not (sleep_in(True, False))
19+
20+
21+
def test_false_true():
22+
assert sleep_in(False, True)
23+
24+
25+
def test_true_true():
26+
assert sleep_in(True, True)
27+
28+
29+
# put tests for monkey_trouble here
30+
# monkey_trouble(True, True) → True
31+
# monkey_trouble(False, False) → True
32+
# monkey_trouble(True, False) → False
33+
34+
def test_monkey_true_true():
35+
assert monkey_trouble(True, True)
36+
37+
def test_monkey_false_false():
38+
assert monkey_trouble(False, False)
39+
40+
def test_monkey_true_false():
41+
assert monkey_trouble(True, False) is False
42+
43+
# more!

examples/Session06/test_my_mod.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
from my_mod import my_func
5+
6+
7+
class MyFuncTestCase(unittest.TestCase):
8+
def test_my_func(self):
9+
test_vals = (2, 3)
10+
expected = test_vals[0] * test_vals[1]
11+
actual = my_func(*test_vals)
12+
self.assertEquals(expected, actual)
13+
14+
15+
if __name__ == '__main__':
16+
unittest.main()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
pytest example of a parameterized test
5+
6+
NOTE: there is a failure in here! can you fix it?
7+
8+
"""
9+
import pytest
10+
11+
12+
# a (really simple) function to test
13+
def add(a, b):
14+
"""
15+
returns the sum of a and b
16+
"""
17+
return a + b
18+
19+
# now some test data:
20+
21+
test_data = [((2, 3), 5),
22+
((-3, 2), -1),
23+
((2, 0.5), 2.5),
24+
(("this", "that"), "this that"),
25+
(([1, 2, 3], [6, 7, 8]), [1, 2, 3, 6, 7, 8]),
26+
]
27+
28+
29+
@pytest.mark.parametrize(("input", "result"), test_data)
30+
def test_add(input, result):
31+
assert add(*input) == result
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
port of the random unit tests from the python docs to py.test
5+
"""
6+
7+
import random
8+
import pytest
9+
10+
11+
seq = list(range(10))
12+
13+
14+
def test_shuffle():
15+
# make sure the shuffled sequence does not lose any elements
16+
random.shuffle(seq)
17+
# seq.sort() # commenting this out will make it fail, so we can see output
18+
print("seq:", seq) # only see output if it fails
19+
assert seq == list(range(10))
20+
21+
22+
def test_shuffle_immutable():
23+
"""should get a TypeError with an imutable type """
24+
with pytest.raises(TypeError):
25+
random.shuffle((1, 2, 3))
26+
27+
28+
def test_choice():
29+
"""make sure a random item selected is in the original sequence"""
30+
element = random.choice(seq)
31+
assert (element in seq)
32+
33+
34+
def test_sample():
35+
"""make sure all items returned by sample are there"""
36+
for element in random.sample(seq, 5):
37+
assert element in seq
38+
39+
40+
def test_sample_too_large():
41+
"""should get a ValueError if you try to sample too many"""
42+
with pytest.raises(ValueError):
43+
random.sample(seq, 20)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
import unittest
3+
4+
5+
class TestSequenceFunctions(unittest.TestCase):
6+
7+
def setUp(self):
8+
self.seq = list(range(10))
9+
10+
def test_shuffle(self):
11+
# make sure the shuffled sequence does not lose any elements
12+
random.shuffle(self.seq)
13+
self.seq.sort()
14+
self.assertEqual(self.seq, list(range(10)))
15+
16+
# should raise an exception for an immutable sequence
17+
self.assertRaises(TypeError, random.shuffle, (1, 2, 3))
18+
19+
def test_choice(self):
20+
element = random.choice(self.seq)
21+
self.assertTrue(element in self.seq)
22+
23+
def test_sample(self):
24+
with self.assertRaises(ValueError):
25+
random.sample(self.seq, 20)
26+
for element in random.sample(self.seq, 5):
27+
self.assertTrue(element in self.seq)
28+
29+
if __name__ == '__main__':
30+
unittest.main()

materials/source/notes/session08.rst

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,40 @@ If the object(s) you need to create are complex, then you can use "fixtures" to
3333

3434
This will start to make more and more sense as we do more testing -- and particularly when we do TDD and write the tests along with the code.
3535

36-
do you always need an __init__?
37-
------------------------------
38-
39-
No -- you don't :-)
36+
Example:
37+
........
4038

41-
The ONLY thing "special" about __init__ is that it is automatically called when and instance is created. Other than that, it's a regular method. So if you don't define one, then the superclass' __init__ will be called.
42-
43-
That's what inheritance is all about -- the subclass inherits ALL the superclasses methods -- including __init__.
39+
.. code-block:: python
4440
45-
So never write an __init__ that does nothing but call the superclass __init__
41+
def test_p_tag():
42+
assert Para.tag == 'p'
4643
44+
I know I started out that way -- 'cause there wasn't anything else to test. But this is really testing an implementation detail -- the Para elements has a attribute named "tag" that is 'p'. But is that a public part of the API? do we care? -- No. What we care about is that the correct tag gets rendered, so a test for THAT makes more sense:
4745

48-
Bad OO design
49-
-------------
46+
.. code-block:: python
5047
51-
Script from work...depersonalized
48+
def test_render_para():
49+
my_stuff = 'spam, spam, spam'
50+
p = Para(my_stuff)
51+
more_stuff = 'eggs, eggs, eggs'
52+
p.append(more_stuff)
53+
contents = render_element(p).strip()
54+
assert contents.startswith('<p>')
55+
assert contents.endswith('</p>')
56+
assert my_stuff in contents
57+
assert more_stuff in contents
5258
53-
In class repo: ``examples/session08/unnecessary_oo.py``
5459
55-
Recall Jack Dietrich's talk: "Stop Writing Classes":
60+
Do you always need an __init__?
61+
------------------------------
5662

57-
Also PEP8, etc....
63+
No -- you don't :-)
5864

59-
let's do a code review.
65+
The ONLY thing "special" about __init__ is that it is automatically called when and instance is created. Other than that, it's a regular method. So if you don't define one, then the superclass' __init__ will be called.
6066

67+
That's what inheritance is all about -- the subclass inherits ALL the superclasses methods -- including __init__.
6168

69+
So never write an __init__ that does nothing but call the superclass __init__
6270

6371

6472

0 commit comments

Comments
 (0)