diff options
author | Leonardo Tarla <41342687+leo8198@users.noreply.github.com> | 2022-08-18 11:57:45 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-18 16:57:45 +0200 |
commit | 760215f0a2552f37b4e9b86b02ef0632bc23d1df (patch) | |
tree | 6cc128b17c8f70a67c5ca67cb82444a0e821bdb9 | |
parent | 6ba0a3a60e3ab85ec85e260fd70b3bc3770ec53c (diff) |
Test code in example folder (#28)
* Unit tests for the examples using pytest
Co-authored-by: Leonardo Ferreira Tarlá <leonardo.tarla@ringa.com.br>
-rw-r--r-- | examples/quotes_to_scrape.py | 1 | ||||
-rw-r--r-- | mlscraper/training.py | 4 | ||||
-rw-r--r-- | tests/test_examples.py | 11 |
3 files changed, 15 insertions, 1 deletions
diff --git a/examples/quotes_to_scrape.py b/examples/quotes_to_scrape.py index 06954a7..291bf7f 100644 --- a/examples/quotes_to_scrape.py +++ b/examples/quotes_to_scrape.py @@ -29,6 +29,7 @@ def main(): resp = requests.get("http://quotes.toscrape.com/author/J-K-Rowling") result = scraper.get(Page(resp.content)) print(result) + return result # returns {'name': 'J.K. Rowling', 'born': 'July 31, 1965'} diff --git a/mlscraper/training.py b/mlscraper/training.py index b71d577..856f5f7 100644 --- a/mlscraper/training.py +++ b/mlscraper/training.py @@ -155,7 +155,9 @@ def train_scraper_for_matches(matches, roots, complexity: int): try: scraper = train_scraper_for_matches(matches_per_key, roots, complexity) except NoScraperFoundException as e: - raise NoScraperFoundException(f'Training DictScraper failed ({k=})') from e + raise NoScraperFoundException( + f"Training DictScraper failed ({k=})" + ) from e scraper_per_key[k] = scraper logging.info(f"found DictScraper ({scraper_per_key=})") return DictScraper(scraper_per_key) diff --git a/tests/test_examples.py b/tests/test_examples.py new file mode 100644 index 0000000..699da18 --- /dev/null +++ b/tests/test_examples.py @@ -0,0 +1,11 @@ +from examples.quotes_to_scrape import main as quotes_to_scrape_main + +# Example 1 - Quotes to scrape +def test_example_quotes_to_scrape(): + ''' + Test if the example quotes_to_scrape.py works + ''' + + assert quotes_to_scrape_main() == \ + {'name': 'J.K. Rowling', 'born': 'July 31, 1965'}, \ + 'Quotes to scrape example failed' |