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

Question

教材中课后的练习,11-1到11-3,选一些写到你的博客上


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值