11-1
import unittest
from city_functions import get_city_country
classCityTestCase(unittest.TestCase):
deftest_city_country(self):
formatted_out= get_city_country("Santiago","Chile")
self.assertEqual(formatted_out,"Santiago, Chile")
unittest.main(argv=['ignored','-v'],exit = False)
output:
test_city_country(__main__.CityTestCase) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.004s
OK
11-2
from city_functions import get_city_country
import unittest
class TestCase(unittest.TestCase):
def test_city_country(self):
formatted_out= get_city_country("Santiago","Chile")
self.assertEqual(formatted_out,"Santiago, Chile - population 5000000")
unittest.main(argv=['ignored','-v'],exit = False)
output:
test_city_country (__main__.TestCase) ...ERROR
======================================================================
ERROR: test_city_country (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File"<ipython-input-2-19c82a13cac5>", line 5, in test_city_country
formatted_out = get_city_country("Santiago","Chile")
TypeError: get_city_country() missing 1required positional argument: 'population'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
output:
from city_functions import get_city_country
import unittest
class TestCase(unittest.TestCase):
deftest_city_country(self):
formatted_out= get_city_country("Santiago","Chile")
self.assertEqual(formatted_out,"Santiago, Chile")
def test_city_country_population(self):
formatted_out= get_city_country("Santiago","Chile","5000000")
self.assertEqual(formatted_out,"Santiago, Chile - population 5000000")
unittest.main(argv=['ignored','-v'],exit = False)
test_city_country (__main__.TestCase) ... ok
test_city_country_population(__main__.TestCase) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
11-3
class Employee():
def__init__(self,second_name,first_name,pay):
self.second_name = second_name
self.first_name = first_name
self.pay = pay
defgive_raise(self, add = 5000):
self.pay = self.pay + add
import unittest
class TestCase(unittest.TestCase):
def setUp(self):
self.formal_pay = 10000
self.employee = Employee('b', 'a', self.formal_pay)
def test_give_default_raise(self):
self.employee.give_raise()
self.formal_pay += 5000
self.assertEqual(self.formal_pay, self.employee.pay)
def test_give_custom_raise(self):
add = 3000
self.employee.give_raise(add)
self.formal_pay += add
self.assertEqual(self.formal_pay, self.employee.pay)
unittest.main(argv=['ignored','-v'],exit = False)
output:
test_give_custom_raise (__main__.TestCase)... ok
test_give_default_raise (__main__.TestCase)... ok
----------------------------------------------------------------------
Ran 2 tests in 0.004s
OK