第十一章习题

1.

import unittest

def cc(city, country):
	c = city.title() + ', ' + country.title()
	return c

class ccTestCase(unittest.TestCase):
	def test_c_c(self):
		com = cc('beijing', 'china')
		self.assertEqual(com, 'Beijing, China')

unittest.main()


.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

2.

import unittest

def cc(city, country, population):
	c = city.title() + ', ' + country.title() + ' - population ' + str(population)
	return c

class ccTestCase(unittest.TestCase):
	def test_c_c(self):
		com = cc('beijing', 'china')
		self.assertEqual(com, 'Beijing, China')

unittest.main()



E
======================================================================
ERROR: test_c_c (__main__.ccTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "未命名.py", line 9, in test_c_c
    com = cc('beijing', 'china')
TypeError: cc() missing 1 required positional argument: 'population'

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (errors=1)
import unittest

def cc(city, country, population = 50000000):
	c = city.title() + ', ' + country.title() + ' - population ' + str(population)
	return c

class ccTestCase(unittest.TestCase):
	def test_c_c(self):
		com = cc('beijing', 'china')
		self.assertEqual(com, 'Beijing, China - population 50000000')

unittest.main()



.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
import unittest

def cc(city, country, population = 50000000):
	c = city.title() + ', ' + country.title() + ' - population ' + str(population)
	return c

class ccTestCase(unittest.TestCase):
	def test_c_c(self):
		com = cc('beijing', 'china')
		self.assertEqual(com, 'Beijing, China - population 50000000')
	
	def test_c_c_p(self):
		com = cc('santiago', 'chile', population = 5000000)
		self.assertEqual(com, 'Santiago, Chile - population 5000000')

unittest.main()



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

OK

3.

import unittest

class Employee():
	def __init__(self, first, last, salary):
		self.first = first
		self.last = last
		self.salary = salary
	
	def give_raise(self, up = 5000):
		self.salary += up

class EmployeeTestCase(unittest.TestCase):
	def setUp(self):
		self.emp = Employee('Tom', 'Smith', 2000)
	
	def test_give_default_raise(self):
		self.emp.give_raise()
		self.assertEqual(self.emp.salary, 7000)
	
	def test_give_custom_raise(self):
		self.emp.give_raise(10000)
		self.assertEqual(self.emp.salary, 12000)

unittest.main()



..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

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

博主推荐

换一批

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