测试函数
任务1.城市和国家
- 问题描述
编写一个函数,它接受两个形参:一个城市名和一个国家名。这个函数返回一个格式为City, Country 的字符串,Santiago, Chile 。将 这个函数存储在一个名为city_functions.py的模块中。创建一个名为test_cities.py的程序,对刚编写的函数进行测试(别忘了,你需要导入模块unittest 以及要测试的函数)。编写一个名test_city_country() 的方法,核实使用类似于’santiago’ 和’chile’ 这样的值来调用前述函数时,得到的字符串是正确的。运行test_cities.py ,确认测试test_city_country() 通过了 - 源代码
city_functions.py:
"""A collection of functions for working with cities."""
def city_country(city, country):
""""Return a string like 'Santiago, Chile'."""
full_inf = city + ", " + country
return full_inf.title()
test_citys.py:
import unittest
from city_functions import city_country
class NameTestCase(unittest.TestCase):
"""Test for 'city_functions.py'."""
def test_city_country(self):
""""Can it process information like city, country correctly"""
santiago_chile = city_country('santiago', 'chile')
self.assertEqual(santiago_chile, 'Santiago, Chile')
unittest.main()
- 结果截图
任务2.人口数量
- 问题描述
修改前面的函数,使其包含第三个必不可少的形参population ,并返回一个格式为City, Country - population xxx 的字符串, 如Santiago, Chile - population 5000000 。运行test_cities.py,确认测试test_city_country() 未通过。 修改上述函数,将形参population 设置为可选的。再次运行test_cities.py,确认测试test_city_country() 又通过了。 再编写一个名为test_city_country_population() 的测试,核实可以使用类似于’santiago’ 、‘chile’ 和’population=5000000’ 这样的值来调用 这个函数。再次运行test_cities.py,确认测试test_city_country_population() 通过了。 - 源代码及结果截图
a.修改任务1的函数,使其包含第三个必不可少的形参population。
"""A collection of functions for working with cities."""
def city_country(city, country, population):
""""Return a string like 'Santiago, Chile'."""
full_inf = city + ", " + country + " - population " + str(int(population))
return full_inf.title()
b.编写及运行test_cities.py。
import unittest
from city_functions import city_country
class NameTestCase(unittest.TestCase):
def test_city_country(self):
""""Can it process information like 'city, country' correctly?"""
santiago_chile = city_country('santiago', 'chile')
self.assertEqual(santiago_chile, 'Santiago, Chile')
unittest.main()
c.继续修改函数,将形参population 设置为可选的。
"""A collection of functions for working with cities."""
def city_country(city, country, population = ''):
""""Return a string like 'Santiago, Chile'."""
if population:
full_inf = city + ", " + country + " - population " + str(int(population))
else:
full_inf = city + ", " + country
return full_inf.title()
d.再次运行test_cities.py,则通过了测试。
e.修改函数及编写test_city_country_population() 的测试。
city_functions.py
"""A collection of functions for working with cities."""
def city_country(city, country, population = ''):
""""Return a string like 'Santiago, Chile'."""
if population:
full_inf = city + ", " + country + " - population " + str(int(population))
else:
full_inf = city + ", " + country
return full_inf
test_cities.py
import unittest
from city_functions import city_country
class NameTestCase(unittest.TestCase):
"""Test for 'city_functions.py'."""
def test_city_country(self):
""""Can it process information like city, country correctly."""
santiago_chile = city_country('Santiago', 'Chile')
self.assertEqual(santiago_chile, 'Santiago, Chile')
def test_city_country_population(self):
"""Can it process information like 'city, country - population xxx' correctly?"""
santiago_chile_5000000 = city_country('Santiago', 'Chile', '5000000')
self.assertEqual(santiago_chile_5000000, 'Santiago, Chile - population 5000000')
unittest.main()
d.最后再次运行test_cities.py,确认测试test_city_country_population() 通过了。
测试类
任务.雇员
- 问题描述
编写一个名为Employee 的类,其方法__init__() 接受名、姓和年薪,并将它们都存储在属性中。编写一个名为give_raise() 的方法,它默认将年薪增加5000美元,但也能够接受其他的年薪增加量。 为Employee 编写一个测试用例,其中包含两个测试方法:test_give_default_raise() 和test_give_custom _raise() 。使用方法setUp() ,以免在每个测试方法中都创建新的雇员实例。运行这个测试用例,确认两个测试都通过了。 - 源代码
employ_functions.py
class Employee():
def __init__(self, first_name, last_name, annual_salary):
self.first_name = first_name
self.last_name = last_name
self.annual_salary = annual_salary
def give_raise(self, amount=5000):
self.annual_salary += amount
test_employee.py
import unittest
from employee_functions import Employee
class TestEmployee(unittest.TestCase):
"""A test for the employee class."""
def setUp(self):
"""Make an employee to use in the tests."""
self.aurora = Employee('aurora', 'marry', 35489)
e
def test_give_default_raise(self):
"""Test that a default raise works corrcetly."""
self.aurora.give_raise()
self.assertEqual(self.aurora.annual_salary, 40489)
def test_give_custom_raise(self):
"""Test that a custom raise works correctly."""
self.aurora.give_raise(24000)
self.assertEqual(self.aurora.annual_salary, 59489)
unittest.main()
- 结果截图
总结
- 方法setUp() 做了两件事情:创建一个调查对象;创建一个答案列表。
- 作为初学者,并非必须为你尝试的所有项目编写测试;但参与工作量较大的项目时,你应对自己编写的函数和类的重要行为进行测试。这样 你就能够更加确定自己所做的工作不会破坏项目的其他部分,你就能够随心所欲地改进既有代码了。如果不小心破坏了原来的功能,你马上就会知道,从而能够轻松地修复问 题。相比于等到不满意的用户报告bug后再采取措施,在测试未通过时采取措施要容易得多。
- 运行测试用例时,每完成一个单元测试,Python都打印一个字符:测试通过时打印一个句点;测试引发错误时打印一个E ;测试导致断言失败时打印一个F 。 这就是运行测试用例时,在输出的第一行中看到的句点和字符数量各不相同的原因。如果测试用例包含很多单元测试,需要运行很长时间,就可通过观察这些结果 来获悉有多少个测试通过了。