Skip to content

Commit 9ac85c7

Browse files
committed
完成第十一章测试代码练习--testcities有bug
1 parent eb0f60c commit 9ac85c7

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

chapter11/city_functions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def city_country(city, country):
2+
output_string = city.title() + ", " + country.title()
3+
if population:
4+
output_string += ' - population ' + str(population)
5+
return output_string

chapter11/employee.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Employee():
2+
"""A class to represent an employee."""
3+
4+
def __init__(self, f_name, l_name, salary):
5+
"""Initialize the employee."""
6+
self.first = f_name.title()
7+
self.last = l_name.title()
8+
self.salary = salary
9+
10+
def give_raise(self, amount=5000):
11+
"""Give the employee a raise."""
12+
self.salary += amount

chapter11/test_cities.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
3+
from city_functions import city_country
4+
5+
class CitiesTestCase(unittest.TestCase):
6+
7+
def test_city_country(self):
8+
santiago_chile = city_country('santiago', 'chile')
9+
self.assertEqual(santiago_chile, 'Santiago, Chile')
10+
11+
def test_city_country_population(self):
12+
santiago_chile = city_country('santiago', 'chile', population=5000000)
13+
self.assertEqual(santiago_chile, 'Santiago, Chile - population 5000000')
14+
15+
unittest.main()

chapter11/test_employee.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
3+
from employee import Employee
4+
5+
class TestEmployee(unittest.TestCase):
6+
7+
def setUp(self):
8+
self.eric = Employee('eric', 'matthes', 65000)
9+
10+
def test_give_default_raise(self):
11+
self.eric.give_raise()
12+
self.assertEqual(self.eric.salary, 70000)
13+
14+
def test_give_custom_raise(self):
15+
self.eric.give_raise(10000)
16+
self.assertEqual(self.eric.salary, 75000)
17+
18+
unittest.main()

0 commit comments

Comments
 (0)