第六周-Python-hw

11-1 城市和国家

代码

#city_functions
def city_country(city, country):
    return city.title() + ", " + country.title();
#test_cities.py
import unittest
from city_functions2 import city_country

class TestCities(unittest.TestCase):      
    def test_city_country(self):  
        format_ans=city_country('Santiago','chile')  
        self.assertEqual(format_ans,'Santiago, Chile')  
unittest.main() 

输出

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

OK

11-2 人口数量

代码

  • 测试1
def city_country(city, country, population):
    return city.title() + ", " + country.title() + " - population " + str(population);
#test_cities
import unittest
from city_functions2 import city_country

class TestCities(unittest.TestCase):      
    def test_city_country(self):  
        format_ans=city_country('Santiago','chile')  
        self.assertEqual(format_ans,'Santiago, Chile')  
unittest.main() 
  • 测试 2
#population可选参数
def city_country(city, country, population=-1):
    if population!=-1:
        return city.title() + ", " + country.title() + " - population " + str(population);
    return city.title() + ", " + country.title();
#test_cities
import unittest
from city_functions2 import city_country

class TestCities(unittest.TestCase):      
    def test_city_country(self):  
        format_ans=city_country('Santiago','chile')  
        self.assertEqual(format_ans,'Santiago, Chile')  
unittest.main() 
  • 测试3
#population可选参数
def city_country(city, country, population=-1):
    if population!=-1:
        return city.title() + ", " + country.title() + " - population " + str(population);
    return city.title() + ", " + country.title();
#test_cities
import unittest
from city_functions2 import city_country

class TestCities(unittest.TestCase):      
    def test_city_country(self):  
        format_ans=city_country('Santiago','chile')  
        self.assertEqual(format_ans,'Santiago, Chile')  

    def test_city_country_population(self):
        format_ans=city_country('Santiago','chile', 1000000)  
        self.assertEqual(format_ans,'Santiago, Chile - population '+str(1000000))  
unittest.main() 

输出

F
======================================================================
FAIL: test_city_country (__main__.TestCities)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_cities.py", line 7, in test_city_country
    self.assertEqual(format_ans,'Santiago, Chile')
AssertionError: 'Santiago, Chile - population -1' != 'Santiago, Chile'
- Santiago, Chile - population -1
+ Santiago, Chile


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

FAILED (failures=1)
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

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

OK

11-3 雇员

代码

class Employee:  
    def __init__ (self,first_name,last_name,salary=-1):  
        self.first_name=first_name  
        self.last_name=last_name  
        self.salary=salary  

    def give_raise(self,raise_=5000):  
        self.salary+=raise_


import unittest  
class Test(unittest.TestCase):  

    def setUp(self):   
        self.employee=Employee('Amy','Smith', 6000)  

    def test_give_default_raise(self):  
        self.employee.give_raise()  
        self.assertEqual(11000,self.employee.salary)  
    def test_give_custom_raise(self):  
        self.employee.give_raise(10000)  
        self.assertEqual(16000,self.employee.salary)  
unittest.main()

输出

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

OK
阅读更多
上一篇第五周-python-hw2
下一篇第8周-Python-leetcode-hw1
想对作者说点什么? 我来说一句

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

关闭
关闭
关闭