Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating Unit tests for methods with global variables
    primarykey
    data
    text
    <p>I have a program that uses a number of global variables and I was hoping to write a few unit tests for some of the methods in the program. </p> <p>I was new to python when I started writing the code, and realise now that I should have been testing all along. Some of the methods in the program are as follows:</p> <pre><code>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) </code></pre> <p>What is confusing me is how to variables are passing through to the test class and how I go about writing the testing methods. So far this is what I have:</p> <pre><code>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 &lt;ENTER&gt; to exit") </code></pre> <p>This does not work at the moment and I know I am not doing it the right way but just can't find any specific help for this problem!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload