def city_function(City,Country):
msg=City+","+Country
return msg.title()
import unittest #导入模块unittest
from name_function import city_function
class CityTest(unittest.TestCase):
def test_city_country(self):
msg=city_function('santiago','chile')
self.assertEqual(msg,'Santiago,Chile')
unittest.main()
#11-2
def city_function(City,Country,population=''):
if population:
msg=City.title()+","+Country.title()+'-'+population
else:
msg=City.title()+","+Country.title()
return msg
import unittest #导入模块unittest
from name_function import city_function
class CityTest(unittest.TestCase):
def test_city_country(self):
msg=city_function('santiago','chile')
self.assertEqual(msg,'Santiago,Chile')
def test_city_country_population(self):
a=city_function('santiago','chile','population=5000000')
self.assertEqual(a,'Santiago,Chile-population=5000000')
unittest.main()
#11-3
class Employee():
def __init__(self,first_name,last_name,salary):
self.first_name=first_name
self.last_name=last_name
self.salary=salary
def give_raise(self,add=5000):
salary=int(self.salary)+add
return int(salary)
import unittest
from name_function import Employee
class TestEmployee(unittest.TestCase):
def setUp(self):
self.my_employee=Employee('zhang','san','8888')
def test_give_default_raise(self):
self.assertEqual(self.my_employee.give_raise(),13888)
def test_give_custom_raise(self):
self.assertEqual(self.my_employee.give_raise(6666),15554)
unittest.main()