如果我通过执行accountant.give_default_raise()或accountant.give_custom_raise()来手动测试这两个函数,但一切正常,但当我运行单元测试时,它不断给我提供错误消息并说错误。代码有效,但单元测试说错误,发生了什么?
class Employee():
def __init__(self, first_name, last_name, annual_salary = 3000):
"""Declare the attributes"""
self.first_name = first_name
self.last_name = last_name
self.annual_salary = annual_salary
def give_default_raise(self):
"""Add $5,0000 by default to the annual salary, but accept any amount"""
self.annual_salary += 5000
new_salary = self.annual_salary
print(new_salary)
def give_custom_raise(self):
"""Add a custom amount"""
custom_raise = input("How much would you like to increase? ")
self.annual_salary += int(custom_raise)
new_custom_salary = self.annual_salary
print(new_custom_salary)
accountant = Employee('John', 'Jones', 120000)
accountant.give_default_raise()
import unittest
class TestEmployee(unittest.TestCase):
"""Test the Employee class"""
def test_give_default_raise(self):
accountant = Employee('John', 'Jones', 120000)
self.assertEqual(annual_salary, 125000)
unittest.main()
什么是'annual_salary'在'test_give_default_raise()'函数?该变量名似乎没有在任何地方定义。 –
你有一个全局的'accountant',然后在'test_give_default_raise'中创建一个本地'accountant'。他们不提及同一个对象。 –