2013-07-01 80 views
4

我有一個使用大量全局變量的程序,我希望爲程序中的某些方法編寫幾個單元測試。爲具有全局變量的方法創建單元測試

當我開始編寫代碼時,我對Python很陌生,現在意識到我應該一直在測試。有一些在程序中的方法如下:

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") 

這並不是目前的工作,我知道我沒有做正確的方式,但就是無法找到這個問題的任何具體的幫助!

回答

3

這裏是一個可運行的例子,讓你開始:

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 = '[email protected]' 
     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() 
+0

可以在測試類在不同的文件中完成或者是很容易做的這種方式? – Johnnerz

+0

測試類應該位於不同的文件中;爲了讓這個例子更簡單,我把它放在一起。 – unutbu