python作业(10)

11-1 城市和国家:

    代码如下:

import unittest
def city_functions(city,country):
	full=city.title()+", "+country.title()
	return full

class test_city(unittest.TestCase):
	def test_it(self):
		full=city_functions('santiago','chile')
		self.assertEqual(full,'Santiago, Chile')
unittest.main()




    运行结果:

    


11-2 人口数量:

    代码如下:

import unittest
def city_functions(city,country,population):
	full=city.title()+", "+country.title()+' - population '+population
	return full

class test_city(unittest.TestCase):
	def test_it(self):
		full=city_functions('santiago','chile')
		self.assertEqual(full,'Santiago, Chile')
unittest.main()
import unittest
def city_functions(city,country,population=0):
	if population:
		full=city.title()+", "+country.title()+' - population '+population
	else:
		full=city.title()+", "+country.title()
	return full

class test_city(unittest.TestCase):
	def test_it(self):
		full=city_functions('santiago','chile')
		self.assertEqual(full,'Santiago, Chile')
unittest.main()
import unittest
def city_functions(city,country,population=0):
	if population:
		full=city.title()+", "+country.title()+' - population '+population
	else:
		full=city.title()+", "+country.title()
	return full

class test_city(unittest.TestCase):
	def test_it(self):
		full=city_functions('santiago','chile')
		self.assertEqual(full,'Santiago, Chile')
	def test_po(self):
		full=city_functions('santiago','chile','5000000')
		self.assertEqual(full,'Santiago, Chile - population 5000000')
unittest.main()






    结果如下:

    



11-3 雇员:

     代码如下:

import unittest
class employee():
	def __init__(self, name,sage):
		self.name=name
		self.sage=sage
	def give_raise(self,add=5000):
		self.sage=self.sage+add



class test_emp(unittest.TestCase):
	def setUp(self):
		self._sage=employee('Mike',6000)

	def test_default_raise(self):
		self._sage.give_raise()	
		self.assertEqual(self._sage.sage,11000)

	def test_default_raise(self):
		self._sage.give_raise(add=2000)	
		self.assertEqual(self._sage.sage,8000)
unittest.main()




    结果如下:

    

    




阅读更多
想对作者说点什么? 我来说一句

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