Andna
Andna

Reputation: 6689

Python - unit testing class that extracts data from html pages

I am writing small application in Python and one module is responsible for downloading and parsing web pages using Beautiful Soup for parsing and urllib2 for page downloading.

I am wondering now, how could I write unit tests for the class responsible for parsing the pages.

I could create some abstraction around urllib2 and inject it to the classes that are responsible for parsing (my plan is to have different classes for different webpages) and during testing I would inject an object that will mock this abstraction. But I am not so sure about this way. I would only create an abstraction because it will be easier to write the tests not because the abstraction is needed (at least that is what I think for now).

Also I would then have a problem how to test this abstraction, it would need internet connection to work properly and when I would run the tests how could I assert that the proper page was downloaded?

Upvotes: 0

Views: 135

Answers (1)

Jean
Jean

Reputation: 36

To implement unit tests in python, you should consider using Mocks and patches: http://www.voidspace.org.uk/python/mock/

Upvotes: 1

Related Questions