-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
133 lines (108 loc) · 4.18 KB
/
conftest.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import base64
import pytest
import allure
from py._xmlgen import html
from selenium import webdriver
from common.readconfig import ini
from config.conf import SCREENSHOT_DIR
from common.inspect import inspect_element
from tools.send_mail import send_report
from tools.time import datetime_strftime, timestamp
from tools.clear import testclear
driver = None
@pytest.fixture(scope='session', autouse=True)
def drivers(request):
global driver
if driver is None:
options = webdriver.ChromeOptions()
options.add_argument('--disable-desktop-notifications')
options.add_argument('--blink-settings=imagesEnabled=false')
driver = webdriver.Chrome(options=options)
inspect_element()
testclear()
def fn():
driver.quit()
request.addfinalizer(fn)
return driver
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
当测试失败的时候,自动截图,展示到html报告中
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
screen_img = _capture_screenshot()
if screen_img:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:1024px;height:768px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra
report.description = str(item.function.__doc__)
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(1, html.th('用例名称'))
cells.insert(2, html.th('Test_nodeid'))
cells.pop(2)
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
cells.insert(1, html.td(report.description))
cells.insert(2, html.td(report.nodeid))
cells.pop(2)
@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):
if report.passed:
del data[:]
data.append(html.div('通过的用例未捕获日志输出.', class_='empty log'))
@pytest.mark.optionalhook
def pytest_html_report_title(report):
report.title = "pytest示例项目测试报告"
@pytest.mark.optionalhook
def pytest_configure(config):
config._metadata.clear()
config._metadata['测试项目'] = "精准营销"
config._metadata['测试地址'] = ini.url
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
# prefix.clear() # 清空summary中的内容
prefix.extend([html.p("所属部门: 艾塔公司测试部")])
prefix.extend([html.p("测试执行人: sww")])
def pytest_terminal_summary(terminalreporter, exitstatus, config):
"""收集测试结果"""
result = {
"total": terminalreporter._numcollected,
'passed': len(terminalreporter.stats.get('passed', [])),
'failed': len(terminalreporter.stats.get('failed', [])),
'error': len(terminalreporter.stats.get('error', [])),
'skipped': len(terminalreporter.stats.get('skipped', [])),
# terminalreporter._sessionstarttime 会话开始时间
'total times': timestamp() - terminalreporter._sessionstarttime
}
print(result)
if result['failed'] or result['error']:
send_report()
def _capture_screenshot():
'''
截图保存为base64
'''
now_time = datetime_strftime("%Y%m%d%H%M%S")
if not os.path.exists(SCREENSHOT_DIR):
os.makedirs(SCREENSHOT_DIR)
screen_path = os.path.join(SCREENSHOT_DIR, "{}.png".format(now_time))
driver.save_screenshot(screen_path)
allure.attach.file(screen_path, "测试失败截图...{}".format(
now_time), allure.attachment_type.PNG)
with open(screen_path, 'rb') as f:
imagebase64 = base64.b64encode(f.read())
return imagebase64.decode()
def pytest_configure(config):
config.addinivalue_line("markers", "jz:精准营销")
config.addinivalue_line("markers", "bd:后台")