Skip to content

Instantly share code, notes, and snippets.

@hithwen
Last active December 17, 2015 14:19
Show Gist options
  • Save hithwen/5623345 to your computer and use it in GitHub Desktop.
Save hithwen/5623345 to your computer and use it in GitHub Desktop.
Buildbot nosetest and nosecoverage with html reports, only works with slave and master in same machine, otherwhise you'll probably need a FileUpload extra step
import os
from buildbot.steps.shell import ShellCommand
from buildbot.process.properties import WithProperties
import stat
import StringIO
import re
class NoseTest(ShellCommand):
report_path = '/var/www/testreport/%s.html'
def __init__(self):
d = WithProperties('--html-file=' + self.report_path, 'buildnumber')
command = ['nosetests', '-v', '--with-html', d]
description = 'Running tests'
workdir = 'build/python'
ShellCommand.__init__(self, command=command, description=description,
workdir=workdir, haltOnFailure=True)
def createSummary(self, log):
buildnumber = self.getProperty('buildnumber')
lines = StringIO.StringIO(log.getText())
doc = self.report_path.split('/')[-1]
url = "https://my-url/testreport/%s" % doc
passed, total = self._getRatio(lines)
os.chmod(self.report_path % buildnumber, stat.S_IROTH);
self.addURL('passing %s/%s' % (passed, total), url % buildnumber)
def _getRatio(self, lines):
'''Returns total and passed tests'''
passed = None
total = 0
for line in lines.readlines():
if line.startswith('Ran'):
total = line.split()[1]
if line.startswith('FAILED'):
regex = re.compile("FAILED \((errors=[0-9]+)?(, )?(failures=[0-9]+)?\)")
r = regex.search(line)
groups = r.groups()
passed = int(total)
if isinstance(groups[0], basestring):
passed = passed - int(groups[0].split('=')[-1])
if len(groups) == 3 and isinstance(groups[2], basestring):
passed = passed - int(groups[2].split('=')[-1])
break
if passed is None:
passed = total
return (passed, total)
class TestWithCodeCoverage(NoseTest):
def __init__(self):
self.coverage_path = '/var/www/coverage'
command = ['nosetests', '--with-coverage', '--cover-package=myproj',
'--cover-erase', '--cover-min-percentage=80', '--cover-html',
'--cover-html-dir=%s' % self.coverage_path]
description = 'Generating coverage'
workdir='build/python'
ShellCommand.__init__(self, command=command, description=description, workdir=workdir)
def createSummary(self, log):
lines = StringIO.StringIO(log.getText())
percentage = 'N/A'
for line in lines.readlines():
if 'TOTAL' in line:
percentage = line.split()[-1]
break
url = "my-url/coverage"
self.addURL("coverage [%s]" %percentage, url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment