4.9python作业

# 11-1
city_funcitons.py
def city_function(city, country):
    return city + ', ' + country
import unittest
from city_functions import city_function

class TestCityFunction(unittest.TestCase):
    def test_city_country(self):
        format = city_function('Santiago', 'Chile')
        self.assertEqual(format, 'Santiago, Chile')

unittest.main()

# 11-2

city_funcitons.py
def city_function(city, country, population= ''):
    if population:
        return city + ', ' + country + ' - ' + 'population' + ' ' + population
    else:
        return city + ', ' + country


import unittest
from city_functions import city_function
class TestCityFunction(unittest.TestCase):
    def test_city_country(self):
        format = city_function('Santiago', 'Chile')
        self.assertEqual(format, 'Santiago, Chile')
    def test_city_country_population(self):
        format = city_function('Santiago', 'Chile', '5000000')
        self.assertEqual(format, 'Santiago, Chile - population 5000000')

unittest.main()

# 11-3
import unittest
class Employee():

    def __init__(self, last_name, first_name, salary):
        self.last_name = last_name
        self.first_name = first_name
        self.salary = salary

    def inc_salary(self, new_salary = 5000):
        self.salary += new_salary
        # print("OK, the salary has been raised ' + str(new_salary)+'.\nNow it's " + str(self.salary) +'.')



class Test_Employee(unittest.TestCase):

    def setUp(self):
        self.first_name = 'Donald'
        self.last_name = 'Trump'
        self.my_Employee =  Employee(self.first_name, self.last_name, 10000)

    def test_give_default_raise(self):
        self.my_Employee.inc_salary()
        self.assertEqual(self.my_Employee.salary, 15000)

    def test_give_custom_raise(self):
        self.my_Employee.inc_salary(10000)
        self.assertEqual(self.my_Employee.salary, 20000)

unittest.main()

阅读更多
个人分类: 作业
上一篇4.4python作业
下一篇4.23python Leetcode array 41
想对作者说点什么? 我来说一句

ASP.net大作业含代码

2015年06月07日 1.07MB 下载

3DMAX 作业

2011年12月10日 205KB 下载

计算机图形学大作业

2015年06月01日 11.14MB 下载

没有更多推荐了,返回首页

关闭
关闭