Python 为具有全局变量的方法创建单元测试
我有一个使用许多全局变量的程序,我希望为程序中的一些方法编写一些单元测试 当我开始编写代码时,我是python新手,现在意识到我应该一直在测试。程序中的一些方法如下所示:Python 为具有全局变量的方法创建单元测试,python,unit-testing,python-unittest,Python,Unit Testing,Python Unittest,我有一个使用许多全局变量的程序,我希望为程序中的一些方法编写一些单元测试 当我开始编写代码时,我是python新手,现在意识到我应该一直在测试。程序中的一些方法如下所示: class Wordnet(): def __init__(self): self.graph = Graph() self.before_at = '' self.after_at = '' self.word_part = '' se
class Wordnet():
def __init__(self):
self.graph = Graph()
self.before_at = ''
self.after_at = ''
self.word_part = ''
self.gloss_part = ''
self.lex_filenum = ''
def process_file(self):
self.file = open("testing_line.txt", "r")
return self.file
def line_for_loop(self, file):
for line in file:
self.split_pointer_part(line)
self.split_word_part(line)
self.split_gloss_part(line)
self.process_lex_filenum(self.word_part)
def split_pointer_part(self, line):
self.before_at, self.after_at = line.split('@', 1)
return self.before_at, self.after_at
def split_word_part(self, line):
self.word_part = line.split()
return self.word_part
def split_gloss_part(self, line):
self.gloss_part = line.strip().split('|')
return self.gloss_part
def process_lex_filenum(self, word_part):
self.lex_filenum = word_part[1]
return self.lex_filenum
if __name__ == '__main__':
wordnet = Wordnet()
my_file = wordnet.process_file()
wordnet.line_for_loop(my_file)
让我困惑的是如何将变量传递给测试类,以及如何编写测试方法。到目前为止,这就是我所拥有的:
class WordnetTestCase(unittest.TestCase):
def setUp(self):
self.wn = wordnet.Wordnet()
self.graph = wordnet.Graph()
self.before_at = wordnet.before_at
self.after_at = wordnet.after_at
self.word_part = wordnet.word_part
self.gloss_part = wordnet.gloss_part
self.lex_filenum = wordnet.lex_filenum
def test_split_pointer_part(line):
expected = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005',' 13796604 n 0000 + 00603894 a 0401 + 00753137 v 0302 + 01527311 v 0203 + 02361703 v 0101 | an overwhelming number or amount; "a flood of requests"; "a torrent of abuse"'
real = self.wn.split_pointer_part()
self.assertEqual(real, expected)
if __name__ == '__main__':
unittest.main()
raw_input("Press <ENTER> to exit")
类WordnetTestCase(unittest.TestCase):
def设置(自):
self.wn=wordnet.wordnet()
self.graph=wordnet.graph()
self.before\u at=wordnet.before\u at
self.after\u at=wordnet.after\u at
self.word\u part=wordnet.word\u part
self.gloss\u part=wordnet.gloss\u part
self.lex_filenum=wordnet.lex_filenum
def测试分割指针部分(行):
预期='13797906 23 n 04洪水0洪水0洪水0洪水0洪水0急流0 005','13796604 n 0000+00603894 a 0401+00753137 v 0302+01527311 v 0203+02361703 v 0101 |一个压倒性的数字或数量;“大量的请求”;“一连串的辱骂”
real=self.wn.split\u pointer\u part()
self.assertEqual(真实、预期)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
unittest.main()
原始输入(“按下退出”)
这在目前还不起作用,我知道我做得不对,但就是找不到解决这个问题的任何具体帮助 下面是一个可运行的示例,让您开始:
import unittest
class Wordnet():
def __init__(self):
# self.graph = Graph()
self.before_at = ''
self.after_at = ''
self.word_part = ''
self.gloss_part = ''
self.lex_filenum = ''
def process_file(self):
self.file = open("testing_line.txt", "r")
return self.file
def line_for_loop(self, file):
for line in file:
self.split_pointer_part(line)
self.split_word_part(line)
self.split_gloss_part(line)
self.process_lex_filenum(self.word_part)
def split_pointer_part(self, line):
self.before_at, self.after_at = line.split('@', 1)
return self.before_at, self.after_at
def split_word_part(self, line):
self.word_part = line.split()
return self.word_part
def split_gloss_part(self, line):
self.gloss_part = line.strip().split('|')
return self.gloss_part
def process_lex_filenum(self, word_part):
self.lex_filenum = word_part[1]
return self.lex_filenum
class WordnetTestCase(unittest.TestCase):
def setUp(self):
self.wn = Wordnet()
def test_split_pointer_part(self):
line = 'foo@bar'
result = self.wn.split_pointer_part(line)
answer = ('foo', 'bar')
self.assertEqual(len(result), 2)
for r, a in zip(result, answer):
self.assertEqual(r, a)
if __name__ == '__main__':
unittest.main()
测试类可以在不同的文件中完成吗?或者这样做更容易吗?测试类应该在不同的文件中;我把它们放在一起只是为了使例子更简单。