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

2人阅读 评论(0) 收藏 举报
分类:

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

查看评论

C#.NET_面向对象编程技术

-
  • 1970年01月01日 08:00

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

Question浏览 Python 主页 ,在博客上写下你有哪些发现和收获。假设你已经称为一位 Python 编程高手,你打算实现怎样的程序?在博客上写下你的目标。Answer发现和收获:① 主页上不...
  • SherryTuan
  • SherryTuan
  • 2018-03-05 22:39:11
  • 24

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

Question教材中课后的练习,5-1到5-13,选一些写到你的博客上Answer''' SYSU - [专选]高级编程技术 - Week3, Lecture1 HW by Duan 2...
  • SherryTuan
  • SherryTuan
  • 2018-03-19 18:02:12
  • 8

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

Question教材中课后的练习,7-1到7-10,选一些写到你的博客上Answer''' SYSU - [专选]高级编程技术 - Week4, Lecture1 HW by Duan ...
  • SherryTuan
  • SherryTuan
  • 2018-03-26 20:34:27
  • 8

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

Question教材中课后的练习,9-1到9-15,选一些写到你的博客上Answer''' SYSU - [专选]高级编程技术 - Week5, Lecture1 HW by Duan ...
  • SherryTuan
  • SherryTuan
  • 2018-04-02 08:59:40
  • 48

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

Question教材中课后的练习,6-1到6-12,选一些写到你的博客上Answer''''' SYSU - [专选]高级编程技术 - Week3, Lecture2 HW by Duan ...
  • SherryTuan
  • SherryTuan
  • 2018-03-22 13:11:35
  • 6

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

Question教材中课后的练习,10-1到10-13,选一些写到你的博客上Answer''' SYSU - [专选]高级编程技术 - Week5, Lecture2 HW by Duan...
  • SherryTuan
  • SherryTuan
  • 2018-04-09 00:49:59
  • 5

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

Question教材中课后的练习,2-1到2-11,选一些写到你的博客上Answer''' SYSU - [专选]高级编程技术 - Week1, Lecture2 HW by Duan 2018.03...
  • SherryTuan
  • SherryTuan
  • 2018-03-08 13:13:42
  • 14

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

Question教材中课后的练习,4-1到4-15,选一些写到你的博客上Answer''' SYSU - [专选]高级编程技术 - Week2, Lecture2 HW by Duan 201...
  • SherryTuan
  • SherryTuan
  • 2018-03-14 21:14:14
  • 13

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

Question教材中课后的练习,8-1到8-17,选一些写到你的博客上Answer''' SYSU - [专选]高级编程技术 - Week4, Lecture2 HW by Duan ...
  • SherryTuan
  • SherryTuan
  • 2018-03-28 19:53:59
  • 10
    个人资料
    持之以恒
    等级:
    访问量: 188
    积分: 102
    排名: 125万+
    文章分类
    文章存档