11-1
定义函数
def get_city_country(city,country):
return city+' '+country
测试函数
import unittest
from name_function import get_city_country
class CityCountryTestCase(unittest.TestCase):
def test_city_country(self):
city = 'santuago'
country = 'chile'
self.assertEqual(get_city_country(city,country),'santuago chile')
unittest.main()
Ran 1 test in 0.000s
OK
------------------
(program exited with code: 0)
11-2
定义函数
def get_city_country(city,country,population):
return city.title()+', '+country.title()+' - population '+ str(population)
测试函数
import unittest
from name_function import get_city_country
class CityCountryTestCase(unittest.TestCase):
def test_city_country(self):
city = 'santiago'
country = 'chile'
population = 5000000
self.assertEqual(get_city_country(city,country,population),'Santiago, Chile - population 5000000')
unittest.main()
Ran 1 test in 0.000s
OK
------------------
(program exited with code: 0)
11-3
import unittest
class Employee():
def __init__(self,first_name,last_name,salary):
self.first_name = first_name
self.last_name = last_name
self.salary = salary
def give_raise(self):
self.salary = 5000
return self.salary
class TestEmployee(unittest.TestCase):
def setUp(self):
self.Employee = Employee('Alice','Bob',200)
def test_give_default_raise(self):
self.assertEqual(self.Employee.salary,200)
def test_give_custom_raise(self):
self.assertEqual(self.Employee.give_raise(),5000)
unittest.main()
Ran 2 tests in 0.000s
OK
------------------
(program exited with code: 0)