【python】第十一次课作业

11-1

city_functions.py

def cityFun(city, country):
	return city + ", " + country

test_cities.py

import unittest
from city_functions import cityFun

class CityTestCase(unittest.TestCase):
	def test_city_country(self):
	    formatted_city = cityFun('santiago', 'chile')
	    self.assertEqual(formatted_city, 'santiago, chile')
		
unittest.main()

运行结果:


11-2

city_functions.py:

def cityFun(city, country, population):
	return city + ', ' + country + '- population ' + population

test_cities.py未改变,得到的结果为:

测试未通过,

将函数改变为

def cityFun(city, country, population = ''):
	return city + ', ' + country + population

得到如下结果

测试通过

改过之后的test_cities类:

import unittest
from city_functions import cityFun

class CityTestCase(unittest.TestCase):
	def test_city_country(self):
	    formatted_city = cityFun('santiago', 'chile')
	    self.assertEqual(formatted_city, 'santiago, chile ')
		
	def test_city_country_population(self):
		formatted_popu = cityFun('santiago', 'chile', 'population=5000000')
		self.assertEqual(formatted_popu, 'santiago, chile population=5000000')
unittest.main()

运行结果:


11-3

hello.py

class Employee():
	def __init__(self, fname, lname, money):
		self.fname = fname
		self.lname = lname
		self.money = money
	
	def give_raise(self, add = '5000'):
		addn = int(add)
		moneyn = int(self.money)
		moneyn += addn
		self.money = str(moneyn)

test_employee.py

import unittest
from hello import Employee

class TestEmployeeCase(unittest.TestCase):
	def setUp(self):
		fname = 'dasfsd'
		lname = 'fsdda'
		money = '30000'
		self.my_employee = Employee(fname, lname, money)
		self.add = '2000'
		
	def test_give_default_raise(self):
		self.my_employee.give_raise()
		self.assertEqual(self.my_employee.money, '35000')
		
	def test_give_custom_raise(self):
		self.my_employee.give_raise(self.add)
		self.assertEqual(self.my_employee.money, '32000')
		
unittest.main()

运行结果


猜你喜欢

转载自blog.csdn.net/karroyzgj/article/details/79873483