Skip to content

Commit 2fa16f3

Browse files
committed
Add to unit testing examples
1 parent 28b2a1e commit 2fa16f3

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,3 +2411,25 @@ To see comments, run
24112411
```
24122412
python3 name_of_test_file.py -v
24132413
```
2414+
2415+
### Common Assertions
2416+
2417+
- `self.assertEqual(x,y)`
2418+
- `self.assertNotEqual(x,y)`
2419+
- `self.assertTrue(x)`
2420+
- `self.assertFalse(x)`
2421+
- `self.assertIsNone(x)`
2422+
- `self.assertIsNotNone(x)`
2423+
2424+
### Testing for errors
2425+
2426+
When testing for errors, `self.assertRaises` lets you test for a specific type of error.
2427+
2428+
```python
2429+
class SomeTests(unittest.TestCase):
2430+
def testing_for_errors(self):
2431+
"""testing for an error"""
2432+
with self.assertRaises(IndexError):
2433+
l = [1,2,3]
2434+
l[100]
2435+
```

unit_testing/activities.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from random import choice
2+
13
def eat(food, is_healthy):
4+
if not isinstance(is_healthy, bool):
5+
raise ValueError("is_healthy must be a boolean")
26
ending = "because YOLO"
37
if is_healthy:
48
ending = "because my body is a temple"
@@ -7,4 +11,11 @@ def eat(food, is_healthy):
711
def nap(num_hours):
812
if num_hours >= 2:
913
return f"Ugh, I overslept. I didn't mean to nap for {num_hours} hours"
10-
return f"I'm feeling refreshed after my {num_hours} hour nap"
14+
return f"I'm feeling refreshed after my {num_hours} hour nap"
15+
16+
def is_funny(person):
17+
if person is "tim": return False
18+
return True
19+
20+
def laugh():
21+
return choice(("lol", "haha", "tehehe"))

unit_testing/tests.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from activities import eat, nap
2+
from activities import eat, nap, is_funny, laugh
33

44
class ActivityTest(unittest.TestCase):
55
def test_eat_healthy(self):
@@ -8,24 +8,45 @@ def test_eat_healthy(self):
88
eat("broccoli", is_healthy=True),
99
"I'm eating broccoli, because my body is a temple"
1010
)
11+
1112
def test_eat_unhealthy(self):
1213
"""eat should indicate you've given up for unhealthy eating"""
1314
self.assertEqual(
1415
eat("pizza", is_healthy=False),
1516
"I'm eating pizza, because YOLO"
1617
)
18+
19+
def test_eat_healthy_boolean(self):
20+
"""is_healthy must be a boolean"""
21+
with self.assertRaises(ValueError):
22+
eat("pizza", is_healthy="who cares?")
23+
1724
def test_short_nap(self):
1825
"""short naps should be refreshing"""
1926
self.assertEqual(
2027
nap(1),
2128
"I'm feeling refreshed after my 1 hour nap"
2229
)
30+
2331
def test_long_nap(self):
2432
"""long naps should be discouraging"""
2533
self.assertEqual(
2634
nap(3),
2735
"Ugh, I overslept. I didn't mean to nap for 3 hours"
2836
)
2937

38+
def test_is_funny_tim(self):
39+
"""Tim should not be funny"""
40+
self.assertFalse(is_funny("tim"), "Tim should not be funny")
41+
42+
def test_is_funny_anyone_else(self):
43+
"""anyone else but Tim should be funny"""
44+
self.assertTrue(is_funny("blue"), "Blue should be funny")
45+
self.assertTrue(is_funny("tammy"), "Tammy should be funny")
46+
self.assertTrue(is_funny("sven"), "Sven should be funny")
47+
48+
def test_laugh(self):
49+
self.assertIn(laugh(), ("lol", "haha", "tehehe"))
50+
3051
if __name__ == "__main__":
3152
unittest.main()

0 commit comments

Comments
 (0)