《python编程从入门到实践》第11章 测试代码(练习)

P192

#11-1
#city_functions.py
def place(city,country):
    """生成城市和国家"""
    full_name = city+', '+country
    return full_name.title()



import unittest
from city_functions import place

class Testplace(unittest.TestCase):
    """测试city_functions.py"""
    
    def test_city_country(self):
        """能够正确处理像Santiago, Chile这样的地点吗?"""
        place_name = place('Santiago','Chile')
        self.assertEqual(place_name,"Santiago, Chile")
        
unittest.main()

#11-2(1)
#city_functions2.py
def place(city,country,population):
    """生成城市 国家和人口数"""
    full_name = city+', '+country+' - population '+population
    return full_name.title()
   

 
import unittest
from city_functions2 import place

class Testplace(unittest.TestCase):
    """测试city_functions.py"""
    
    def test_city_country(self):
        """能够正确处理像Santiago, Chile这样的地点吗?"""
        place_name = place('Santiago','Chile')
        self.assertEqual(place_name,"Santiago, Chile")
        
unittest.main()

#11-2(2)
#city_functions3.py

def place(city,country,population = ''):
    """生成城市 国家和人口数"""
    if population:
        full_name = city+', '+country+' - '
        full__name = full_name.title()+population
        return full__name
        
    else:
        full_name = city+', '+country
        return full_name.title()
    
    


import unittest
from city_functions3 import place

class Testplace(unittest.TestCase):
    """测试city_functions.py"""
    
    def test_city_country(self):
        """能够正确处理像Santiago, Chile这样的地点吗?"""
        place_name = place('Santiago','Chile')
        self.assertEqual(place_name,"Santiago, Chile")
        
unittest.main()


#11-2(3)
#city_functions3.py
def place(city,country,population = ''):
    """生成城市 国家和人口数"""
    if population:
        full_name = city+', '+country+' - '
        full__name = full_name.title()+population
        return full__name
        
    else:
        full_name = city+', '+country
        return full_name.title()
        


import unittest
from city_functions3 import place

class Testplace(unittest.TestCase):
    """测试city_functions.py"""
    
    def test_city_country(self):
        """能够正确处理像Santiago, Chile这样的地点吗?"""
        place_name = place('santiago','chile')
        self.assertEqual(place_name,"Santiago, Chile")
        
    def test_city_country_population(self):
        msg = place('santiago','chile','population = 5000000')
        self.assertEqual(msg,"Santiago, Chile - population = 5000000")
        
unittest.main()

P198  11-3

第一种方法

#eply.py
class Employee():
    
    def __init__(self,first_name,last_name,salary):
        self.first_name = first_name
        self.last_name = last_name
        self.salary = int(salary)
        
    def give_raise(self,add_salary = 5000):
        self.salary += add_salary
import unittest
from eply import Employee

class TestEmployee(unittest.TestCase):
    """针对Employee的测试"""
    
    def setUp(self):
        """创建雇员实例"""
        
        self.employee = Employee('chen','xie','1000')
        
    def test_give_default_raise(self):
        self.employee.give_raise()
        self.assertEqual(self.employee.salary,6000)
        
    def test_give_custom_raise(self):
        self.employee.give_raise(3000)
        self.assertEqual(self.employee.salary,4000)
        
unittest.main()

第二种方法

#eply1.py
class Employee():
    
    def __init__(self,first_name,last_name,salary):
        self.first_name = first_name
        self.last_name = last_name
        self.salary = int(salary)
        self.Raise = 5000
        
    def give_raise(self):
        self.salary += self.Raise
        return self.salary
import unittest
from eply1 import Employee

class TestEmployee(unittest.TestCase):
    """针对Employee的测试"""
    
    def setUp(self):
        """创建雇员实例"""
        
        self.employee = Employee('chen','xie','1000')
        self.my_salary = [5000,2000]
        
    def test_give_default_raise(self):
        self.assertEqual(self.employee.give_raise(),6000)
        
    def test_give_custom_raise(self):
        self.employee.Raise = self.my_salary[1]
        self.assertEqual(self.employee.give_raise(),3000)
        
unittest.main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值