pastebin.py 3.4 KB

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