Skip to content

Commit

Permalink
Tests: prepare test cases at module import-time
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison committed Oct 25, 2024
1 parent 9b9e958 commit e1fc342
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,41 @@ def test_func(self):
return test_func


def prepare_test_cases():
"""
This function dynamically generates the class definition for RecipeTestCase by adding
a test function for each pair of testhtml and testjson files found in the
tests/test_data directory.
"""
test_dir = pathlib.Path("tests/test_data")
for host in test_dir.iterdir():
if not host.is_dir():
continue

for testhtml in host.glob("*.testhtml"):
testjson = testhtml.with_suffix(".json")
if not testjson.is_file():
continue

# Add a new function to RecipeTestCase class to test this scraper
# The name of this function the path to the testjson file.
setattr(
RecipeTestCase,
str(testjson),
test_func_factory(host.name, testhtml, testjson),
)


prepare_test_cases()


def load_tests(
loader: unittest.TestLoader, standard_tests: unittest.TestSuite, pattern: str
) -> unittest.TestSuite:
"""
Customise the loading of tests. This function is automatically picked up by the
unittest test loader.
This function dynamically generates the class definition for RecipeTestCase by adding
a test function for each pair of testhtml and testjson files found in the
tests/test_data directory.
This also includes the library tests from the tests/library folder as well.
Expand All @@ -197,23 +221,6 @@ def load_tests(
A TestSuite object populated with tests from the pairs of testhtml and testjson
files, and the library tests.
"""
test_dir = pathlib.Path("tests/test_data")
for host in test_dir.iterdir():
if not host.is_dir():
continue

for testhtml in host.glob("*.testhtml"):
testjson = testhtml.with_suffix(".json")
if not testjson.is_file():
continue

# Add a new function to RecipeTestCase class to test this scraper
# The name of this function the path to the testjson file.
setattr(
RecipeTestCase,
str(testjson),
test_func_factory(host.name, testhtml, testjson),
)

# Create a test suite and load all tests from the RecipeTestClass definition
suite = unittest.TestSuite()
Expand Down

0 comments on commit e1fc342

Please sign in to comment.