Best Python code snippet using avocado_python
test_wikipedia_html_parsers.py
Source:test_wikipedia_html_parsers.py  
...20    '/wiki/Rebecca_Sugar' to make sure the article is added to21    _WikipediaArticleParser.articles22    """23    article_parser = wikipedia_html_parsers.WikipediaArticleParser('')24    article_parser.handle_starttag('a', [('href', '/wiki/Rebecca_Sugar')])25    assert article_parser.articles == ['https://en.wikipedia.org/wiki/Rebecca_Sugar']26def test_wap_wrong_attrs() -> None:27    """Test _WikipediaArticleParser.handle_starttag with an <a> tag28    with the following attributes: [('href', '/wiki/Rebecca_Sugar')],29    to make sure tags with no 'href' attribute are ignored"""30    article_parser = wikipedia_html_parsers.WikipediaArticleParser('')31    article_parser.handle_starttag('a', [('id', '/wiki/Rebecca_Sugar')])32    assert article_parser.articles == []33def test_wap_wrong_tag() -> None:34    """Test _WikipediaArticleParser.handle_starttag with an <p> tag to make35    sure tags that aren't <a> tags are ignored"""36    article_parser = wikipedia_html_parsers.WikipediaArticleParser('')37    article_parser.handle_starttag('p', [('id', '/wiki/Rebecca_Sugar')])38    assert article_parser.articles == []39def test_wap_not_wiki() -> None:40    """Test _WikipediaArticleParser.handle_starttag with an <a> and the41    following attributes: [('href', 'google.com')] to make sure non wikipedia42    articles are ignored"""43    article_parser = wikipedia_html_parsers.WikipediaArticleParser('')44    article_parser.handle_starttag('a', [('href', 'google.com')])45    assert article_parser.articles == []46def test_wap_not_unwanted_url() -> None:47    """Test _WikipediaArticleParser.handle_starttag with an unwanted article48    to make sure _WikipediaArticleParser.articles does not updated"""49    article_parser = wikipedia_html_parsers.WikipediaArticleParser('')50    article_parser.handle_starttag('a', [('href', '/wiki/Help:Contents')])51    assert article_parser.articles == []52def test_wap_no_duplicates() -> None:53    """Test _WikipediaArticleParser.handle_starttag to make sure the same article54    can not be added to _WikipediaArticleParser.articles twice"""55    article_parser = wikipedia_html_parsers.WikipediaArticleParser('')56    article_parser.handle_starttag('a', [('href', '/wiki/Rebecca_Sugar')])57    article_parser.handle_starttag('a', [('href', '/wiki/Rebecca_Sugar')])58    assert article_parser.articles == ['https://en.wikipedia.org/wiki/Rebecca_Sugar']59# ==================================================================================================60# TEST _WikipediaSummaryParser61# ==================================================================================================62def test_wsp_collects_data() -> None:63    """Test _WikipediaSummaryParser.handle_data to make sure it collects data64     we found a <p> tag"""65    summary_parser = wikipedia_html_parsers.WikipediaSummaryParser()66    summary_parser.handle_starttag('p', [])67    summary_parser.handle_data('hello')68    assert summary_parser.summary == 'hello'69def test_wsp_skips_footnote() -> None:70    """Test _WikipediaSummaryParser.handle_data to make sure skips footnotes71    (<sup> tags)"""72    summary_parser = wikipedia_html_parsers.WikipediaSummaryParser()73    summary_parser.handle_starttag('p', [])74    summary_parser.handle_data('hello')75    summary_parser.handle_starttag('sup', [])76    summary_parser.handle_data('1')77    assert summary_parser.summary == 'hello'78def test_wsp_skips_non_p() -> None:79    """Test _WikipediaSummaryParser.handle_data to it collects data only when80    we are inside <p> tags. It should stop collecting data when we reach a </p> endtag"""81    summary_parser = wikipedia_html_parsers.WikipediaSummaryParser()82    summary_parser.handle_starttag('p', [])83    summary_parser.handle_data('hello')84    summary_parser.handle_endtag('p')85    summary_parser.handle_data('what is life')86    assert summary_parser.summary == 'hello'87def test_wsp_collects_2_sentences() -> None:88    """Test _WikipediaSummaryParser.handle_data to make sure it only collects89    2 sentences (when sentences wanted is set to default)"""90    summary_parser = wikipedia_html_parsers.WikipediaSummaryParser()91    summary_parser.handle_starttag('p', [])92    summary_parser.handle_data('what is life. life is meaningless.')93    summary_parser.handle_data('meaningless is life.')94    assert summary_parser.summary == 'what is life. life is meaningless.'95def test_wsp_strips_tail() -> None:96    """Test _WikipediaSummaryParser.handle_data to make sure it only collects97    2 sentences (when sentences wanted is set to default), and gets rid of any word98    tailing after the second period"""99    summary_parser = wikipedia_html_parsers.WikipediaSummaryParser()100    summary_parser.handle_starttag('p', [])101    summary_parser.handle_data('what is life. life is ')102    summary_parser.handle_data('meaningless. meaningless is life.')103    assert summary_parser.summary == 'what is life. life is meaningless.'104# ==================================================================================================105# TEST get_adjacent_url and get_adjacent_url_weighted106# ==================================================================================================107def test_get_adjacent_url() -> None:108    """ Test get_adjacent_url for getting the correct number of adjacent urls109    """110    expected = {'https://en.wikipedia.org/wiki/Thoroughbred',111                'https://en.wikipedia.org/wiki/Leading_sire_in_Great_Britain_and_Ireland',112                'https://en.wikipedia.org/wiki/Francis_Godolphin,_2nd_Earl_of_Godolphin',113                'https://en.wikipedia.org/wiki/Foundation_bloodstock',114                'https://en.wikipedia.org/wiki/Godolphin_Arabian',...test1.py
Source:test1.py  
...10parser11parser.reset12parser.reset()13class MyHTMLParser(HTMLParser):14    def handle_starttag(self, tag, attrs):15        print("Encountered a start tag:", tag)16    def handle_endtag(self, tag):17        print("Encountered an end tag:", tag)18    def handle_data(self, data):19        print("Encountered data:", data)20        21myparser = MyHTMLParser()22myparser.feed(raw)23class MyHTMLParser(HTMLParser):24    def handle_starttag(self, tag, attrs):25        if not tag == "script":26            print("Encountered a start tag:", tag)27    def handle_endtag(self, tag):28        if not tag == "script":29            print("Encountered an end tag:", tag)30    def handle_data(self, data):31        print("Encountered data:", data)32        33myparser = MyHTMLParser()34myparser.feed(raw)35class MyHTMLParser(HTMLParser):36    lasttag = ""37    def handle_starttag(self, tag, attrs):38        if not tag == "script":39            print("Encountered a start tag:", tag)40            lasttag = tag41    def handle_endtag(self, tag):42        if not tag == "script":43            print("Encountered an end tag:", tag)44    def handle_data(self, data):45        if lasttag != "script":46            print("Encountered data:", data)47        48from queue import LifoQueue49class MyHTMLParser(HTMLParser):50    lasttag = LifoQueue()51    def handle_starttag(self, tag, attrs):52        if not tag == "script":53            print("Encountered a start tag:", tag)54            lasttag.put(tag)55    def handle_endtag(self, tag):56        if not tag == "script":57            print("Encountered an end tag:", tag)58            lasttag.pop()59    def handle_data(self, data):60        if lasttag.get() != "script":61            print("Encountered data:", data)62        63myparser = MyHTMLParser()64myparser.feed(raw)65class MyHTMLParser(HTMLParser):66    prev_tag = LifoQueue()67    def handle_starttag(self, tag, attrs):68        if tag != "script":69            print("Encountered a start tag:", tag)70        prev_tag.put(tag)71    def handle_endtag(self, tag):72        if tag != "script":73            print("Encountered an end tag:", tag)74        prev_tag.pop()75    def handle_data(self, data):76        if prev_tag.get() != "script":77            print("Encountered data:", data)78        79myparser = MyHTMLParser()80myparser.feed(raw)81class MyHTMLParser(HTMLParser):82    83    def __init__(self, *, covert_charrefs=True):84        self.prev_tag = LifoQueue()85        self.convert_charrefs = convert_charrefs86        self.reset()87    88    def handle_starttag(self, tag, attrs):89        if tag != "script":90            print("Encountered a start tag:", tag)91        self.prev_tag.put(tag)92    def handle_endtag(self, tag):93        if tag != "script":94            print("Encountered an end tag:", tag)95        self.prev_tag.pop()96    def handle_data(self, data):97        if self.prev_tag.get() != "script":98            print("Encountered data:", data)99        100myparser = MyHTMLParser()101class MyHTMLParser(HTMLParser):102    103    def __init__(self, *, convert_charrefs=True):104        self.prev_tag = LifoQueue()105        self.convert_charrefs = convert_charrefs106        self.reset()107    108    def handle_starttag(self, tag, attrs):109        if tag != "script":110            print("Encountered a start tag:", tag)111        self.prev_tag.put(tag)112    def handle_endtag(self, tag):113        if tag != "script":114            print("Encountered an end tag:", tag)115        self.prev_tag.pop()116    def handle_data(self, data):117        if self.prev_tag.get() != "script":118            print("Encountered data:", data)119        120myparser = MyHTMLParser()121myparser.feed(raw)122from bs4 import BeautifulSoup...test_link_finder.py
Source:test_link_finder.py  
...9        self.base_url = Url('http://www.technovium.nl')10        self.page_url = Url('http://www.technovium.nl')11        self.sut = LinkFinder(page_url=self.page_url)12    def test_url_with_http(self):13        self.sut.handle_starttag('a', [('href', 'http://www.technovium.nl')])14        actual = str(self.sut.links.pop())15        expected = 'http://www.technovium.nl'16        self.assertEqual(actual, expected)17    def test_url_with_https(self):18        self.sut.handle_starttag('a', [('href', 'https://www.technovium.nl')])19        actual = str(self.sut.links.pop())20        expected = 'https://www.technovium.nl'21        self.assertEqual(actual, expected)22    def test_url_with_www(self):23        self.sut.handle_starttag('a', [('href', 'www.technovium.nl')])24        actual = str(self.sut.links.pop())25        expected = 'http://www.technovium.nl'26        self.assertEqual(actual, expected)27    def test_url_without_https_or_www(self):28        self.sut.handle_starttag('a', [('href', 'technovium.nl')])29        actual = str(self.sut.links.pop())30        expected = 'http://technovium.nl'31        self.assertEqual(actual, expected)32    def test_url_without_nl_domain(self):33        self.sut.handle_starttag('a', [('href', 'http://www.technovium.com')])34        actual = self.sut.links35        self.assertFalse(actual)36    def test_mailto(self):37        self.sut.handle_starttag('a', [('href', 'mailto:test@test.nl')])38        actual = self.sut.links39        self.assertFalse(actual)40    def test_http_http(self):41        self.sut.handle_starttag('a', [('href', 'http://http:')])42        actual = self.sut.links43        self.assertFalse(actual)44    def test_https(self):45        self.sut.handle_starttag('a', [('href', 'https://')])46        actual = self.sut.links47        self.assertFalse(actual)48    def test_http(self):49        self.sut.handle_starttag('a', [('href', 'http:')])50        actual = self.sut.links51        self.assertFalse(actual)52    def test_relative_url(self):53        self.sut.handle_starttag('a', [('href', '/over_ons')])54        expected = 'http://www.technovium.nl/over_ons'55        actual = str(self.sut.links.pop())56        self.assertEqual(actual, expected)57    def test_relative_url_starting_without_dash(self):58        self.sut.handle_starttag('a', [('href', 'over_ons.html')])59        expected = 'http://www.technovium.nl/over_ons.html'60        actual = str(self.sut.links.pop())61        self.assertEqual(actual, expected)62    def test_relative_url_starting_with_points(self):63        self.sut.page_url.url_string = 'http://www.technovium.nl/test/'64        self.sut.handle_starttag('a', [('href', '../over_ons')])65        expected = 'http://www.technovium.nl/over_ons'66        actual = str(self.sut.links.pop())67        self.assertEqual(actual, expected)68    def test_relative_url_starting_with_double_points_and_slashes(self):69        self.sut.page_url.url_string = 'http://www.technovium.nl/test/tweedetest/derdetest'70        self.sut.handle_starttag('a', [('href', '../../over_ons')])71        expected = 'http://www.technovium.nl/over_ons'72        actual = str(self.sut.links.pop())73        self.assertEqual(actual, expected)74    def test_double_relative_url(self):75        self.sut.page_url.url_string = 'http://www.technovium.nl/test/tweedetest/derdetest/vierdetest'76        self.sut.handle_starttag('a', [('href', '../../over_ons')])77        expected = 'http://www.technovium.nl/test/over_ons'78        actual = str(self.sut.links.pop())79        self.assertEqual(actual, expected)80    def test_blacklistnotfounderror_is_raised(self):81        Properties.BLACKLIST_FILE = 'Empty'82        with self.assertRaises(BlacklistNotFoundError):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
