import unittest
from word_counter import get_word_occurences
class TestGetWordOccurences(unittest.TestCase):
def test_returns_zero_if_word_not_in_text(self):
word = 'marto'
text = 'someone is testing'
result = get_word_occurences(word, text)
self.assertEqual(result, 0)
def test_returns_one_if_word_is_exactly_once_in_text(self):
word = 'marto'
text = 'marto is testing'
result = get_word_occurences(word, text)
self.assertEqual(result, 1)
if __name__ == '__main__':
unittest.main()
(A)rrange
(A)ct
(A)ssert
(A)rrange
(A)ct
(A)ssert
if __name__ == '__main__':
unittest.main()
python week01/01.PythonProblems/test_word_counter.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
watch python3 week01/01.PythonProblems/test_word_counter.py
Every 2,0s: python3 week01/01.PythonProblems/test_word_counter.py martin056-lenovo: Wed Mar 4 15:05:38 2020
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Using watch
Running tests by hand
Each method should do exactly one thing.
Each test method should test exactly one unit/thing.
import unittest
class TestGetWordOccurences(unittest.TestCase):
def test_returns_all_word_occurances_in_text(self):
word = 'marto'
text = 'marto is testing'
result = get_word_occurences(word, text)
self.assertEqual(result, 1)
E
======================================================================
ERROR: test_returns_zero_if_word_not_in_text (__main__.TestGetWordOccurences)
----------------------------------------------------------------------
Traceback (most recent call last):
File "week01/01.PythonProblems/test_word_counter.py", line 9, in test_returns_zero_if_word_not_in_text
result = get_word_occurences(word, text)
NameError: name 'get_word_occurences' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
def get_word_occurences(word, text):
return int(word in text)
class TestGetWordOccurences(unittest.TestCase):
def test_returns_all_word_occurances_in_text(self):
word = 'marto'
text = 'marto is testing'
result = get_word_occurences(word, text)
self.assertEqual(result, 1)
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
def get_word_occurences(word, text):
return int(word in text)
class TestGetWordOccurences(unittest.TestCase):
def test_returns_all_word_occurances_in_text(self):
word = 'marto'
text = 'marto is testing'
result1 = get_word_occurences(word, text)
result2 = get_word_occurences(word, f'{text} marto marto')
self.assertEqual(result1, 1)
self.assertEqual(result2, 3)
FAIL: test_returns_all_word_occurances_in_text (__main__.TestGetWordOccurences)
----------------------------------------------------------------------
Traceback (most recent call last):
File "week01/01.PythonProblems/test_word_counter.py", line 26, in test_returns_all_word_occurances_in_text
self.assertEqual(result2, 3)
AssertionError: 1 != 3
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1)