Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I've just started learning the unit test module for python and need help. I have the following files wordcount.py, histogram.py, word_test.py and text.txt. All im

I've just started learning the unit test module for python and need help.

I have the following files wordcount.py, histogram.py, word_test.py and text.txt.

All im wanting to do is test methods with various test cases 'word_test.py'.

For f1 function, try empty filename and wrong file name.

For f2 function, try input types other than dictionary, empty dictionary.

For the function f3, I need to test the code somehow.

Here is the code in wordcount.py:

import sys import string def f1(filename): try: fhand = open(filename) except: print("cannot open", filename) fhand.close() return [] counts = {} words = fhand.read().split() if len(words) == 0: fhand.close() else: for word in words: translator = str.maketrans('', '', string.punctuation) w = word.upper().translate(translator) if (len(w) > 0): counts[w] = counts.get(w, 0) + 1 fhand.close() return counts def f2(counts): if len(counts) == 0: return [] lst = [] for key, val in counts.items(): newtup = (val, key) lst.append(newtup) lst = sorted(lst, reverse=True) return lst 

Here is the code in histogram.py:

import sys import string import wordcount def f3(lst): total = 0 for cnt, label in lst: total += cnt # produce a histogram for cnt, label in lst: print(label.ljust(15), "\t[", sep='', end='') per = int(cnt) * 100 // total for i in range(0, per, 5): print("*", sep='', end='') print("] ", per, "%", sep='') counts = wordcount.f1('text.txt') slist = wordcount.f2(counts) f3(slist) 

Here is what I have tried to do so far in word_test.py file. But I haven't had any success yet:

import wordcount import histogram import unittest counts = wordcount.f1('text.txt') slist = wordcount.f2(counts) histogram.f3(slist) class Test_word_count(unittest.TestCase): def test_f1(self): result = wordcount.f1('text.txt') self.assertEqual(result) unittest.main() 

text.txt file:

'How much wood would a woodchuck chuck if a woodchuck could chuck wood.'

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions