0

I'm trying to create unit tests for my function, so the project was this: for week 1 we were supposed to create a function that counts the occurrence of x words and displays the top x words of that count. For the second week we were supposed to create a GUI for that program (both of which I have completed). And now for the third week the goal is to create unit tests for the project. I've read the textbook about what a unit test is however it didn't go into detail for how to run a unit test or how to create one for this particular project (the ones in the module text were very vague).

Here's my program with the gui element that I'm creating the unit tests for:

#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 = {}
        

#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()

And here's the unit test I was told to make a separate post for:

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())
#run unit test
    unittest.main()

I would also appreciate if someone could explain what's wrong with the Unit Test I have created.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • What sort of problems are you having with your unit test? Is it not running? Are you getting unexpected errors? Is it working when you think it should be producing an error? – larsks Mar 05 '21 at 03:36
  • @Iarsks, the problem is it's not running or I don't understand how to run it, I would appreciate some help I talked to another user who told me that I should create this question as I seem to have a knowledge gap when it comes to making unit tests, – Fallen Dionysus Mar 05 '21 at 03:37
  • @Iarsks, here's the link to my original post if that provides you some more context https://stackoverflow.com/questions/66479969/how-to-run-unit-test-in-python-idle/66485193#66485193 – Fallen Dionysus Mar 05 '21 at 03:37
  • 1
    I don't see where in your unit tests you're trying to call your application code. Also, just looking at what's here in your question, you're setting `n_print=5`, so it's an integer, but then you're writing `n_print(int)` as if it were some sort of function. – larsks Mar 05 '21 at 03:38

0 Answers0