11-1 城市和国家
city_functions.py 部分
def city_functions(city,country):
full_name = city.title() + ", "+country.title();
return full_name
测试程序部分:
import unittest
from city_functions import city_function
class NamesTestCase(unittest.TestCase):
def city_country_test(self):
full_name = city_functions('shenzhen', 'china')
self.assertEqual(full_name, 'Shenzhen, China')
if __name__ == '__main__':
unittest.main()
11-2 人口数量
city_functions.py部分
def city_function(city,country,population):
full_name = city + ", "+country + " - population " + population;
return full_name.title()
测试函数部分
import unittest
from city_functions import city_function
class NamesTestCase(unittest.TestCase):
def city_country_test(self):
full_name = city_functions('Santiago', 'Chile', '5000000')
self.assertEqual(full_name, 'Santiago, Chile - population 5000000')
if __name__ == '__main__':
unittest.main()
11-3 雇员
myEmployee.py部分
class AnonymousSurvey():
def __init__(self, myname,myage,mysalary):
self.naem = myname
self.age = myage
self.salary = mysalary
def give_raise(self,inc=5000):
self.salary = self.salary + inc;
测试函数部分
import unittest
from myEmployee import employee
class TestEmployee(unittest.TestCase):
def setUp(self):
self.my_employee = employee("Tim",30,5000)
def test_give_default_raise():
self.my_employeegive_raise()
self.assertIn(self.my_employee(10000, self.my_employee.salary))
def test_give_custom_raise():
self.my_employeegive_raise(1000)
self.assertIn(self.my_employee(11000, self.my_employee.salary))
unittest.main()