some answers for chapter 11

11-1

import unittest

from city_functions import get_city_country

classCityTestCase(unittest.TestCase):

    deftest_city_country(self):

        formatted_out= get_city_country("Santiago","Chile")

        self.assertEqual(formatted_out,"Santiago, Chile")

unittest.main(argv=['ignored','-v'],exit = False)

output:

test_city_country(__main__.CityTestCase) ... ok

 

----------------------------------------------------------------------

Ran 1 test in 0.004s

 

OK

 

11-2

from city_functions import get_city_country

import unittest

class TestCase(unittest.TestCase):

    def test_city_country(self):

        formatted_out= get_city_country("Santiago","Chile")

        self.assertEqual(formatted_out,"Santiago, Chile - population 5000000")

unittest.main(argv=['ignored','-v'],exit = False)

output:

test_city_country (__main__.TestCase) ...ERROR

 

======================================================================

ERROR: test_city_country (__main__.TestCase)

----------------------------------------------------------------------

Traceback (most recent call last):

  File"<ipython-input-2-19c82a13cac5>", line 5, in test_city_country

   formatted_out = get_city_country("Santiago","Chile")

TypeError: get_city_country() missing 1required positional argument: 'population'

 

----------------------------------------------------------------------

Ran 1 test in 0.000s

 

FAILED (errors=1)

 

output:

from city_functions import get_city_country

import unittest

class TestCase(unittest.TestCase):

    deftest_city_country(self):

        formatted_out= get_city_country("Santiago","Chile")

        self.assertEqual(formatted_out,"Santiago, Chile")

    def test_city_country_population(self):

        formatted_out= get_city_country("Santiago","Chile","5000000")

        self.assertEqual(formatted_out,"Santiago, Chile - population 5000000")

unittest.main(argv=['ignored','-v'],exit = False)

test_city_country (__main__.TestCase) ... ok

test_city_country_population(__main__.TestCase) ... ok

 

----------------------------------------------------------------------

Ran 2 tests in 0.000s

 

OK

 

11-3

class Employee():

    def__init__(self,second_name,first_name,pay):

        self.second_name = second_name

        self.first_name = first_name

        self.pay = pay

    defgive_raise(self, add = 5000):

        self.pay = self.pay + add

import unittest

class TestCase(unittest.TestCase):

    def setUp(self):

        self.formal_pay = 10000

        self.employee = Employee('b', 'a', self.formal_pay)

    def test_give_default_raise(self):

        self.employee.give_raise()

        self.formal_pay += 5000

        self.assertEqual(self.formal_pay, self.employee.pay)

    def test_give_custom_raise(self):

        add = 3000

        self.employee.give_raise(add)

        self.formal_pay += add

        self.assertEqual(self.formal_pay, self.employee.pay)

unittest.main(argv=['ignored','-v'],exit = False)

output:

test_give_custom_raise (__main__.TestCase)... ok

test_give_default_raise (__main__.TestCase)... ok

 

----------------------------------------------------------------------

Ran 2 tests in 0.004s

 

OK

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值