Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Test Case Assertion Error
#1
Good evening,

I am running the unittest in Python for my code and I am getting errors/Failures. I am unsure how to fix it, or what the errors mean exactly. Can someone explain to me, please? I would really appreciate it.

One Failure is listed below:

def discover_path(self):
        self._src.endswith('.txt')
        src_type = 'path'
        with open('pride-and-prejudice.txt') as f:
            self._content = self._orig_content
Error:
FAIL: test_discover_path (__main__.TestTextAnalyzer) ---------------------------------------------------------------------- Traceback (most recent call last): File "<ipython-input-48-bce1ccf365eb>", line 16, in test_discover_path self.assertEqual(ta._src_type, 'path') AssertionError: 'discover' != 'path' - discover + path
Quote
#2
The error means that ta._src_type is equal to 'discover', and that 'discover' does not equal the expected value of 'path'.

My guess is that line 3 should be self._src_type = 'path', but you really haven't given us enough information. We don't know how the test is written, and we don't know the other content of the class for ta that may be affecting the _src_type attribute.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
ichabod801 - ok, I understand what you are saying. But, I am still trying to figure out if the discover is supposed to be as def discover_path(self), then how do I get it to recognize the self._src as path? The code is supposed to be written to accept any website, file, or text listed below. But I found if I used an if/else it gave me an error. Thank you for your help! It is greatly appreciated!

Here is the snipit of the code where this starts:
import requests, re
from bs4 import BeautifulSoup
from collections import Counter
import statistics as stats
import string
import operator
import matplotlib.pyplot as plt
plt.rcdefaults()

class TextAnalyzer():
    "A Text Analyzer"
    def __init__(self, src, src_type='discover'):   
        """Creates a object for analyzing text
    
        Keyword arguments:
        src (str) -- text, path to file, or url
        src_type (str) -- The type of input (text, path, url, discover)"""

       
        if isinstance(src, str) == False or len(src) <= 0:
            raise exception('Source must be a valid string, filepath or a valid URL')

        self._src = src
        self._src_type = src_type
        self._content = None
        self._orig_content = None


    def discover_url(self):
        self._src.startswith('http')
        self._src_type = 'url'
        url = 'https://www.webucator.com/how-to/address-by-bill-clinton-1997.cfm'
        r = requests.get(self._src)
        res = r.content
        self._orig_content = r.text
        self._content = res

    def discover_path(self):
        self._src.endswith('.txt')
        src_type = 'path'
        with open('pride-and-prejudice.txt') as f:
            self._content = self._orig_content
     

    def discover_text(self):
        src_type = 'text'
        text = ("The outlook wasn't brilliant for the Mudville Nine that day;the score stood four to two, with but one inning more to play. And then when Cooney died at first, and Barrows did the same, a sickly silence fell upon the patrons of the game.")
        self._orig_content = self._src
        self._content = self._src

Here is the test code for this part:

import unittest

url = 'https://www.webucator.com/how-to/address-by-bill-clinton-1997.cfm'
path = 'pride-and-prejudice.txt'
text = '''The outlook wasn't brilliant for the Mudville Nine that day;
the score stood four to two, with but one inning more to play.
And then when Cooney died at first, and Barrows did the same,
a sickly silence fell upon the patrons of the game.'''

class TestTextAnalyzer(unittest.TestCase):
    def test_discover_url(self):
        ta = TextAnalyzer(url)
        self.assertEqual(ta._src_type, 'url')
    def test_discover_path(self):
        ta = TextAnalyzer(path)
        self.assertEqual(ta._src_type, 'path')
    def test_discover_text(self):
        ta = TextAnalyzer(text)
        self.assertEqual(ta._src_type, 'text')
Quote
#4
You need to set _src_type based on _src, but you're not doing that. You do need an if/elif/else. The clauses you need are on lines 30 and 39. You probably need to read in the content as well. I'm not sure why you have the test cases in your code (line 32) and sometimes overriding any other input (line 41, where you should be using self._src). Also line 42 needs to read the content from the file. Currently it is setting self._content to None.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
ok, I have re-written it. I am a little uncertain if it should be get_values or set_values. When I run the test code, it still returns the same errors.



import requests, re
from bs4 import BeautifulSoup
from collections import Counter
import statistics as stats
import string
import operator
import matplotlib.pyplot as plt
plt.rcdefaults()

class TextAnalyzer():
    "A Text Analyzer"
    def __init__(self, src, src_type='discover'):   
        """Creates a object for analyzing text
    
        Keyword arguments:
        src (str) -- text, path to file, or url
        src_type (str) -- The type of input (text, path, url, discover)"""

       
        if isinstance(src, str) == False or len(src) <= 0:
            raise exception('Source must be a valid string, filepath or a valid URL')

        self._src = src
        self._src_type = src_type
        self._content = []
        self._orig_content = []


    def get_values(self):
      
        if self._src.endswith('.txt'):
            self._src_type = 'path'
            self._content = self.read_file(self._src)
        
        elif self._src.startswith('http'):
            self._src_type = 'url'
            r = requests.get(self._src)
            res = r.content
            self._orig_content = r.text
            self._content = res
        else:
            self._src_type = 'text'
            self._orig_content = self._src
            self._content = self._src

Error:
FAIL: test_discover_path (__main__.TestTextAnalyzer) ---------------------------------------------------------------------- Traceback (most recent call last): File "<ipython-input-13-bce1ccf365eb>", line 16, in test_discover_path self.assertEqual(ta._src_type, 'path') AssertionError: 'discover' != 'path' - discover + path FAIL: test_discover_text (__main__.TestTextAnalyzer) ---------------------------------------------------------------------- Traceback (most recent call last): File "<ipython-input-13-bce1ccf365eb>", line 19, in test_discover_text self.assertEqual(ta._src_type, 'text') AssertionError: 'discover' != 'text' - discover + text FAIL: test_discover_url (__main__.TestTextAnalyzer) ---------------------------------------------------------------------- Traceback (most recent call last): File "<ipython-input-13-bce1ccf365eb>", line 13, in test_discover_url self.assertEqual(ta._src_type, 'url') AssertionError: 'discover' != 'url' - discover + url
Quote
#6
Where do you call get_values? You should be doing that in __init__, but I don't see it anywhere.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#7
so you're saying I don't need line 29? That this if\else\elif should be right underneath the _init_ statement?

I was writing this according to the specifics below. I guess reading it wrong because what you said makes sense. According to the instructions below, I thought I had to have def_discover. This is where I am getting confused on where/how to put it.

__init__()
TextAnalyzer objects are instantiated by passing in one of the following to the src parameter:
A valid URL beginning with "http"
A path to a text file ending with the file extension "txt"
A string of text
The __init__() method also includes a src_type parameter, which is used to specify the type of the src argument. Options are:
discover (default) - You must write code to discover the type of src.
If the src begins with "http", it is a url.
If the src ends in "txt", it is a path.
Otherwise, it is text.
url
path
text
You should set self._src_type, self._content, and self._orig_content in the __init__() method.
Quote
#8
Based on those specifications you should get rid of line 29. You need to replace it with an if statement to check if _src_type is discover. Then indent all the code currently in get_values so that it is under the if statement.

If you use def to define a function or method, that just stores the code to be run later. It is not run until you call the function or method.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#9
Ok I have removed line 29. Out of all the things I have read online, your explanation is by far the clearest. Thank you so much!

When I try to put it in the if statement, I get the following:
It is telling me I need to define 'discover' in the if statement if I write it this way:
self._src_type(discover):

If I write it this way: self._src_type('discover'):

error is 'str' not callable


How (and this might sound naive) do I know if it needs to be self._src_type or just src._type? Its very confusing from the way it is written on line 21 with self._src_type = src._type from _init_. Is that not correct? Should it be written as self._src_type = src._type(discover)or should it be written as self._src_type = src._type = 'discover'? I am sure there is a huge difference, but if someone could explain it to me, I would appreciate it.

Thank you!
Quote
#10
(Mar-11-2019, 03:04 AM)moga2003 Wrote: If I write it this way: self._src_type('discover'):

Why are you writing that? You need to check if self._src_type is equal to 'discover'. You use the comparison operator (==) for that, not a function call.

I don't know what you are talking about with the variable names. src_type is a parameter to you __init__ method. It is defined in the def line as having a default value of 'discover', which is only used if no value is supplied to that parameter. self._src_type is an instance attribute that is set to the value associated with the src_type parameter by the __init__ code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Assertion Error Mateoo 2 299 Jan-20-2019, 03:59 PM
Last Post: stullis
  error: (-215:Assertion failed) gkiller007 1 2,893 Jan-04-2019, 04:27 AM
Last Post: stullis
  pytest fixture in conftest.py thrown error while in the test file runs OzzieOzzum 1 697 Jul-31-2018, 12:12 PM
Last Post: OzzieOzzum
  Is this a normal/benign make test error when building python3.6 sofuego 2 1,025 Feb-12-2018, 12:32 AM
Last Post: sofuego

Forum Jump:


Users browsing this thread: 1 Guest(s)