2 Tests, one at a time
just setupWe cannot tell whether count still works after a change, unless we try it by hand each time. Tests try it for us.
Each test gives count a text, and compares what it gives back with what we expect.
We add the tests one file at a time, and run them after each. One test asks for something that count cannot do yet. It asks count to count
"The" and "the" as one word.
What changed
src/tally/__init__.py: changescount. 1 line added, 1 removed. The file.tests/test_count.py: addsclass Count,test_case,test_words. New, 11 lines. The file.tests/test_empty.py: addsclass Empty,test_empty. New, 8 lines. The file.
The diff of src/tally/__init__.py
@@ -5,7 +5,7 @@ import sys
def count(text):
counts = {}
- for word in text.split():
+ for word in text.lower().split():
counts[word] = counts.get(word, 0) + 1
return counts
The diff of tests/test_count.py
@@ -0,0 +1,11 @@
+import unittest
+
+from tally import count
+
+
+class Count(unittest.TestCase):
+ def test_words(self):
+ self.assertEqual(count("a b a"), {"a": 2, "b": 1})
+
+ def test_case(self):
+ self.assertEqual(count("A a"), {"a": 2})The diff of tests/test_empty.py
@@ -0,0 +1,8 @@
+import unittest
+
+from tally import count
+
+
+class Empty(unittest.TestCase):
+ def test_empty(self):
+ self.assertEqual(count(""), {})