SYSU python 作业

11.1

#test_cities.py

import unittest
from city_functions import func
class test(unittest.TestCase):
	def test_city_country(self):
		cc=func("A","B")
		self.assertEqual(cc,"A,B")
unittest.main()

#city_functions.py

def func(a,b):
	ans=a+','+b
	return ans

11.2
#必要
def func(a,b,c):
	ans=a+','+b+' - population '+str(c)
	return ans
#可选
def func(a,b,c=0):
	if c==0:
		ans=a+','+b+' - population '+str(c)
	else:
		ans=a+','+b+' - population '+str(c)
	return ans
#测试
import unittest
from city_functions import func
class test(unittest.TestCase):
	def test_city_country(self):
		cc=func("A","B")
		self.assertEqual(cc,"A,B")
unittest.main()

11.3
#test.py

import unittest
from Employee import employee
class test(unittest.TestCase):
	def setUp(self):
		self.emp=employee('A','B',5000)
		self.num=5000
	def test_give_default_raise(self):
		self.assertEqual(self.num+5000,self.emp.give_raise())
	def test_give_custom_raise(self):
		self.assertEqual(self.num+6000,self.emp.give_raise(6000))
unittest.main()

#Employee.py

class employee():
	def __init__(self,firstname,lastname,salary):
		self.fn=firstname
		self.ln=lastname
		self.s=int(salary)
	def give_raise(self , a=5000):
		self.s+=int(a)
		return self.s

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页