您現在的位置是:首頁 > 籃球

python程式設計從入門到實踐:一個要測試的類

  • 由 小龍談數學說Python 發表于 籃球
  • 2022-01-20
簡介annual_salary,5050)def test_give_custom_raise(self):self

測試類怎麼寫

想了解更多精彩內容,快來關注小龍談數學說Python

1.僱員:編寫一個名為Employee的類,其方法__init__()接受名、姓和年薪,並將它們都儲存在屬性中。編寫一個名為give_raise()的方法,它預設將年薪增加5000美元,但也能夠接受其他的年薪增加量。

為Employee編寫一個測試用例,其中包含兩個測試方法:test_give_default_raise()和test_give_custom_raise()。使用方法setUp(),以免在每個測試方法中都建立新的僱員例項。執行這個測試用例,確認兩個測試都通過了。

class Employee():

def __init__(self,name,surname,annual_salary):

self。name=name

self。surname=surname

self。annual_salary=annual_salary

def give_raise(self,amount=5000):

self。annual_salary+=amount

print(self。annual_salary)

import unittest

class TestEmployee(unittest。TestCase):

def setUp(self):

self。name=“xiaoming”

self。surname=“wang”

self。annual_salary=50

self。ex=Employee(“xiaoming”,“wang”,50)

def test_give_default_raise(self):

self。ex。give_raise()

self。assertEqual(self。ex。annual_salary,5050)

def test_give_custom_raise(self):

self。ex。give_raise(100)

self。assertEqual(self。ex。annual_salary,150)

unittest。main()

python程式設計從入門到實踐:一個要測試的類

Top