1
I’m doing an inverse exercise on Python modules and tests with pytest, but I’m not succeeding. Whenever I run the code I get the message "E Attributeerror: 'str' Object has no attribute 'status_code' ".
In case, I must write the necessary code for the test to work and not the other way around.
I have a test file called "test_google_driver.py", which was given and a file with the module that will be tested by this test, the "google_driver.py", which I created to try to make the module that will be tested.
#arquivo test_google_driver.py
from google_driver import GoogleDriver
def test_requests():
result = GoogleDriver().search("test")
assert 200 == result.status_code
assert "test" in str(result.content)
google_driver.py file
class GoogleDriver():
def __init__(self):
self.result = "test"
def search(self, string):
return self.result
@property
def status_code(self):
return self.__status_code
@status_code.setter
def status_code(self, integer):
self.status_code = 200
@property
def content(self):
return self.__content
@content.setter
def content(self, string):
self.content = "test"
By the test code that was given I understand that I need to create the Googledriver class, which will have the search method that receives the parameter of a string and returns a result object; and the status_code properties (which must have a value of 200) and content (which must have a string as well).
How can I make my module work?
Thank you very much, now yes the test ran. You used a lot of things that I don’t know. I’m still a beginner in this study. I thought it was mandatory to use the self in all methods of the class.
– Alineat
Yeah, the use of
selfis indeed mandatory but in the case of Static method and also of class method it will be added precisely by the corresponding decorator (the@staticmethodand@classmethod, respectively).– Giovanni Nunes