【发布时间】:2017-05-04 16:23:39
【问题描述】:
我希望有人不介意解释这里发生了什么。我正在尝试运行已确认可以使用 python 2.7 运行的 python unittest。但是,当尝试在运行 python 2.6 的机器上运行相同的测试时,我遇到了一个我无法弄清楚的错误。这是正在发生的事情的示例
import re, string, os, subprocess, unittest
import MERCH_FUNCTIONS
class merchTests(unittest.TestCase):
@classmethod
def setUpClass(self):
self._merchFileString=open("test_file.txt",'r').read()
self._merchFileList=self._merchFileString.split("\n") #convert string to list
def test_stuff(self):
#print list
print(self._merchFileList)
if __name__ == '__main__':
unittest.main()
由于某种原因,如果我使用 python 2.7 运行此代码,它会成功运行测试,并且列表 self._merchFileList 会打印出来。
但是,当使用 python 2.6 运行相同的代码时,我收到以下错误:
======================================================================
ERROR: test_stuff (__main__.merchTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "MERCH_Test_Case.py", line 14, in test_stuff
print(self._merchFileList)
AttributeError: 'merchTests' object has no attribute '_merchFileList'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
我一生都无法弄清楚这里发生了什么。我尝试了几种不同的方法,但都没有成功。如果有人愿意解释这里出了什么问题,我将不胜感激。
提前谢谢你。
【问题讨论】:
标签: python python-2.7 unit-testing python-2.6 class-variables