1. 程式人生 > >《Python程式設計:從入門到實踐》第11章-測試程式碼 習題

《Python程式設計:從入門到實踐》第11章-測試程式碼 習題

文章目錄

11-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

def place(city, country):
	"""生成一個地點"""
	place = city.title() + ", " + country.title()
	return place

test_city.py

import unittest
from city_functions import place

class PlaceTestCase(unittest.TestCase):
	"""測試city_functions.py"""
	#繼承unittest模組中的TestCase類
	def test_city_country_name
(self): """能夠正確處理像Santiago, Chile這樣的地名嗎?""" place_name = place('santiago', 'chile') self.assertEqual(place_name, 'Santiago, Chile') unittest.main()

build

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

OK

11-2 人口數量

修改前面的函式,使其包含第三個不可少的形參population,並返回一個格式為City, Country - population xxx的字串,如Santiago, Chile - population 5000000。執行test_city.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()通過了。

(1)修改函式模組

city_functions2.py

def place(city, country, population = ''):
	"""生成一個地點和該地人口"""
	if population:
		place = city.title() + ", " + country.title() + " - population " + str(population)
	else:
		place = city.title() + ", " + country.title()  
	return place

(2)測試

import unittest
from city_functions2 import place

class PlacePopulationTestCase(unittest.TestCase):
	"""測試city_functions2.py"""
	def test_city_country_name(self):
		"""能夠正確處理像Santiago, Chile這樣的地名嗎?"""
		place_name = place('santiago', 'chile')
		self.assertEqual(place_name, 'Santiago, Chile')

	def test_city_country_population(self):
		"""能夠正確處理像Santiago, Chile - population 5000000這樣的地名和資訊嗎?"""
		place_name = place('santiago', 'chile', population = '5000000')
		self.assertEqual(place_name, 'Santiago, Chile - population 5000000')

unittest.main()

注意:

  • 記住匯入整個模組(unittest)的語句。記住從模組匯入函式的語句。
    • 【匯入整個模組的情況下呼叫函式/類要加模組名的字首,如unittest.TestCase, unittest.main()
    • 【匯入函式的情況下不用加字首】
  • 記住繼承的類名。unittest.TestCase。這是固定的。
  • self.assertEqual() 斷言方法
  • unittest.main()

11-3 僱員

編寫一個名為Employee的類,其方法__init__(接受名、姓和年薪,並將它們都儲存在屬性中。編寫一個名為give_raise()的方法,它預設將年薪增加5000美元,但也能夠接受其他的年薪增量。
為Employee編寫一個測試用例,其中包含兩個測試方法:test_give_default_raise()和test_give_custom_raise()。使用方法setUp(),以免在每個測試方法中都建立新的僱員例項。執行這個測試用例,確認兩個測試都通過了。
(1)編寫一個名為Employee的類

class Employee():
	"""收集僱員的資訊"""

	def __init__(self, last_name, first_name, wage):
		"""儲存僱員的名,姓,薪水"""
		self.last_name = last_name.title()
		self.first_name = first_name.title()
		self.wage = wage

	def give_raise(self, increment=5000):
		self.increment = increment
		self.wage += increment

(2)測試

import unittest
from employee import Employee

class TestEmployee(unittest.TestCase):
	"""針對Employee類的測試"""

	def setUp(self):
		"""建立一組名,姓,薪水的例項,供使用的測試方法使用"""
		f_name = "Wang"
		l_name = "Xizhi"
		wage = 10000
		self.wangxizhi = Employee(l_name, f_name, wage)

	def test_give_default_raise(self):
		"""測試使用預設增量能被妥善儲存"""
		self.wangxizhi.give_raise()
		self.assertEqual(self.wangxizhi.wage, 15000)

	def test_give_custom_raise(self):
		"""測試使用其他年薪增量能被妥善儲存"""
		self.wangxizhi.give_raise(2000)
		self.assertEqual(self.wangxizhi.wage, 12000)

unittest.main()

build

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

注意:
①方法setUp()
②包含字首self的變數名,可以在這個類的任何地方使用。