Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests: move scraper construction from setUp (per-method) to setUpClass (per-case) #668

Merged
merged 1 commit into from Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions tests/__init__.py
Expand Up @@ -11,22 +11,23 @@ class ScraperTest(unittest.TestCase):
test_file_extension = "testhtml"
scraper_class: Any

@property
def expected_requests(self) -> Iterator[Tuple[str, str, str]]:
@classmethod
def expected_requests(cls) -> Iterator[Tuple[str, str, str]]:
"""
Descriptions of the expected requests that the scraper-under-test will make, as
tuples of: HTTP method, URL, path-to-content-file
"""
filename = self.test_file_name or self.scraper_class.__name__.lower()
path = f"tests/test_data/{filename}.{self.test_file_extension}"
filename = cls.test_file_name or cls.scraper_class.__name__.lower()
path = f"tests/test_data/{filename}.{cls.test_file_extension}"
yield responses.GET, "https://test.example.com", path

def setUp(self):
@classmethod
def setUpClass(cls):
with responses.RequestsMock() as rsps:
start_url = None
for method, url, path in self.expected_requests:
for method, url, path in cls.expected_requests():
start_url = start_url or url
with open(path, encoding="utf-8") as f:
rsps.add(method, url, body=f.read())

self.harvester_class = self.scraper_class(url=start_url)
cls.harvester_class = cls.scraper_class(url=start_url)
5 changes: 1 addition & 4 deletions tests/test_farmhousedelivery_1.py
Expand Up @@ -5,10 +5,7 @@
class TestFarmhouseDeliveryScraper(ScraperTest):

scraper_class = FarmhouseDelivery

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "farmhousedelivery_1"

def test_host(self):
self.assertEqual("recipes.farmhousedelivery.com", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_farmhousedelivery_2.py
Expand Up @@ -5,10 +5,7 @@
class TestFarmhouseDeliveryScraper(ScraperTest):

scraper_class = FarmhouseDelivery

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "farmhousedelivery_2"

def test_host(self):
self.assertEqual("recipes.farmhousedelivery.com", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_finedininglovers_1.py
Expand Up @@ -5,10 +5,7 @@
class TestFineDiningLoversScraper(ScraperTest):

scraper_class = FineDiningLovers

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "finedininglovers_1"

def test_host(self):
self.assertEqual("finedininglovers.com", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_finedininglovers_2.py
Expand Up @@ -5,10 +5,7 @@
class TestFineDiningLoversScraper(ScraperTest):

scraper_class = FineDiningLovers

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "finedininglovers_2"

def test_host(self):
self.assertEqual("finedininglovers.com", self.harvester_class.host())
Expand Down
4 changes: 2 additions & 2 deletions tests/test_foodnetwork.py
Expand Up @@ -8,8 +8,8 @@ class TestFoodNetworkScraper(ScraperTest):

scraper_class = FoodNetwork

@property
def expected_requests(self):
@classmethod
def expected_requests(cls):
yield GET, "https://www.foodnetwork.com/recipes/tyler-florence/chicken-marsala-recipe-1951778", "tests/test_data/foodnetwork.testhtml"

def test_host(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gousto.py
Expand Up @@ -8,8 +8,8 @@ class TestGoustoScraper(ScraperTest):

scraper_class = Gousto

@property
def expected_requests(self):
@classmethod
def expected_requests(cls):
yield GET, "https://gousto.co.uk/cookbook/creamy-pork-tagliatelle", "tests/test_data/gousto.testhtml"

def test_host(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_goustojson.py
Expand Up @@ -8,8 +8,8 @@ class TestGoustoScraper(ScraperTest):

scraper_class = GoustoJson

@property
def expected_requests(self):
@classmethod
def expected_requests(cls):
yield GET, "https://www.gousto.co.uk/cookbook/recipes/malaysian-style-coconut-meat-free-chicken-pickled-cucumber", "tests/test_data/gousto.testjson"
yield GET, "https://production-api.gousto.co.uk/cmsreadbroker/v1/recipe/malaysian-style-coconut-meat-free-chicken-pickled-cucumber", "tests/test_data/gousto.testjson"

Expand Down
5 changes: 1 addition & 4 deletions tests/test_greatbritishchefs_1.py
Expand Up @@ -5,10 +5,7 @@
class TestGreatBritishChefsScraper(ScraperTest):

scraper_class = GreatBritishChefs

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "greatbritishchefs_1"

def test_host(self):
self.assertEqual("greatbritishchefs.com", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_greatbritishchefs_2.py
Expand Up @@ -5,10 +5,7 @@
class TestGreatBritishChefsScraper(ScraperTest):

scraper_class = GreatBritishChefs

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "greatbritishchefs_2"

def test_host(self):
self.assertEqual("greatbritishchefs.com", self.harvester_class.host())
Expand Down
4 changes: 2 additions & 2 deletions tests/test_kptncook.py
Expand Up @@ -8,8 +8,8 @@ class TestKptnCookScraper(ScraperTest):

scraper_class = KptnCook

@property
def expected_requests(self):
@classmethod
def expected_requests(cls):
yield responses.GET, "https://mobile.kptncook.com/recipe/pinterest/Low-Carb-Tarte-Flamb%C3%A9e-with-Serrano-Ham-%26-Cream-Cheese/315c3c32?lang=en", "tests/test_data/kptncook.testhtml"
yield responses.POST, "https://mobile.kptncook.com/recipes/search?kptnkey=6q7QNKy-oIgk-IMuWisJ-jfN7s6&lang=en", "tests/test_data/kptncook.testjson"

Expand Down
4 changes: 2 additions & 2 deletions tests/test_marleyspoon.py
Expand Up @@ -8,8 +8,8 @@ class TestMarleySpoonScraper(ScraperTest):

scraper_class = MarleySpoon

@property
def expected_requests(self):
@classmethod
def expected_requests(cls):
yield responses.GET, "https://marleyspoon.de/menu/113813-glasierte-veggie-burger-mit-roestkartoffeln-und-apfel-gurken-salat", "tests/test_data/marleyspoon.testhtml"
yield responses.GET, "https://api.marleyspoon.com/recipes/113813?brand=ms&country=de&product_type=web", "tests/test_data/marleyspoon.testjson"

Expand Down
5 changes: 1 addition & 4 deletions tests/test_panelinha_1.py
Expand Up @@ -5,10 +5,7 @@
class TestPanelinhaScraper(ScraperTest):

scraper_class = Panelinha

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "panelinha_1"

def test_host(self):
self.assertEqual("panelinha.com.br", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_panelinha_2.py
Expand Up @@ -4,10 +4,7 @@

class TestPanelinhaScraper(ScraperTest):
scraper_class = Panelinha

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "panelinha_2"

def test_host(self):
self.assertEqual("panelinha.com.br", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_reishunger_1.py
Expand Up @@ -5,10 +5,7 @@
class TestReishungerScraper(ScraperTest):

scraper_class = Reishunger

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "reishunger_1"

def test_host(self):
self.assertEqual("reishunger.de", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_reishunger_2.py
Expand Up @@ -4,10 +4,7 @@

class TestReishungerScraper(ScraperTest):
scraper_class = Reishunger

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "reishunger_2"

def test_host(self):
self.assertEqual("reishunger.de", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_seriouseats_1.py
Expand Up @@ -5,10 +5,7 @@
class TestSeriousEats(ScraperTest):

scraper_class = SeriousEats

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "seriouseats_1"

def test_host(self):
self.assertEqual("seriouseats.com", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_seriouseats_2.py
Expand Up @@ -5,10 +5,7 @@
class TestSeriousEats(ScraperTest):

scraper_class = SeriousEats

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "seriouseats_2"

def test_host(self):
self.assertEqual("seriouseats.com", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_thehappyfoodie_1.py
Expand Up @@ -5,10 +5,7 @@
class TestTheHappyFoodie(ScraperTest):

scraper_class = TheHappyFoodie

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "thehappyfoodie_1"

def test_host(self):
self.assertEqual("thehappyfoodie.co.uk", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_thehappyfoodie_2.py
Expand Up @@ -5,10 +5,7 @@
class TestTheHappyFoodie(ScraperTest):

scraper_class = TheHappyFoodie

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "thehappyfoodie_2"

def test_host(self):
self.assertEqual("thehappyfoodie.co.uk", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_thespruceeats_1.py
Expand Up @@ -5,10 +5,7 @@
class TestTheSpruceEatsScraper(ScraperTest):

scraper_class = TheSpruceEats

@property
def test_file_name(self):
return "{}_1".format(self.scraper_class.__name__.lower())
test_file_name = "thespruceeats_1"

def test_host(self):
self.assertEqual("thespruceeats.com", self.harvester_class.host())
Expand Down
5 changes: 1 addition & 4 deletions tests/test_thespruceeats_2.py
Expand Up @@ -5,10 +5,7 @@
class TestTheSpruceEatsScraper(ScraperTest):

scraper_class = TheSpruceEats

@property
def test_file_name(self):
return "{}_2".format(self.scraper_class.__name__.lower())
test_file_name = "thespruceeats_2"

def test_host(self):
self.assertEqual("thespruceeats.com", self.harvester_class.host())
Expand Down
5 changes: 3 additions & 2 deletions tests/test_wild_mode.py
Expand Up @@ -6,9 +6,10 @@ class TestWildMode(ScraperTest):

scraper_class = SchemaScraperFactory

def setUp(self):
@classmethod
def setUpClass(cls):
with open("tests/test_data/wild_mode.testhtml", encoding="utf-8") as testfile:
self.harvester_class = self.scraper_class.generate(
cls.harvester_class = cls.scraper_class.generate(
url="https://test.example.com/", html=testfile.read()
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_woolworths.py
Expand Up @@ -8,8 +8,8 @@ class TestWoolworthsScraper(ScraperTest):

scraper_class = Woolworths

@property
def expected_requests(self):
@classmethod
def expected_requests(cls):
yield GET, "https://www.woolworths.com.au/shop/recipes/asparagus-salad-with-lemon-vinaigrette", "tests/test_data/woolworths.testhtml"
yield GET, "https://foodhub.woolworths.com.au/content/woolworths-foodhub/en/asparagus-salad-with-lemon-vinaigrette.model.json", "tests/test_data/woolworths.testhtml"

Expand Down