Skip to content

Instantly share code, notes, and snippets.

@els-pnw
Created June 12, 2012 13:23
Show Gist options
  • Save els-pnw/2917490 to your computer and use it in GitHub Desktop.
Save els-pnw/2917490 to your computer and use it in GitHub Desktop.
import bugzilla
import ConfigParser
import pytest
class BugzillaHooks(object):
def __init__(self, config, bugzilla):
self.config = config
self.bugzilla = bugzilla
def pytest_runtest_setup(self, item):
if 'bugzilla' in item.keywords:
marker = item.keywords['bugzilla']
if len(marker.args) != 1:
raise TypeError('Bugzilla marker must have exactly 1 argument')
bug_id = item.keywords['bugzilla'].args[0]
bug = self.bugzilla.getbugsimple(bug_id)
status = str(bug).split(None, 2)[1]
if status in ['NEW', 'ASSIGNED', 'ON_DEV']:
pytest.skip("https://bugzilla.redhat.com/show_bug.cgi?id=%s" % bug_id)
def pytest_addoption(parser):
"""
Add a options section to py.test --help for bugzilla integration.
Parse configuration file, bugzilla.cfg and / or the command line options
passed.
"""
config = ConfigParser.ConfigParser()
config.read('bugzilla.cfg')
group = parser.getgroup('Bugzilla integration')
group.addoption('--bugzilla',
action='store_true',
default=False,
dest='bugzilla',
help='Enable Bugzilla support.')
group.addoption('--bugzilla-url',
action='store',
dest='bugzilla_url',
default=config.get('DEFAULT', 'bugzilla_url'),
metavar='url',
help='Overrides the xmlrpc url for bugzilla found in bugzilla.cfg.')
group.addoption('--bugzilla-user',
action='store',
dest='bugzilla_username',
default=config.get('DEFAULT', 'bugzilla_username'),
metavar='username',
help='Overrides the bugzilla username in bugzilla.cfg.')
group.addoption('--bugzilla-password',
action='store',
dest='bugzilla_password',
default=config.get('DEFAULT', 'bugzilla_password'),
metavar='password',
help='Overrides the bugzilla password in bugzilla.cfg.')
def pytest_configure(config):
"""
If bugzilla is neabled, setup a session
with bugzilla_url.
"""
if config.getvalue("bugzilla"):
url = config.getvalue('bugzilla_url')
user = config.getvalue('bugzilla_username')
password = config.getvalue('bugzilla_password')
bz = bugzilla.Bugzilla(url=url)
bz.login(user,password)
my = BugzillaHooks(config, bz)
ok = config.pluginmanager.register(my, "bugzilla_helper")
assert ok
@els-pnw
Copy link
Author

els-pnw commented Jun 12, 2012

$ py.test --bugzilla -v
============================= test session starts ==============================
platform linux2 -- Python 2.7.1 -- pytest-2.2.4 -- /usr/bin/python
collected 3 items

test_nothin.py:7: TestNothin.test_new_bz SKIPPED
test_nothin.py:12: TestNothin.test_closed_bz PASSED
test_nothin.py:16: TestNothin.test_closed_bz_with_failure FAILED

=================================== FAILURES ===================================
____________________ TestNothin.test_closed_bz_with_failure ____________________

self = <test_nothin.TestNothin object at 0x24c0290>

@pytest.mark.bugzilla('12345')
def test_closed_bz_with_failure(self):
  assert(os.path.exists('/etcccc'))

E assert <function exists at 0x1665050>('/etcccc')
E + where <function exists at 0x1665050> = <module 'posixpath' from '/usr/lib64/python2.7/posixpath.pyc'>.exists
E + where <module 'posixpath' from '/usr/lib64/python2.7/posixpath.pyc'> = os.path

test_nothin.py:18: AssertionError
================ 1 failed, 1 passed, 1 skipped in 11.93 seconds ================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment