Python测试不工作

2024-09-30 05:16:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一些帮助来确定为什么我写的测试不起作用。最初,我编写了这样的测试程序,它工作了,现在仍然如此:

import unittest
from employee_info import EmployeeData

class TestEmployee(unittest.TestCase):

def setUp(self):
    self.david_scott = EmployeeData('david', 'scott', 60000)

def test_give_custom_raise(self):
    self.david_scott.give_raise(5200)
    self.assertEqual(self.david_scott.annual_salary, 65200)

unittest.main()

当天晚些时候,我在练习,并重新编写了相同的代码如下所示,它不会工作

import unittest
from employee_info import EmployeeData

class TestEmployee(unittest.TestCase):


def setUP(self):
    self.david_scott = EmployeeData('david', 'scott', 60000)

def test_give_custom_raise(self):
    self.david_scott.give_raise(5200)
    self.assertEqual(self.david_scott.annual_salary, 65200)

unittest.main()

为了解决问题,我复制了原来的代码,并将其粘贴到同一个打开的文件中,它按预期工作。现在,我花了2个小时试图弄清楚为什么将第一个代码粘贴到文件中效果很好,而粘贴第二个代码会返回以下错误:

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

错误:test给自定义加薪(main.TestEmployee)

回溯(最近一次呼叫): 文件“/Users/DavinChace/Desktop/python\u work/Chapter 15-Generating Data/employee\u test\u module.py”,第10行,test\u give\u custom\u raise 赛尔夫。大卫•斯科特。加薪(5200) AttributeError:'TestEmployee'对象没有属性'david\u scott'


在0.000秒内运行了1次测试

失败(错误=1)

我试过将这两个代码复制到MicrosoftWord和excel中,逐行比较它们,它们完全相同。关于是什么导致一个代码工作而另一个代码失败,有什么想法吗?要清楚的是,在这两种情况下,代码都运行在同一个打开的文件中


Tags: 文件代码testimportselfdefcustomemployee

热门问题