Created
March 15, 2017 21:23
-
-
Save evrom/4f8158b2613cb80c5050e7315fa309cc to your computer and use it in GitHub Desktop.
Practice Test for Python Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run the test cases, execute
python3 -m unittest practice_test