lundi 10 avril 2017

Monkey patching a function in python

I have a function ubuntu_data which, using, urllib and bs4 scrapes data from a website. I'd like to test this function, and to not have it scrape the website every time the test is run I'd like to pass in fake data for it to scrape and process the results.

I'd like to monkey patch this fake data as a parameters in bs4, in ubuntu_data i.e.

soup = bs.BeautifulSoup (fake_data, "lxml")

I have the fake data in my test file and the scraper in another.

This is what I have done so far in the test function:

def test_ubuntu_accuracy(monkeypatch):
"""Tests if data output for Ubuntu is, as expected"""

from urllib import urlopen

monkeypatch.setattr("urllib.urlopen", ubuntu_fake_data)

ubuntu_data() #This returns a list, vuln_status

assert vuln_status[0] == "low" 

There is something I am doing horribly wrong,here. Would love if someone could point out what.

Thanks!

Aucun commentaire:

Enregistrer un commentaire