-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sample.py
More file actions
78 lines (61 loc) · 2.48 KB
/
test_sample.py
File metadata and controls
78 lines (61 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Tests methods themselves
"""
from bs4 import BeautifulSoup # https://www.crummy.com/software/BeautifulSoup/bs4/doc/
from env import Env, env, env_ # pylint: disable=unused-import
def grab_all_links(htm: str):
"""
Parse the HTML code. extract all links.
return the list of urls
"""
print(" ************** GRAB-ALL-LINKS ******************* ")
soup = BeautifulSoup(htm, 'html.parser')
# print(soup.prettify())
# print(" ************** END-OF-Prettify ******************* ")
links = [link.get('href') for link in soup.find_all('a')
if link not in ['/', ""] # skip trash ones
]
links = [l for l in links if "www.w3" not in l] # reduce number DEBUG-ONLY. TODO: Remove
print(f"==>> Found {len(links)} links")
return links
def link_to_full_url(env: Env, link: str): # pylint: disable=redefined-outer-name
""" Convert link to full format:
Not changing those already in the full format
Prevents doubling slash - from host ending and link start symbol
"""
if link.startswith("https://"):
return link
# remove extra slash: when host_url ends with '/' and the link itself
if env.host_url[-1:] == '/' and link[0] == '/':
return env.host_url[:-1] + link
return env.host_url + link
def pytest_generate_tests(metafunc):
"""
pytest_generate_tests generates data-driven tests for the hardcoded parameters name.
https://docs.pytest.org/en/stable/example/parametrize.html
"""
print("** pytest_generate_tests ** ")
# print(metafunc.fixturenames)
if "link" in metafunc.fixturenames:
html = env_().get_endpoint('').text
links = [link_to_full_url(env=env_(), link=link) for link in grab_all_links(html)]
selected_links = links
print(links)
print(f"{len(links)} / {len(selected_links)}")
metafunc.parametrize("link", selected_links, ids=selected_links)
def test_link_valid(env, link: str): # pylint: disable=redefined-outer-name
""" Ensure the link s valid - otherwise assert """
print(f" >> testing link {link} --{link[-1]}")
# assert "#" not in link ### TO MAKE IT FAIL
res = env.get(link)
assert res.status_code == 200
# def main():
# """ main function is used to have variables inside it local """
# htm = env_().get_endpoint('/').text
# for link in grab_all_links(htm):
# # print(link)
# print(link_to_full_url(env=env_(), link=link))
#
#
# if __name__ == "__main__":
# main()