dimanche 29 mai 2016

How to use mocks in Python?

I know that there are tutorials and other posts about mocks in Python but I'm new to testing and mocks, I read some posts and watched a tutorial but they confused me more. I have two examples that I just wrote fast and I wanna know how can I use mocks to test two examples.

In the first example test the the value that gets returned and in the second one test that a file gets created if we create a new instance of the 'MyFile' class.

1:

def get_link_tags(url):
    response = requests.get(url)

    pattern = re.compile("<link.*>")
    found_link_tags = pattern.findall(response.text)
    return found_link_tags

2:

class MyFile:
    def __init__(self, filename, content):
        self.content = content

        with open(filename, "w") as fl:
            fl.write(content)

Thank you

Aucun commentaire:

Enregistrer un commentaire