Python编程从入门到实践第十一章-测试代码

一曲冷凌霜 提交于 2020-02-12 17:52:21

11-1

def get_formatted_name(city,country):
    '''函数返回一个格式为City, Country 的字符串'''
    full_name = city + country
    return full_name.title()
import unittest
from city_functions import get_formatted_name
class CityCountryTestCase(unittest.TestCase):
    def test_city_country(self):
        formatted_name =get_formatted_name('santiago ', 'chile')
        self.assertEqual(formatted_name,'Santiago Chile')
unittest.main()

11-2

def get_formatted_name(city,country,population=''):
    full_name = city + country + '-population' + str(population)
    return full_name.title()

 

import unittest
from city_functions import get_formatted_name
class CityCountryTestCase(unittest.TestCase):
    def test_city_country(self):
        formatted_name =get_formatted_name('santiago ', 'chile','50000')
        self.assertEqual(formatted_name,'Santiago Chile-Population50000')
unittest.main()

 11-3

import  unittest
class Employee ():
    def __init__(self,first,last,salary):
        self.first = first
        self.last = last
        self.salary = salary
    def give_raise(self,salary=5000):
        self.salary+=salary
class TesstEmployee (unittest. TestCase) :
    def setUp(self):
        self.emp = Employee('','',1000)
    def test_give_default_raise(self):
        self.emp.give_raise() 
        self.assertEqual(self.emp.salary,6000)
    def test_give_custom_raise(self):
        self.emp.give_raise(6000)
        self.assertEqual(self.emp.salary,7000)
unittest.main()
输出:
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!