《Python编程-从入门到实践》第11章习题选练

11-1. City, Country: Write a function that accepts two parameters: a city name and a country name. The function should return a single string of the form City, Country, such as Santiago, Chile. Store the function in a module called city_functions.py.

Create a file called test_cities.py that tests the function you just wrote (remember that you need to import unittest and the function you want to test). Write a method called test_city_country() to verify that calling your function with values such as 'santiago' and 'chile' results in the correct string. Run test_cities.py, and make sure test_city_country() passes.

Codes:

#city_function.py
def cities(City, Country):
    return City+', '+Country
#test_cities.py
import unittest
from city_function import cities

class CitiesTestCase(unittest.TestCase):
    def test_city_country(self):
        format_city = cities('Guangzhou', 'China')
        self.assertEqual(format_city, 'Guangzhou, China')

unittest.main()

Results:

.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

11-2. Population: Modify your function so it requires a third parameter, population. It should now return a single string of the form City, Country – population xxx, such as Santiago, Chile – population 5000000. Run test_cities.py again. Make sure test_city_country() fails this time.
Modify the function so the population parameter is optional. Run test_cities.py again, and make sure test_city_country() passes again.
Write a second test called test_city_country_population() that verifies you can call your function with the values 'santiago', 'chile', and 'population=5000000'. Run test_cities.py again, and make sure this new test

passes.

Codes:

#city_function.py
def cities(City, Country, population = 0):
    if population:
        return City+', '+Country+' - population '+str(population)
    else:
        return City+', '+Country
#test_cities.py
import unittest
from city_function import cities

class CitiesTestCase(unittest.TestCase):
    def test_city_country(self):
        format_city = cities('Guangzhou', 'China')
        self.assertEqual(format_city, 'Guangzhou, China')
    def test_city_country_population(self):
        format_city = cities('Guangzhou', 'China', 14000000)
        self.assertEqual(format_city, 'Guangzhou, China - population 14000000')
unittest.main()

Results:

E
======================================================================
ERROR: test_city_country (__main__.CitiesTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_cities.py", line 7, in test_city_country
    format_city = cities('Guangzhou', 'China')
TypeError: cities() missing 1 required positional argument: 'population'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
..
----------------------------------------------------------------------
Ran 2 tests in 0.011s

OK

11-3. Employee: Write a class called Employee. The __init__() method should take in a first name, a last name, and an annual salary, and store each of these as attributes. Write a method called give_raise() that adds $5000 to the annual salary by default but also accepts a different raise amount.

Write a test case for Employee. Write two test methods, test_give_default_raise() and test_give_custom_raise(). Use the setUp() method so you don’t have to create a new employee instance in each test method. Run your test case, and make sure both tests pass.

Codes:

#employee_class.py

class Employee():
    def __init__(self,first,last,salary):
        self.first = first
        self.last = last
        self.salary = salary
    def give_raise(self, r = 5000):
        self.salary += r
#test_employee.py
import unittest
from employee_class import Employee

class EmployeeTestCase(unittest.TestCase):
    def setUp(self):
        self.my_employee = Employee('Zachary','Lei',10000)
    def test_give_default_raise(self):
        self.my_employee.give_raise()
        self.assertEqual(self.my_employee.salary, 15000)
    def test_give_custom_raise(self):
        self.my_employee.give_raise(10000)
        self.assertEqual(self.my_employee.salary, 20000)
        
unittest.main()

Results:

..
----------------------------------------------------------------------
Ran 2 tests in 0.013s

OK

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值