Skip to content

Instantly share code, notes, and snippets.

@evrom
Created March 15, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrom/4f8158b2613cb80c5050e7315fa309cc to your computer and use it in GitHub Desktop.
Save evrom/4f8158b2613cb80c5050e7315fa309cc to your computer and use it in GitHub Desktop.
Practice Test for Python Class
from unittest import TestCase
"""
To pass, you must make 2 of the testcases pass
"""
class WordCounterTestCase(TestCase):
"""
Return a dict with the number of occurrences of an item in a list
>>> function(['hi', 'bye', 'hi'])
{'hi': 2, 'bye': 1}
"""
def setUp(self):
self.word_counter = None # assign to your function
def test_word_count_ex1(self):
data = ['car', 'bee', 'car', 'car', 'car']
expected_result = {
'car': 4, 'bee': 1}
result = self.word_counter(data)
self.assertEqual(result, expected_result)
def test_word_count_ex2(self):
data = ['tree', 'tree', 'park', 'park']
expected_result = {
'tree': 2, 'park': 2}
result = self.word_counter(data)
self.assertEqual(result, expected_result)
class MessageWriterTestCase(TestCase):
"""
this function should return a message depending on the input
Args:
recipient (str)
sender (str, optional)
mean (bool, optional)
>>> function('Bob')
"Hi Bob! -Anonymous"
>>> function('Bob', 'Sally')
"Hi Bob! -Sally"
>>> function('Bob', 'Sally', mean=True)
"Hi Bob! You are bad. -Sally"
"""
def setUp(self):
self.message_writer = None # assign to your function
def test_word_count_ex1(self):
expected_result = "Hi Taavi! -Piip"
result = self.message_writer(sender='Piip', recipient='Taavi')
self.assertEqual(result, expected_result)
def test_word_count_ex2(self):
expected_result = "Hi Peeka! You are bad. -Anonymous"
result = self.message_writer('Peeka', mean=True)
self.assertEqual(result, expected_result)
class BulkNumberAdderTestCase(TestCase):
"""
return a list with some number to every element of a list of numbers
>>> function(2, [1,2,3])
[3,4,5]
"""
def setUp(self):
self.bulk_number_adder = None # assign to your function
def test_word_count_ex1(self):
expected_result = [4, 3, 0]
result = self.bulk_number_adder(3, [1, 0, -3])
self.assertEqual(result, expected_result)
def test_word_count_ex2(self):
expected_result = [0, 4, 1]
result = self.bulk_number_adder(-2, [2, 6, 3])
self.assertEqual(result, expected_result)
@evrom
Copy link
Author

evrom commented Mar 16, 2017

To run the test cases, execute python3 -m unittest practice_test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment