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()

猜你喜欢

转载自blog.csdn.net/control_xu/article/details/79949067
4.9