Skip to content

Commit 2ba9069

Browse files
authored
Merge pull request #90 from nicoddemus/pytest4-compat
pytest 4.1 compatibility fixes
2 parents c90add3 + 60b235c commit 2ba9069

File tree

8 files changed

+48
-47
lines changed

8 files changed

+48
-47
lines changed

testing/conftest.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ def pytest_addoption(parser):
3939
group.addoption(
4040
'--gx', action="append", dest="gspecs", default=None,
4141
help="add a global test environment, XSpec-syntax. ")
42-
group.addoption(
43-
'--gwscope', action="store", dest="scope", default="session",
44-
type="choice", choices=["session", "function"],
45-
help="set gateway setup scope, default: session.")
4642
group.addoption(
4743
'--pypy', action="store_true", dest="pypy",
4844
help="run some tests also against pypy")
@@ -52,13 +48,6 @@ def pytest_addoption(parser):
5248
"page on invalid addresses"))
5349

5450

55-
def pytest_report_header(config):
56-
return [
57-
"gateway test setup scope: %s" % config.getvalue("scope"),
58-
"execnet: {} -- {}".format(execnet.__file__, execnet.__version__),
59-
]
60-
61-
6251
@pytest.fixture
6352
def specssh(request):
6453
return getspecssh(request.config)
@@ -144,22 +133,22 @@ def anypython(request):
144133
executable = None
145134
py.test.skip("no {} found".format(name))
146135
if "execmodel" in request.fixturenames and name != 'sys.executable':
147-
backend = request.getfuncargvalue("execmodel").backend
136+
backend = request.getfixturevalue("execmodel").backend
148137
if backend != "thread":
149138
pytest.xfail(
150139
"cannot run {!r} execmodel with bare {}".format(backend, name))
151140
return executable
152141

153142

143+
@pytest.fixture(scope='session')
144+
def group():
145+
g = execnet.Group()
146+
yield g
147+
g.terminate(timeout=1)
148+
149+
154150
@pytest.fixture
155-
def gw(request, execmodel):
156-
scope = request.config.option.scope
157-
group = request.cached_setup(
158-
setup=execnet.Group,
159-
teardown=lambda group: group.terminate(timeout=1),
160-
extrakey="testgroup",
161-
scope=scope,
162-
)
151+
def gw(request, execmodel, group):
163152
try:
164153
return group[request.param]
165154
except KeyError:
@@ -180,14 +169,16 @@ def gw(request, execmodel):
180169
gw.proxygw = proxygw
181170
assert pname in group
182171
elif request.param == "ssh":
183-
sshhost = request.getfuncargvalue('specssh').ssh
172+
sshhost = request.getfixturevalue('specssh').ssh
184173
# we don't use execmodel.backend here
185174
# but you can set it when specifying the ssh spec
186175
gw = group.makegateway("ssh={}//id=ssh".format(sshhost))
187176
elif request.param == 'proxy':
188177
group.makegateway('popen//id=proxy-transport')
189178
gw = group.makegateway('popen//via=proxy-transport//id=proxy'
190179
'//execmodel=%s' % execmodel.backend)
180+
else:
181+
assert 0, "unknown execmodel: {}".format(request.param)
191182
return gw
192183

193184

testing/test_basics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,10 @@ def func(channel, data):
385385

386386
def test_remote_exc__no_kwargs(makegateway):
387387
gw = makegateway()
388-
pytest.raises(TypeError, gw.remote_exec, gateway_base, kwarg=1)
389-
pytest.raises(TypeError, gw.remote_exec, 'pass', kwarg=1)
388+
with pytest.raises(TypeError):
389+
gw.remote_exec(gateway_base, kwarg=1)
390+
with pytest.raises(TypeError):
391+
gw.remote_exec('pass', kwarg=1)
390392

391393

392394
@skip_win_pypy

testing/test_channel.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def test_waitclose_timeouterror(self, gw):
3737

3838
def test_channel_receive_timeout(self, gw):
3939
channel = gw.remote_exec('channel.send(channel.receive())')
40-
pytest.raises(channel.TimeoutError, "channel.receive(timeout=0.2)")
40+
with pytest.raises(channel.TimeoutError):
41+
channel.receive(timeout=0.2)
4142
channel.send(1)
4243
channel.receive(timeout=TESTTIMEOUT)
4344

@@ -263,9 +264,8 @@ def f(item):
263264
""")
264265
subchan = channel.receive()
265266
subchan.send(1)
266-
excinfo = pytest.raises(
267-
subchan.RemoteError,
268-
"subchan.waitclose(TESTTIMEOUT)")
267+
with pytest.raises(subchan.RemoteError) as excinfo:
268+
subchan.waitclose(TESTTIMEOUT)
269269
assert "42" in excinfo.value.formatted
270270
channel.send(1)
271271
channel.waitclose()
@@ -289,7 +289,8 @@ def test_channel_file_write_error(self, gw):
289289
f = channel.makefile()
290290
assert not f.isatty()
291291
channel.waitclose(TESTTIMEOUT)
292-
pytest.raises(IOError, f.write, 'hello')
292+
with pytest.raises(IOError):
293+
f.write('hello')
293294

294295
def test_channel_file_proxyclose(self, gw):
295296
channel = gw.remote_exec("""

testing/test_gateway.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ def test_remote_exec_error_after_close(self, gw):
138138

139139
def test_remote_exec_no_explicit_close(self, gw):
140140
channel = gw.remote_exec('channel.close()')
141-
excinfo = py.test.raises(
142-
channel.RemoteError,
143-
"channel.waitclose(TESTTIMEOUT)")
141+
with pytest.raises(channel.RemoteError) as excinfo:
142+
channel.waitclose(TESTTIMEOUT)
144143
assert "explicit" in excinfo.value.formatted
145144

146145
def test_remote_exec_channel_anonymous(self, gw):
@@ -218,9 +217,8 @@ def test_chdir_separation(self, tmpdir, makegateway):
218217
assert x.lower() == str(waschangedir).lower()
219218

220219
def test_remoteerror_readable_traceback(self, gw):
221-
e = py.test.raises(
222-
gateway_base.RemoteError,
223-
'gw.remote_exec("x y").waitclose()')
220+
with pytest.raises(gateway_base.RemoteError) as e:
221+
gw.remote_exec("x y").waitclose()
224222
assert "gateway_base" in e.value.formatted
225223

226224
def test_many_popen(self, makegateway):
@@ -253,9 +251,12 @@ def test_waitclose_on_remote_killed(self, makegateway):
253251
""")
254252
remotepid = channel.receive()
255253
py.process.kill(remotepid)
256-
py.test.raises(EOFError, "channel.waitclose(TESTTIMEOUT)")
257-
py.test.raises(IOError, channel.send, None)
258-
py.test.raises(EOFError, channel.receive)
254+
with pytest.raises(EOFError):
255+
channel.waitclose(TESTTIMEOUT)
256+
with pytest.raises(IOError):
257+
channel.send(None)
258+
with pytest.raises(EOFError):
259+
channel.receive()
259260

260261
def test_receive_on_remote_sysexit(self, gw):
261262
channel = gw.remote_exec("""
@@ -371,8 +372,8 @@ def test_status_with_threads(self, makegateway):
371372
class TestTracing:
372373
def test_popen_filetracing(self, testdir, monkeypatch, makegateway):
373374
tmpdir = testdir.tmpdir
374-
monkeypatch.setenv("TMP", tmpdir)
375-
monkeypatch.setenv("TEMP", tmpdir) # windows
375+
monkeypatch.setenv("TMP", str(tmpdir))
376+
monkeypatch.setenv("TEMP", str(tmpdir)) # windows
376377
monkeypatch.setenv('EXECNET_DEBUG', "1")
377378
gw = makegateway("popen")
378379
# hack out the debuffilename

testing/test_multi.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,17 @@ def test_gateway_and_id(self):
176176
group = Group()
177177
gw = group.makegateway("popen//id=hello")
178178
assert group["hello"] == gw
179-
py.test.raises((TypeError, AttributeError), "del group['hello']")
180-
py.test.raises((TypeError, AttributeError), "group['hello'] = 5")
179+
with pytest.raises((TypeError, AttributeError)):
180+
del group['hello']
181+
with pytest.raises((TypeError, AttributeError)):
182+
group['hello'] = 5
181183
assert 'hello' in group
182184
assert gw in group
183185
assert len(group) == 1
184186
gw.exit()
185187
assert 'hello' not in group
186-
py.test.raises(KeyError, "group['hello']")
188+
with pytest.raises(KeyError):
189+
_ = group['hello']
187190

188191
def test_default_group(self):
189192
oldlist = list(execnet.default_group)

testing/test_rsync.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ def test_dirsync_twice(self, dirs, gw1, gw2):
9090
rsync.add_target(gw1, dirs.dest1)
9191
rsync.send()
9292
assert dirs.dest1.join('hello').check()
93-
py.test.raises(IOError, "rsync.send()")
93+
with pytest.raises(IOError):
94+
rsync.send()
9495
assert rsync.send(raises=False) is None
9596
rsync.add_target(gw1, dirs.dest2)
9697
rsync.send()
9798
assert dirs.dest2.join('hello').check()
98-
py.test.raises(IOError, "rsync.send()")
99+
with pytest.raises(IOError):
100+
rsync.send()
99101
assert rsync.send(raises=False) is None
100102

101103
def test_rsync_default_reporting(self, capsys, dirs, gw1):

testing/test_serializer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ def py3(request):
116116

117117
@pytest.fixture(params=['py2', 'py3'])
118118
def dump(request):
119-
return request.getfuncargvalue(request.param).dump
119+
return request.getfixturevalue(request.param).dump
120120

121121

122122
@pytest.fixture(params=['py2', 'py3'])
123123
def load(request):
124-
return request.getfuncargvalue(request.param).load
124+
return request.getfixturevalue(request.param).load
125125

126126

127127
simple_tests = [

testing/test_xspec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def test_norm_attributes(self):
2323
assert spec.nice is None
2424
assert not hasattr(spec, '_xyz')
2525

26-
pytest.raises(AttributeError, "spec._hello")
26+
with pytest.raises(AttributeError):
27+
spec._hello()
2728

2829
spec = XSpec("socket=192.168.102.2:8888//python=python2.5//nice=3")
2930
assert spec.socket == "192.168.102.2:8888"

0 commit comments

Comments
 (0)