第十一章课后习题

11-1:

city_functions.py

def country_city(city,country):
    #接受city和country
    #返回city,country
    result=city+','+country
    return result

test_cities.py

import unittest
from city_functions import country_city
class testCityFunctions(unittest.TestCase):
    def test_city_country(self):
        result=country_city('Shanghai','China')
        self.assertEqual(result,'Shanghai,China')
unittest.main()

运行结果:


11-2:

city_functions.py

def country_city(city,country,population):
    #接受city和country和population
    #返回city,country-population xxx
    result=city+','+country+'-population '+population
    return result

test_cities.py同上

运行结果:


修改后的city_functions.py

扫描二维码关注公众号,回复: 22527 查看本文章
def country_city(city,country,population=''):
    #接受city和country和population
    #返回city,country-population xxx
    if population:
        result=city+','+country+'-population '+population
    else:
        result=city+','+country
    return result

运行结果


修改后的test_cities.py

import unittest
from city_functions import country_city
class testCityFunctions(unittest.TestCase):
    # def test_city_country(self):
    #     result=country_city('Shanghai','China')
    #     self.assertEqual(result,'Shanghai,China')
    def test_city_country_population(self):
        result=country_city('Shanghai','China','250')
        self.assertEqual(result,'Shanghai,China-population 250')
unittest.main()

运行结果:


11-3:

employee.py

class Employee():
    def __init__(self,first_name,last_name,salary):
        self.first_name=first_name
        self.last_name=last_name
        self.salary=salary
    def salary_raise(self,raise_money=5000):
        self.salary+=raise_money

test_employee.py:

import unittest
from employee import Employee
class TestEmployee(unittest.TestCase):
    def setUp(self):
        self.my_employee=Employee('Zhan','Jianzhou',1000000)
    def test_default_salary_raise(self):
        self.my_employee.salary_raise()
        self.assertEqual(self.my_employee.salary,1005000)
    def test_not_default_salary_raise(self):
        self.my_employee.salary_raise(100000)
        self.assertEqual(self.my_employee.salary,1100000)
unittest.main()

猜你喜欢

转载自blog.csdn.net/qq_39178023/article/details/79943692