作业十2018.4.15

11-1

import unittest
def city_country(city,country):
    return city+', '+country
class Nametest(unittest.TestCase):
    def test_city_country(self):
        string=city_country('Santiago','Chile')
        self.assertEqual(string,'Santiago, Chile')
unittest.main()

11-2

import unittest
def city_country(city,country,pop):
    return city+', '+country+' population- '+pop
class Nametest(unittest.TestCase):
    def test_city_country(self):
        string=city_country('Santiago','Chile','5000000')
        self.assertEqual(string,'Santiago, Chile population- 5000000')
unittest.main()

输出:

Ran 1 test in 0.000s

FAILED (failures=1)
import unittest
def city_country(city,country,popu):
    if popu:
        return city+', '+country+' population- '+popu
    else:
        return city + ', ' + country
class Nametest(unittest.TestCase):
    def test_city_country(self):
        string=city_country('Santiago','Chile','5000000')
        self.assertEqual(string,'Santiago, Chile population- 5000000')
unittest.main()

输出:

Ran 1 test in 0.000s

OK
import unittest
def city_country(city,country,popu):
    if popu:
        return city+', '+country+' population- '+popu
    else:
        return city + ', ' + country
class Nametest(unittest.TestCase):
    def test_city_country(self):
        string=city_country('Santiago','Chile',popu='5000000')
        self.assertEqual(string,'Santiago, Chile population- 5000000')
unittest.main()

输出:

Ran 1 test in 0.000s

OK

11-3

import unittest
class Employee():
    def __init__(self,firstname,lastname,annual_salary):
        self.firstname=firstname
        self.lastname=lastname
        self.salary=annual_salary
    def give_raise(self,number=5000):
        self.salary=self.salary+number
        return self.salary
class Test(unittest.TestCase):
    def setUp(self):
        self.number=[15000,20000]
        self.employ_one=Employee('Leborn','James',10000)
        self.employ_two=Employee('Stephen','Curry',10000)
    def test_give_default_raise(self):
        self.assertEqual(self.employ_one.give_raise(),self.number[0])
        self.assertEqual(self.employ_two.give_raise(),self.number[0])
    def test_give_custom_raise(self):
        self.assertEqual(self.employ_one.give_raise(10000), self.number[1])
        self.assertEqual(self.employ_two.give_raise(10000), self.number[1])
unittest.main()

输出:

Ran 2 tests in 0.001s

OK



阅读更多
想对作者说点什么?

博主推荐

换一批

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