-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
117 lines (85 loc) · 3.59 KB
/
conftest.py
File metadata and controls
117 lines (85 loc) · 3.59 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python3
# -*- encoding=utf8 -*-
# This is example shows how we can manage failed tests
# and make screenshots after any failed test case.
import pytest
import allure
import uuid
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
# This function helps to detect that some test failed
# and pass this information to teardown:
outcome = yield
rep = outcome.get_result()
setattr(item, "rep_" + rep.when, rep)
return rep
@pytest.fixture
def chrome_options(chrome_options):
# chrome_options.binary_location = '/usr/bin/google-chrome-stable'
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--log-level=DEBUG')
return chrome_options
@pytest.fixture
def web_browser(request, selenium):
browser = selenium
browser.set_window_size(1400, 1000)
# Return browser instance to test case:
yield browser
# Do teardown (this code will be executed after each test):
if request.node.rep_call.failed:
# Make the screen-shot if test failed:
try:
browser.execute_script("document.body.bgColor = 'white';")
# Make screen-shot for local debug:
browser.save_screenshot('screenshots/' + str(uuid.uuid4()) + '.png')
# Attach screenshot to Allure report:
allure.attach(browser.get_screenshot_as_png(),
name=request.function.__name__,
attachment_type=allure.attachment_type.PNG)
# For happy debugging:
print('URL: ', browser.current_url)
print('Browser logs:')
for log in browser.get_log('browser'):
print(log)
except:
pass # just ignore any errors here
def get_test_case_docstring(item):
""" This function gets doc string from test case and format it
to show this docstring instead of the test case name in reports.
"""
full_name = ''
if item._obj.__doc__:
# Remove extra whitespaces from the doc string:
name = str(item._obj.__doc__.split('.')[0]).strip()
full_name = ' '.join(name.split())
# Generate the list of parameters for parametrized test cases:
if hasattr(item, 'callspec'):
params = item.callspec.params
res_keys = sorted([k for k in params])
# Create List based on Dict:
res = ['{0}_"{1}"'.format(k, params[k]) for k in res_keys]
# Add dict with all parameters to the name of test case:
full_name += ' Parameters ' + str(', '.join(res))
full_name = full_name.replace(':', '')
return full_name
def pytest_itemcollected(item):
""" This function modifies names of test cases "on the fly"
during the execution of test cases.
"""
if item._obj.__doc__:
item._nodeid = get_test_case_docstring(item)
def pytest_collection_finish(session):
""" This function modified names of test cases "on the fly"
when we are using --collect-only parameter for pytest
(to get the full list of all existing test cases).
"""
if session.config.option.collectonly is True:
for item in session.items:
# If test case has a doc string we need to modify it's name to
# it's doc string to show human-readable reports and to
# automatically import test cases to test management system.
if item._obj.__doc__:
full_name = get_test_case_docstring(item)
print(full_name)
pytest.exit('Done!')