Skip to content

Commit 842ad41

Browse files
committed
install reactor earlier, in pytest_configure hook
1 parent 3263af3 commit 842ad41

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

pytest_twisted.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def in_reactor(d, f, *args):
120120

121121

122122
@pytest.fixture(scope="session", autouse=True)
123-
def twisted_greenlet(request, reactor):
123+
def twisted_greenlet(request):
124124
request.addfinalizer(stop_twisted_greenlet)
125125
return _instances.gr_twisted
126126

@@ -144,7 +144,7 @@ def init_default_reactor():
144144
)
145145

146146

147-
def init_qt5_reactor(qapp):
147+
def init_qt5_reactor():
148148
import qt5reactor
149149

150150
_install_reactor(
@@ -153,18 +153,12 @@ def init_qt5_reactor(qapp):
153153
)
154154

155155

156-
_reactor_fixtures = {
156+
_reactor_installers = {
157157
'default': init_default_reactor,
158158
'qt5reactor': init_qt5_reactor,
159159
}
160160

161161

162-
def _init_reactor():
163-
import twisted.internet.reactor
164-
_instances.reactor = twisted.internet.reactor
165-
init_twisted_greenlet()
166-
167-
168162
def _install_reactor(reactor_installer, reactor_type):
169163
try:
170164
reactor_installer()
@@ -177,24 +171,19 @@ def _install_reactor(reactor_installer, reactor_type):
177171
type(twisted.internet.reactor),
178172
)
179173
)
180-
_init_reactor()
174+
import twisted.internet.reactor
175+
_instances.reactor = twisted.internet.reactor
176+
init_twisted_greenlet()
181177

182178

183179
def pytest_addoption(parser):
184180
group = parser.getgroup('twisted')
185181
group.addoption(
186182
'--reactor',
187183
default='default',
188-
choices=tuple(_reactor_fixtures.keys()),
184+
choices=tuple(_reactor_installers.keys()),
189185
)
190186

191187

192188
def pytest_configure(config):
193-
reactor_fixture = _reactor_fixtures[config.getoption('reactor')]
194-
195-
class ReactorPlugin(object):
196-
reactor = staticmethod(
197-
pytest.fixture(scope='session', autouse=True)(reactor_fixture)
198-
)
199-
200-
config.pluginmanager.register(ReactorPlugin())
189+
_reactor_installers[config.getoption('reactor')]()

testing/test_basic.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,17 @@ def test_succeed():
198198

199199
@skip_if_reactor_not('default')
200200
def test_wrong_reactor(testdir, cmd_opts):
201-
testdir.makepyfile("""
201+
testdir.makeconftest("""
202+
def pytest_addhooks():
202203
import twisted.internet.reactor
203204
twisted.internet.reactor = None
204-
205+
""")
206+
testdir.makepyfile("""
205207
def test_succeed():
206208
pass
207209
""")
208210
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
209-
assert 'WrongReactorAlreadyInstalledError' in rr.stdout.str()
210-
assert_outcomes(rr, {'error': 1})
211+
assert 'WrongReactorAlreadyInstalledError' in rr.stderr.str()
211212

212213

213214
@skip_if_reactor_not('qt5reactor')
@@ -219,9 +220,7 @@ def test_blockon_in_hook_with_qt5reactor(testdir, cmd_opts):
219220
220221
221222
def pytest_configure(config):
222-
qapp = pytestqt.plugin.qapp(pytestqt.plugin.qapp_args())
223-
224-
pt.init_qt5_reactor(qapp)
223+
pt.init_qt5_reactor()
225224
d = defer.Deferred()
226225
227226
from twisted.internet import reactor
@@ -242,16 +241,18 @@ def test_succeed():
242241

243242
@skip_if_reactor_not('qt5reactor')
244243
def test_wrong_reactor_with_qt5reactor(testdir, cmd_opts):
245-
testdir.makepyfile("""
244+
testdir.makeconftest("""
245+
def pytest_addhooks():
246246
import twisted.internet.default
247247
twisted.internet.default.install()
248-
248+
""")
249+
testdir.makepyfile("""
249250
def test_succeed():
250251
pass
251252
""")
252253
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
253-
assert 'WrongReactorAlreadyInstalledError' in rr.stdout.str()
254-
assert_outcomes(rr, {'error': 1})
254+
assert 'WrongReactorAlreadyInstalledError' in rr.stderr.str()
255+
# assert_outcomes(rr, {'error': 1})
255256

256257

257258
@skip_if_reactor_not('default')

0 commit comments

Comments
 (0)