第11章--测试类

#练习 11-1:
# #存储在名为city_functions.py的模块中
# def city_country(city,country):
#     loc = f'{city},{country}'
#     return loc.title()
# #创建一个名为test_cities.py的程序,对刚才的程序进行测试
# import unittest
# from city_functtions import city_country as cc
# class CityTestCase(unittest.TestCase):
#     """用于测试city_country"""
#     def test_city_country(self):
#         """能够正确处理像,newyork,america的位置吗?"""
#         loction = cc('newyork','america')
#         self.assertEqual(loction,'Newyork,America')

# if __name__ == '__main__':
#     unittest.main()
    

#练习 11-2:
# #存储在名为city_functions.py的模块中
# def city_country(city,country,population=''):
#     if population:
#         loc = f'{city},{country}-population {population}'
#     else:
#         loc = f'{city},{country}'
#     return loc.title()
# #创建一个名为test_cities.py的程序,对刚才的程序进行测试
# import unittest
# class CityTestCase(unittest.TestCase):
#     """用于测试city_country"""
#     def test_city_country(self):
#         """能够正确处理像,newyork,america的例子吗?"""
#         loction = city_country('newyork','america')
#         self.assertEqual(loction,'Newyork,America')
#     def test_city_country_population(self):
#         """能够正确处理像,newyork,america-Population 5000000的例子吗?"""
#         example = city_country('newyork', 'america',5000000)
#         self.assertEqual(example, "Newyork,America-Population 5000000")
# if __name__ == '__main__':
#     unittest.main()



#练习 11-3:
# import unittest
# class Employee:
#     def __init__(self,first_name,last_name,yearly_salary):
#         """将三个变量存储在属性里,以供随时调用"""
#         self.first_name = first_name
#         self.last_name = last_name
#         self.yearly_salary = yearly_salary
#     def give_raise(self,amount = 5000):
#         """每次将年薪增加五千美元"""
#         self.yearly_salary += amount

# class TestEmployee(unittest.TestCase):
#     def setUp(self):
#         """
#         创建一个调查对象和一组答案,供使用的测试方法使用
#         """
#         self.my_employee = Employee('华','李',50000)
#     def test_give_default_raise(self):
#         """
#         测试增加五千年薪是否可行
#         """
#         self.my_employee.give_raise()
#         self.assertTrue(self.my_employee.yearly_salary == 55000)
#     def test_give_custom_raise(self):
#         """
#         测试自定义增加年薪是否可行
#         """
#         self.my_employee.give_raise(100000)
#         self.assertTrue(self.my_employee.yearly_salary == 150000)

# if __name__ == '__main__':
#     unittest.main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-阿呆-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值