Skip to content

Instantly share code, notes, and snippets.

@evrom

evrom/exam2.py Secret

Created May 24, 2017 20:27
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/50b884d419c18ad9235fc741bf9fd490 to your computer and use it in GitHub Desktop.
Save evrom/50b884d419c18ad9235fc741bf9fd490 to your computer and use it in GitHub Desktop.
exam2
"""
Make two test cases pass to pass the class.
Run with "python3 -m unittest exam2.py"
"""
from unittest import TestCase
class ProductTestCase(TestCase):
"""
Args:
a - a number
b - another number
Returns:
a string with the format
"$A times $B is $C.", where
$A is the first argument, $B is a the second argument,
and $C is the $A*$B.
>>> test_function(3, 4)
"3 times 4 is 12."
"""
def setUp(self):
self.test_function = None # assign to your function
def test_ex1(self):
result = self.test_function(2, 3)
self.assertEqual(result, '2 times 3 is 6')
def test_ex2(self):
result = self.test_function(5, 4)
self.assertEqual(result, '5 times 4 is 20')
class MakeDictionaryTestCase(TestCase):
"""
Args:
english_words - a list of english words
estonian_words - a list of estonian words
Returns:
a dictionary where the keys are english words and the values are
estonian words. The first word in english_words is mapped to the
first word in estonian_words
>>> test_function(['cat', 'dog'], ['kass', 'koer'])
{'cat': 'kass', 'dog': 'koer'}
"""
def setUp(self):
self.test_function = None # assign to your function
def test_ex1(self):
english_words = ['dandelion', 'calendula', 'echinacea']
estonian_words = ['võilill', 'saialill', 'päevakübar']
expected_result = {'dandelion': 'võilill', 'calendula': 'saialill',
'echinacea': 'päevakübar'}
result = self.test_function(english_words, estonian_words)
self.assertEqual(result, expected_result)
def test_ex2(self):
english_words = ['millet', 'buckwheat']
estonian_words = ['hirss', 'tatar']
expected_result = {'millet': 'hirss', 'buckwheat': 'tatar'}
result = self.test_function(english_words, estonian_words)
self.assertEqual(result, expected_result)
class GreetManyTestCase(TestCase):
"""
Args:
names - a list of names
Returns:
a string starting with 'Hello ', followed by the names separated by
commas, and ending with a '.'
Hint:
There a multiple ways to do this. The last name in the list does not
have ', ' following it.
you may need to check if a name is the last element of the list.
>>> test_function(['Donald', 'Kersti'])
'Hello Donald, Kersti.'
"""
def setUp(self):
self.test_function = None # assign to your function
def test_ex1(self):
names = ['Bob']
result = self.test_function(names)
self.assertEqual(result, 'Hello Bob.')
def test_ex2(self):
names = ['Taavet', 'Kalev', 'Maarja']
result = self.test_function(names)
self.assertEqual(result, 'Hello Taavet, Kalev, Maarja.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment