11-1 城市和国家
city_functions.py
def union(city,country):
return city + ',' + country
test_cities.py
import unittest
from city_functions import union
class Test(unittest.TestCase):
def test1(self):
form = union('Santiago', 'Chile')
self.assertEqual(form, 'Santiago,Chile')
unittest.main
注意在pycharm里运行时,最后一行“main”后不能像书上那样有括号,不然会无法读取测试内容。
11-2 人口数量
函数:
def union(city, country, population):
return city + ',' + country + '- population ' + str(population)
测试:
import unittest
from city_functions import union
class Test(unittest.TestCase):
def test1(self):
form = union('Santiago', 'Chile',population=5000000)
self.assertEqual(form, 'Santiago,Chile- population 5000000')
unittest.main
11-3 雇员
类:
class Employee:
def __init__(self, first_name, last_name, salary=0):
self.first_name = first_name
self.last_name = last_name
self.salary = salary
def give_raise(self, salary_plus=5000):
self.salary += salary_plus
return (salary_plus)
测试代码:
import unittest
from city_functions import Employee
class Test(unittest.TestCase):
def setUp(self):
self.e1 = Employee('Bob', 'Stevin')
self.e2 = Employee('Stuart', 'Kevin', 5000)
def test_give_default_raise(self):
self.assertEqual(str(self.e1.give_raise()), '5000')
def test_give_custom_raise(self):
self.assertEqual(str(self.e2.give_raise(3000)), '3000')