| 1 |
""" submit failure or test session information to a pastebin service. """ |
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
|
| 6 |
|
| 7 |
def pytest_addoption(parser): |
| 8 |
group = parser.getgroup("terminal reporting") |
| 9 |
group._addoption('--pastebin', metavar="mode", |
| 10 |
action='store', dest="pastebin", default=None, |
| 11 |
choices=['failed', 'all'], |
| 12 |
help="send failed|all info to bpaste.net pastebin service.") |
| 13 |
|
| 14 |
@pytest.hookimpl(trylast=True) |
| 15 |
def pytest_configure(config): |
| 16 |
import py |
| 17 |
if config.option.pastebin == "all": |
| 18 |
tr = config.pluginmanager.getplugin('terminalreporter') |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
if tr is not None: |
| 23 |
|
| 24 |
config._pastebinfile = tempfile.TemporaryFile('w+b') |
| 25 |
oldwrite = tr._tw.write |
| 26 |
def tee_write(s, **kwargs): |
| 27 |
oldwrite(s, **kwargs) |
| 28 |
if py.builtin._istext(s): |
| 29 |
s = s.encode('utf-8') |
| 30 |
config._pastebinfile.write(s) |
| 31 |
tr._tw.write = tee_write |
| 32 |
|
| 33 |
def pytest_unconfigure(config): |
| 34 |
if hasattr(config, '_pastebinfile'): |
| 35 |
|
| 36 |
config._pastebinfile.seek(0) |
| 37 |
sessionlog = config._pastebinfile.read() |
| 38 |
config._pastebinfile.close() |
| 39 |
del config._pastebinfile |
| 40 |
|
| 41 |
tr = config.pluginmanager.getplugin('terminalreporter') |
| 42 |
del tr._tw.__dict__['write'] |
| 43 |
|
| 44 |
tr.write_sep("=", "Sending information to Paste Service") |
| 45 |
pastebinurl = create_new_paste(sessionlog) |
| 46 |
tr.write_line("pastebin session-log: %s\n" % pastebinurl) |
| 47 |
|
| 48 |
def create_new_paste(contents): |
| 49 |
""" |
| 50 |
Creates a new paste using bpaste.net service. |
| 51 |
|
| 52 |
:contents: paste contents as utf-8 encoded bytes |
| 53 |
:returns: url to the pasted contents |
| 54 |
""" |
| 55 |
import re |
| 56 |
if sys.version_info < (3, 0): |
| 57 |
from urllib import urlopen, urlencode |
| 58 |
else: |
| 59 |
from urllib.request import urlopen |
| 60 |
from urllib.parse import urlencode |
| 61 |
|
| 62 |
params = { |
| 63 |
'code': contents, |
| 64 |
'lexer': 'python3' if sys.version_info[0] == 3 else 'python', |
| 65 |
'expiry': '1week', |
| 66 |
} |
| 67 |
url = 'https://bpaste.net' |
| 68 |
response = urlopen(url, data=urlencode(params).encode('ascii')).read() |
| 69 |
m = re.search(r'href="/raw/(\w+)"', response.decode('utf-8')) |
| 70 |
if m: |
| 71 |
return '%s/show/%s' % (url, m.group(1)) |
| 72 |
else: |
| 73 |
return 'bad response: ' + response |
| 74 |
|
| 75 |
def pytest_terminal_summary(terminalreporter): |
| 76 |
import _pytest.config |
| 77 |
if terminalreporter.config.option.pastebin != "failed": |
| 78 |
return |
| 79 |
tr = terminalreporter |
| 80 |
if 'failed' in tr.stats: |
| 81 |
terminalreporter.write_sep("=", "Sending information to Paste Service") |
| 82 |
for rep in terminalreporter.stats.get('failed'): |
| 83 |
try: |
| 84 |
msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc |
| 85 |
except AttributeError: |
| 86 |
msg = tr._getfailureheadline(rep) |
| 87 |
tw = _pytest.config.create_terminal_writer(terminalreporter.config, stringio=True) |
| 88 |
rep.toterminal(tw) |
| 89 |
s = tw.stringio.getvalue() |
| 90 |
assert len(s) |
| 91 |
pastebinurl = create_new_paste(s) |
| 92 |
tr.write_line("%s --> %s" %(msg, pastebinurl)) |