How to write a python module from a test?

Asked

Viewed 131 times

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?

1 answer

2


It is possible to obtain this result in several ways, this one is one of them. Starting with the class design, or better, classes:

""" GoogleDriver.py """

class GoogleDriver: 
    """ The GoogleDriver ^_^ """ 

    @staticmethod 
    def search(string): 
        """ dummy static method... """ 
        return GoogleDriverResults(string) 


class GoogleDriverResults: 
    """ Search for things """ 

    messages = { 
        "test": "This is a test", 
        "dummy": "It's a dummy message", 
        "dunno": "Put your funny message here", 
    } 

    def __init__(self, search_str): 
        result =  self.messages.get(search_str) 
        if result: 
            self.status_code = 200 
            self.content = result 
        else: 
            self.status_code = 404 
            self.content = "Not found, try again." 

The class GoogleDriver acts kind of like a Factory instantiating the correct class from a static method (the presence of the GoogleDriver().search("test") already denounced what would need to be done).

Of course this is an example and lack enough filling in both but the important thing is that the class GoogleDriverResults effectively does a search and returns a "valid"

Hence it is possible to use the test routine to get the expected result...

============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /tmp
collected 1 item                                                               

test_google_driver.py .                                                  [100%]

============================== 1 passed in 1.75s ===============================
  • 1

    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.

  • 1

    Yeah, the use of self is 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).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.