Python from introduction to practice: Chapter 11 of exercises after class

11-1 city and country: write a function that accepts two parameters: a city name and a country name. This function returns a string in the form of City, Country, such as Santiago, Chile. Store this function in a city_functions.py Module of.

Create a test_cities.py To test the function you just wrote (remember, you need to import the module unittest and the function you want to test). Write a test_ city_ Method of country() to verify that the string is correct when using values like 'santiago' and 'chile' to call the above functions. Run test_cities.py , confirm test_city_country() passed.

from  city_functions  import city_country
import unittest
 
class CityTestCase(unittest.TestCase):
	def test_city_country(self):
		city1 = city_country('santiago', 'chile')
		self.assertEqual(city1,'Santiago, Chile')
 
unittest.main()

 

11-2 population: modify the previous function to include the third essential parameter population, and return a string in the format of City, Country - population xxx, such as Santiago, Chile - population 5000000. Run test_cities.py , confirm test_city_country() failed. Modify the above function to set the parameter population to optional. Run test again_ cities.py , confirm test_city_country() passed again. Write another test_ city_ country_ A test of population () to verify that the function can be called with values like 'santiago', 'chile' and 'population=5000000'. Run test again_ cities.py , confirm test_city_country_population() passed.

from  city_functions  import city_country2
import unittest
 
class TestCity(unittest.TestCase):
    def test_city_country(self):
        city2 = city_country2('santiago', 'chile')
        self.assertEqual(city2,'Santiago, Chile')
	def test_city_country_population(self):
         city3 = city_country2('santiago', 'chile', 'population=5000000')
         self.assertEqual(city3,'Santiago, Chile - population=5000000')
 
unittest.main()

 

11-3 Employee: write a class called Employee with methods__ init__ () takes the first name, last name, and annual salary and stores them in the properties. Write a project called give_raise() method, which increases the annual salary by $5000 by default, but can also accept other annual salary increases.

Write a test case for Employee, which contains two test methods: test_give_default_raise() and test_give_custom_raise() . Use the method setUp() to avoid creating new Employee instances in each test method. Run the test case to verify that both tests have passed.

class Employee():
	def __init__(self, first, last, salaryYear):
         self.first = first
         self.last = last
         self.salaryYear = salaryYear
	def give_raise(self,add = 5000):
         self.salaryYear += add
         return self.salaryYear
from Employee import Employee
import unittest

class TestCase(unittest.TestCase):
	def setUp(self):
         self.DefaultEmploy = Employee('ming', 'xiao', 1000)
         self.ans1 = 9000
         self.give_raise_ = Employee('hua', 'xiao', 800)
         self.ans2 = 10000
	def test_give_default_raise(self):
         self.assertEqual(str(self.ans1), str(self.DefaultEmploy.give_raise(0)))
	def test_give_custom_raise(self):
         self.assertEqual(self.ans2,self.give_raise_.give_raise(500))

unittest.main()

 

Added by adamwhiles on Thu, 18 Jun 2020 11:33:24 +0300