11-1
# city_functions.py def city_country(city, country): return city + ',' + country# test_cities.py
from city_functions import *
import unittest
class test(unittest.TestCase):
def test_city_country(self):
self.assertEqual(city_country('Santiago', 'Chile'),
'Santiago,Chile')
unittest.main()
11-2
# city_functions.py def city_country(city, country,population): string = city + ',' + country if population: string +='-population' + population return string
# test_cities.py from city_functions import * import unittest class test(unittest.TestCase): def test_city_country(self): self.assertEqual(city_country('Santiago', 'Chile', '5000000'), 'Santiago,Chile') unittest.main()
可选后
# city_functions.py def city_country(city, country, population = ''): string = city + ',' + country if population: string +='-population' + population return string
# test_cities.py from city_functions import * import unittest class test(unittest.TestCase): def test_city_country(self): self.assertEqual(city_country('Santiago', 'Chile'), 'Santiago,Chile') unittest.main()
# test_cities.py from city_functions import * import unittest class test(unittest.TestCase): def test_city_country(self): self.assertEqual(city_country('Santiago', 'Chile'), 'Santiago,Chile') def test_city_country_population(self): self.assertEqual(city_country('Santiago', 'Chile', '5000000'), 'Santiago,Chile-population5000000') unittest.main()
11-3
#Employee.py class Employee(): def __init__(self, firstname, lastname, salary): self.firstname = firstname self.lastname = lastname self.salary = salary def give_raise(self, raises = 5000): self.salary += raises
#test_employee.py import unittest from Employee import * class test(unittest.TestCase): def setUp(self): self.employee = Employee('Michael', 'Jackson', 1000000) def test_give_default_raise(self): self.employee.give_raise() self.assertEqual(self.employee.salary, 1005000) def test_give_custom_raise(self): self.employee.give_raise(15000) self.assertEqual(self.employee.salary, 1015000) unittest.main()