# [SYSU][大二下] 高级编程技术HW Week-6 Lecture-1

Question

Answer

'''
SYSU - [专选]高级编程技术 - Week6, Lecture1 HW
by Duan
2018.04.16
'''

'''
11-1 城市和国家：
'''
import unittest
from city_functions import get_formatted_city
class FormattedCityTest(unittest.TestCase):
def test_city_country(self):
'''测试 city_functions.py'''
formatted_city = get_formatted_city('santiago', 'chile')
self.assertEqual(formatted_city, 'Santiago, Chile')
unittest.main()
#Output:
# --------------- 11-1 ---------------
# .
# ----------------------------------------------------------------------
# Ran 1 test in 0.000s
#
# OK

'''
11-2 人口数量：
'''
import unittest
from city_functions import get_formatted_city
from city_functions import formatted_city_popu
from city_functions import formatted_city_opt_popu
class FormattedCityTest(unittest.TestCase):
def test_city_country(self):
#formatted_city = formatted_city_popu('santiago', 'chile', '5000000')
#self.assertEqual(formatted_city, 'Santiago, Chile')     # 无法通过测试
formatted_city = formatted_city_opt_popu('santiago', 'chile')
self.assertEqual(formatted_city, 'Santiago, Chile')      # 可以通过测试

def test_city_country_population(self):
formatted_city = formatted_city_opt_popu('santiago', 'chile', '5000000')
self.assertEqual(formatted_city, 'Santiago, Chile - population 5000000')
unittest.main()
#Output:
#只运行第一个测试（代码中已被注释掉的那两行）：
# F
# ======================================================================
# FAIL: test_city_country (__main__.FormattedCityTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
#   File "11.1 - 城市和国家.py", line 40, in test_city_country
#     self.assertEqual(formatted_city, 'Santiago, Chile')      # 无法通过测试
# AssertionError: 'Santiago, Chile - population 5000000' != 'Santiago, Chile'
# - Santiago, Chile - population 5000000
# + Santiago, Chile

# ----------------------------------------------------------------------
# Ran 1 test in 0.001s

# FAILED (failures=1)

# 只运行后两个测试：
# ..
# ----------------------------------------------------------------------
# Ran 2 tests in 0.000s

# OK

'''
11-3 雇员：
'''
import unittest
class Employee():
def __init__(self, first_name, last_name, salary):
self.fname = first_name
self.lname = last_name
self.salary = salary

def give_raise(self, plus = 5000):
self.salary += plus
return self.salary

class EmployeeTest(unittest.TestCase):
def setUp(self):
self.employee = Employee('Peter', 'Parker', 100000)

def test_give_default_raise(self):
self.employee.salary = self.employee.give_raise()
self.assertEqual(self.employee.salary, 105000)

def test_give_custom_raise(self):
self.employee.salary = self.employee.give_raise(3000)
self.assertEqual(self.employee.salary, 103000)

unittest.main()
#Output:
# ..
# ----------------------------------------------------------------------
# Ran 2 tests in 0.001s

# OK


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120