Adding decorator requires(ModuleName) for tests

This commit is contained in:
Lauro Neto 2009-12-15 16:04:07 -03:00
commit 859cec5632
5 changed files with 88 additions and 15 deletions

View file

@ -6,20 +6,31 @@ import sys
import unittest import unittest
from PySide.QtCore import QObject, SIGNAL, QProcess from PySide.QtCore import QObject, SIGNAL, QProcess
from PySide.QtGui import QApplication, QSpinBox, QPushButton
from helper import UsesQApplication try:
from PySide.QtGui import QApplication, QSpinBox, QPushButton
except ImportError:
QApplication = object
QSpinBox = object
QPushButton = object
from helper import UsesQApplication, UsesQCoreApplication
from helper import decorators
class Dummy(QObject): class Dummy(QObject):
def __init__(self, *args): def __init__(self, *args):
super(Dummy, self).__init__(*args) super(Dummy, self).__init__(*args)
class BasicCase(unittest.TestCase): class BasicCase(unittest.TestCase):
def testSimplePythonSignalNoArgs(self): def testSimplePythonSignalNoArgs(self):
#Connecting a lambda to a simple python signal without arguments #Connecting a lambda to a simple python signal without arguments
obj = Dummy() obj = Dummy()
QObject.connect(obj, SIGNAL('foo()'), lambda : setattr(obj, 'called', True)) QObject.connect(obj, SIGNAL('foo()'),
lambda: setattr(obj, 'called', True))
obj.emit(SIGNAL('foo()')) obj.emit(SIGNAL('foo()'))
self.assert_(obj.called) self.assert_(obj.called)
@ -27,11 +38,13 @@ class BasicCase(unittest.TestCase):
#Connecting a lambda to a simple python signal witharguments #Connecting a lambda to a simple python signal witharguments
obj = Dummy() obj = Dummy()
arg = 42 arg = 42
QObject.connect(obj, SIGNAL('foo(int)'), lambda x : setattr(obj, 'arg', 42)) QObject.connect(obj, SIGNAL('foo(int)'),
lambda x: setattr(obj, 'arg', 42))
obj.emit(SIGNAL('foo(int)'), arg) obj.emit(SIGNAL('foo(int)'), arg)
self.assertEqual(obj.arg, arg) self.assertEqual(obj.arg, arg)
class QtSigLambda(UsesQApplication):
class QtSigLambda(UsesQCoreApplication):
qapplication = True qapplication = True
@ -39,7 +52,8 @@ class QtSigLambda(UsesQApplication):
'''Connecting a lambda to a signal without arguments''' '''Connecting a lambda to a signal without arguments'''
proc = QProcess() proc = QProcess()
dummy = Dummy() dummy = Dummy()
QObject.connect(proc, SIGNAL('started()'), lambda : setattr(dummy, 'called', True)) QObject.connect(proc, SIGNAL('started()'),
lambda: setattr(dummy, 'called', True))
proc.start(sys.executable, ['-c', '""']) proc.start(sys.executable, ['-c', '""'])
proc.waitForFinished() proc.waitForFinished()
self.assert_(dummy.called) self.assert_(dummy.called)
@ -48,15 +62,21 @@ class QtSigLambda(UsesQApplication):
'''Connecting a lambda to a signal with arguments''' '''Connecting a lambda to a signal with arguments'''
proc = QProcess() proc = QProcess()
dummy = Dummy() dummy = Dummy()
QObject.connect(proc, SIGNAL('finished(int)'), lambda x: setattr(dummy, 'called', x)) QObject.connect(proc, SIGNAL('finished(int)'),
lambda x: setattr(dummy, 'called', x))
proc.start(sys.executable, ['-c', '""']) proc.start(sys.executable, ['-c', '""'])
proc.waitForFinished() proc.waitForFinished()
self.assertEqual(dummy.called, proc.exitCode()) self.assertEqual(dummy.called, proc.exitCode())
@decorators.requires('PySide.QtGui')
class QtGuiSigLambda(UsesQApplication):
def testButton(self): def testButton(self):
#Connecting a lambda to a QPushButton.clicked() #Connecting a lambda to a QPushButton.clicked()
obj = QPushButton('label') obj = QPushButton('label')
QObject.connect(obj, SIGNAL('clicked()'), lambda : setattr(obj, 'called', True)) QObject.connect(obj, SIGNAL('clicked()'),
lambda: setattr(obj, 'called', True))
obj.click() obj.click()
self.assert_(obj.called) self.assert_(obj.called)
@ -64,7 +84,8 @@ class QtSigLambda(UsesQApplication):
#Connecting a lambda to a QPushButton.clicked() #Connecting a lambda to a QPushButton.clicked()
obj = QSpinBox() obj = QSpinBox()
arg = 444 arg = 444
QObject.connect(obj, SIGNAL('valueChanged(int)'), lambda x: setattr(obj, 'arg', 444)) QObject.connect(obj, SIGNAL('valueChanged(int)'),
lambda x: setattr(obj, 'arg', 444))
obj.setValue(444) obj.setValue(444)
self.assertEqual(obj.arg, arg) self.assertEqual(obj.arg, arg)

View file

@ -13,6 +13,7 @@ except ImportError:
QSpinBox = object QSpinBox = object
from helper import BasicPySlotCase, UsesQApplication, UsesQCoreApplication from helper import BasicPySlotCase, UsesQApplication, UsesQCoreApplication
from helper.decorators import requires
def random_gen(count=100, largest=99, lowest=0): def random_gen(count=100, largest=99, lowest=0):
@ -46,6 +47,7 @@ class MultipleSignalConnections(unittest.TestCase):
self.assert_(rec.called) self.assert_(rec.called)
@requires('PySide.QtGui')
class QtGuiMultipleSlots(UsesQApplication, MultipleSignalConnections): class QtGuiMultipleSlots(UsesQApplication, MultipleSignalConnections):
'''Multiple connections to QtGui signals''' '''Multiple connections to QtGui signals'''

View file

@ -6,14 +6,21 @@ import sys
import unittest import unittest
from PySide.QtCore import QObject, QTimer, QCoreApplication, SIGNAL, SLOT from PySide.QtCore import QObject, QTimer, QCoreApplication, SIGNAL, SLOT
from PySide.QtGui import QSpinBox, QPushButton, QApplication
from helper import BasicPySlotCase, UsesQApplication
try:
from PySide.QtGui import QSpinBox, QPushButton, QApplication
except ImportError:
QSpinBox = object
QPushButton = object
QApplication = object
from helper import BasicPySlotCase, UsesQApplication
from helper.decorators import requires
@requires('PySide.QtGui')
class ButtonPySlot(UsesQApplication, BasicPySlotCase): class ButtonPySlot(UsesQApplication, BasicPySlotCase):
"""Tests the connection of python slots to QPushButton signals""" """Tests the connection of python slots to QPushButton signals"""
qapplication = True
def setUp(self): def setUp(self):
super(ButtonPySlot, self).setUp() super(ButtonPySlot, self).setUp()
@ -45,11 +52,10 @@ class ButtonPySlot(UsesQApplication, BasicPySlotCase):
self.assert_(self.called) self.assert_(self.called)
@requires('PySide.QtGui')
class SpinBoxPySlot(UsesQApplication, BasicPySlotCase): class SpinBoxPySlot(UsesQApplication, BasicPySlotCase):
"""Tests the connection of python slots to QSpinBox signals""" """Tests the connection of python slots to QSpinBox signals"""
qapplication = True
def setUp(self): def setUp(self):
super(SpinBoxPySlot, self).setUp() super(SpinBoxPySlot, self).setUp()
self.spin = QSpinBox() self.spin = QSpinBox()
@ -79,6 +85,7 @@ class SpinBoxPySlot(UsesQApplication, BasicPySlotCase):
self.args = (554,) self.args = (554,)
self.assertRaises(TypeError, self.spin.emit, SIGNAL('valueChanged(int)')) self.assertRaises(TypeError, self.spin.emit, SIGNAL('valueChanged(int)'))
@requires('PySide.QtGui')
class QSpinBoxQtSlots(UsesQApplication): class QSpinBoxQtSlots(UsesQApplication):
"""Tests the connection to QSpinBox qt slots""" """Tests the connection to QSpinBox qt slots"""

View file

@ -0,0 +1,43 @@
'''Decorators for skipping test methods and test cases'''
import logging
import unittest
class requires(object):
'''Skip if the given module is not found
Usage:
@requires('RequiredModule')
class MyTestCase(unittest.TestCase):
...
'''
def __init__(self, *args):
'''Setup this decorator. Args should be a list of
module names'''
self.skip = False
for module in args:
try:
__import__(module)
except ImportError:
self.skip = True
self.skipped = module
break
def __call__(self, klass):
'''Replace a skipped class with a dummy testCase'''
if not self.skip:
return klass
logging.warning('Module %s not found. Skipping %s' % (self.skipped,
klass.__name__))
class SkipWrapper(unittest.TestCase):
'''Dummy wrapper'''
pass
return SkipWrapper