gpt4 book ai didi

Python Unittest-类变量

转载 作者:行者123 更新时间:2023-11-28 21:07:36 25 4
gpt4 key购买 nike

我希望有人不介意解释这里发生了什么。我正在尝试运行已确认可以使用 python 2.7 工作的 python 单元测试。但是,当尝试在运行 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)

我一辈子都弄不明白这里发生了什么。我尝试了几种不同的方法但没有成功。如果有人愿意解释这里出了什么问题,我将不胜感激。

提前谢谢你。

最佳答案

setUpClassintroduced in python2.7 .因此,当您使用早期版本(例如 Python 2.6)运行它时,它不会被自动调用。

关于Python Unittest-类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41225610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com