Skip to content

Commit 7958545

Browse files
committed
testing QTest methods are available in qtbot
1 parent 62696cb commit 7958545

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

pytestqt/_tests/test_qtest_proxies.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from pytestqt.qt_compat import USING_PYSIDE
4+
5+
6+
fails_on_pyqt = pytest.mark.xfail(USING_PYSIDE, reason='not exported by PyQt')
7+
8+
9+
@pytest.mark.parametrize('expected_method', [
10+
'keyPress',
11+
'keyClick',
12+
'keyClicks',
13+
'keyEvent',
14+
'keyPress',
15+
'keyRelease',
16+
fails_on_pyqt('keyToAscii'),
17+
18+
'mouseClick',
19+
'mouseDClick',
20+
'mouseEvent',
21+
'mouseMove',
22+
'mousePress',
23+
'mouseRelease',
24+
],
25+
)
26+
def test_expected_qtest_proxies(qtbot, expected_method):
27+
"""
28+
Ensure that we are exporting expected QTest API methods.
29+
"""
30+
hasattr(qtbot, expected_method)
31+
assert getattr(qtbot, expected_method).__name__ == expected_method

pytestqt/plugin.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ def _inject_qtest_methods(cls):
1616

1717
def create_qtest_proxy_method(method_name):
1818

19-
qtest_method = getattr(QtTest.QTest, method_name)
19+
if hasattr(QtTest.QTest, method_name):
20+
qtest_method = getattr(QtTest.QTest, method_name)
2021

21-
def result(*args, **kwargs):
22-
return qtest_method(*args, **kwargs)
22+
def result(*args, **kwargs):
23+
return qtest_method(*args, **kwargs)
2324

24-
functools.update_wrapper(result, qtest_method)
25-
return staticmethod(result)
25+
functools.update_wrapper(result, qtest_method)
26+
return staticmethod(result)
27+
else:
28+
return None
2629

2730
# inject methods from QTest into QtBot
2831
method_names = [
@@ -43,7 +46,8 @@ def result(*args, **kwargs):
4346
]
4447
for method_name in method_names:
4548
method = create_qtest_proxy_method(method_name)
46-
setattr(cls, method_name, method)
49+
if method is not None:
50+
setattr(cls, method_name, method)
4751

4852
return cls
4953

@@ -113,7 +117,9 @@ class QtBot(object):
113117
:param Qt.Key_* key: one of the constants for keys in the Qt namespace.
114118
115119
:return type: str
116-
:returns: the equivalent character string.
120+
:returns: the equivalent character string.
121+
122+
.. note:: this method is not available in PyQt.
117123
118124
---
119125
@@ -149,7 +155,7 @@ class QtBot(object):
149155
:param int delay: after the event, delay the test for this miliseconds (if > 0).
150156
151157
152-
.. _QTest API: http://doc.qt.digia.com/4.7/qtest.html
158+
.. _QTest API: http://doc.qt.digia.com/4.8/qtest.html
153159
154160
"""
155161

pytestqt/qt_compat.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
try:
1515
import PySide.QtCore as _QtCore
1616
QtCore = _QtCore
17-
USE_PYSIDE = True
17+
USING_PYSIDE = True
1818
except ImportError:
19-
USE_PYSIDE = False
19+
USING_PYSIDE = False
2020

2121
FORCE_PYQT = os.environ.get('PYTEST_QT_FORCE_PYQT', 'false') == 'true'
22-
if not USE_PYSIDE or FORCE_PYQT:
22+
if not USING_PYSIDE or FORCE_PYQT:
2323
try:
2424
import sip
2525
except ImportError:
2626
msg = 'pytest-qt requires either PyQt4 or PySide to be installed'
2727
raise ImportError(msg)
28-
USE_PYSIDE = False
28+
USING_PYSIDE = False
2929
sip.setapi('QString', 2)
3030
sip.setapi('QVariant', 2)
3131
import PyQt4.QtCore as _QtCore
3232
QtCore = _QtCore
3333

34-
if USE_PYSIDE:
34+
if USING_PYSIDE:
3535
def _import_module(moduleName):
3636
pyside = __import__('PySide', globals(), locals(), [moduleName], 0)
3737
return getattr(pyside, moduleName)
@@ -55,8 +55,9 @@ def _import_module(moduleName):
5555
QEvent = QtCore.QEvent
5656

5757
else:
58-
# mock Qt when we are generating documentation at readthedocs.org
58+
USING_PYSIDE = True
5959

60+
# mock Qt when we are generating documentation at readthedocs.org
6061
class Mock(object):
6162
def __init__(self, *args, **kwargs):
6263
pass

0 commit comments

Comments
 (0)