第十一次作業(2018-04-09,週一)

huanghh29發表於2018-04-12

教材第十一章習題

11-1

#city_functions.py

def city_func(city_name, country_name):
	return (city_name+","+country_name).title()

#test_cities.py

import unittest
from city_functions import city_func
class Test(unittest.TestCase):
	def test_city_country(self):
		city_country = city_func('santiago', 'chile')
		self.assertEqual(city_country,'Santiago,Chile')
unittest.main()

11-2

a.

"""city_functions.py"""
def city_func(city_name, country_name,population):
	return (city_name+", "+country_name).title()+" - population "+str(population)
"""test.cities.py"""
import unittest
from city_functions import city_func
class Test(unittest.TestCase):
	def test_city_country(self):
		city_country = city_func('santiago', 'chile')
		self.assertEqual(city_country,'Santiago,Chile')
unittest.main()

b.

"""city_functions.py"""
def city_func(city_name, country_name,population=0):
	if population:
		return (city_name+", "+country_name).title()+" - population "+str(population)
	else:
		return (city_name+", "+country_name).title()
"""test_city_country.py"""
import unittest
from city_functions import city_func
class Test(unittest.TestCase):
	def test_city_country(self):
		city_country = city_func('santiago', 'chile')
		self.assertEqual(city_country,'Santiago, Chile')
unittest.main()

c.

"""city_functions.py"""
def city_func(city_name, country_name,population=0):
	if population:
		return (city_name+", "+country_name).title()+" - population "+str(population)
	else:
		return (city_name+", "+country_name).title()
"""test_city_country.py"""
import unittest
from city_functions import city_func
class Test(unittest.TestCase):
	def test_city_country(self):
		city_country = city_func('santiago', 'chile')
		self.assertEqual(city_country,'Santiago, Chile')
	def test_city_country_population(self):
		city_country_population = city_func('santiago', 'chile', 5000000)
		self.assertEqual(city_country_population, 'Santiago, Chile - population 5000000')
unittest.main()

11-3

"""test_employee"""
import unittest
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, increase_salary=5000):
		self.salary+=increase_salary

class TestEmployee(unittest.TestCase):
	def setUp(self):
		self.employee = Employee("Hello","World",10000)
		#employee2 = Employee("Hello", "World", 10000)
	def test_give_default_raise(self):
		old_salary = self.employee.salary
		self.employee.give_raise()
		self.assertEqual(self.employee.salary,old_salary+5000)
	def test_give_custom_raise(self):
		old_salary = self.employee.salary
		raise_salary = 10000
		self.employee.give_raise(raise_salary)
		self.assertEqual(self.employee.salary, old_salary+raise_salary)
unittest.main()