I created a python unit test for my word occurence Gui project, I want to test the occurrence of the top 5 words so it should return a true value however I can't figure out how to run the unit test? I'm trying to use idle shell but should I use the visual studio command prompt instead or is the problem with my unit test not being set up properly? I'll display the code below in case you need it for the task:
#Imports
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from collections import Counter
from tkinter import messagebox
import collections
import unittest
# Initialize the dictionary
wordcount = {}
#Unit Test
class TestWordCount(unittest.TestCase):
def test_count_words(self):
n_print = 5
expected_result = {
'the' : 731,
'and' : 565,
'to' : 379,
'of' : 342,
'i' : 313
}
counter = n_print(int)
result = counter.count_words()
assert len(result) == len(expected_result)
assert result == expected_result
unittest.Word_Occurence_GUI().run(TestWordCount())
#open Macbeth text file
file = open('Macbeth Entire Play.txt', encoding="utf8")
a= file.read()
class Application(tk.Frame):
def __init__(self, master):
super().__init__() # Call __init__() method in parent (tk.Frame)
self.label = tk.Button(self, text='How many words to Sort?', command=self.ask_count)
self.label.grid(row=0)
self.open_btn = tk.Button(text='Compute', command=self.ask_count)
self.open_btn.pack(pady=(30,10))
self.exit_btn = tk.Button(text='Exit', command=master.destroy)
self.exit_btn.pack()
def ask_count(self):
with open('Macbeth Entire Play.txt', encoding="utf8") as file:
self.file_text = file.read()
for word in a.lower().split():
word = word.replace(".","")
word = word.replace(",","")
word = word.replace(":","")
word = word.replace("\"","")
word = word.replace("!","")
word = word.replace("“","")
word = word.replace("‘","")
word = word.replace("*","")
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
n_print = int(input("How many most common words are: "))
print("\nThe {} most common words are as follows\n".format(n_print))
word_counter = collections.Counter(wordcount)
for word, count in word_counter.most_common(n_print):
print(word, ": ", count)
messagebox.showinfo("Top words...", "The top words are: \n" + "\n".join([(str(word)+": "+str(count)) for word, count in word_counter.most_common(n_print)]))
# Close the file
file.close()
messagebox.showinfo("The top words are: ")
if __name__ == '__main__':
root = tk.Tk()
root.title("Count words")
root.geometry('400x400+900+50')
app = Application(root)
app.pack(expand=True, fill='both')
root.mainloop()
#run unit test
unittest.main()